Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7624481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:58:35+00:00 2026-05-31T04:58:35+00:00

I will try to keep this as simple as possible. Basically I have a

  • 0

I will try to keep this as simple as possible. Basically I have a Linked List of data where every element is on a separate line. However when I try to save it to a file it all just gets linked together in one long String. I need it to save to the file on different lines because I have to keep reading and saving to this file multiple times, and the way I read the file everything has to be on separate lines.
Thanks!

Code for Saving Linked List to file:

        String file_name = "output.txt";
        try {

                FileWriter fstream = new FileWriter(file_name);
                BufferedWriter out = new BufferedWriter(fstream);

                ListIterator itr = account.listIterator();
                while (itr.hasNext()) {
                    Account element = (Account) itr.next();
                    out.write(element + "\n");
                }

                out.close();
                System.out.println("File created successfully.");

        } catch (Exception e) {
        }

Code for creating Linked List:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
public class Main extends javax.swing.JFrame implements ActionListener{

public static String readLine(BufferedReader br) throws IOException {
    String rl = br.readLine();
    if (rl.trim().length() > 2){
        return rl;
    }else return readLine(br);
}

public static void main(String[] args) {

    LinkedList<Account> account = new LinkedList<Account>();

    try
    {
        read(account, "output.txt");
    } catch (Exception e)
    {
        System.err.println(e.toString());
    }
        display(account);
    }

    public static void read(LinkedList<Account> account, String inputFileName) throws java.io.IOException
    {
        BufferedReader infile = new BufferedReader(new FileReader(inputFileName));
        while(infile.ready())
        {

            String username = readLine(infile);
            String password = readLine(infile);
            String email = readLine(infile);
            String name = readLine(infile);
            String breed = readLine(infile);
            String gender = readLine(infile);
            String age = readLine(infile);
            String state = readLine(infile);
            String hobby = readLine(infile);

            Account a = new Account(username, password, email, name, breed, gender, age, state, hobby);
            account.add(a);
            a.showList();
        }
        infile.close();
    }

    public static void display(LinkedList<?> c)
    {
        for (Object e : c)
        {
            System.out.println(e);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:58:36+00:00Added an answer on May 31, 2026 at 4:58 am

    Instead,

    out.write(element + "\n");
    

    try

    out.write(element);
    out.newLine();
    

    Referring to the javadoc of newLine() method:

    Write a line separator. The line separator string is defined by the
    system property line.separator, and is not necessarily a single
    newline (‘\n’) character.

    So, \n does not seem correct always for new line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I will try to keep this as simple as possible. I have a rather
I'll try and keep this simple, I have worked on projects in the past
I will try to describe it as simple as possible. We have SqlConnection and
I'll try to keep this as simple as possible. A button created in a
I'll try and keep this simple. .h NSMutableArray *allEventsData; @property (nonatomic, retain) NSMutableArray *allEventsData;
I will try to keep it simple: In my main activity I make a
I have a simple doubly linked list example that I'm working on, but for
I'll try to keep my question simple: I have the following instance: MainClass mc
I will try to make this as clear as I can, but if you
I will try to be as clear as possible because I can't get anybody

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.