Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving a Linked List to a file on seperate lines
    primarykey
    data
    text
    <p>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!</p> <p><strong>Code for Saving Linked List to file:</strong></p> <pre><code> 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></pre> <p><strong>Code for creating Linked List:</strong></p> <pre><code>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() &gt; 2){ return rl; }else return readLine(br); } public static void main(String[] args) { LinkedList&lt;Account&gt; account = new LinkedList&lt;Account&gt;(); try { read(account, "output.txt"); } catch (Exception e) { System.err.println(e.toString()); } display(account); } public static void read(LinkedList&lt;Account&gt; 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&lt;?&gt; c) { for (Object e : c) { System.out.println(e); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload