Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I put,read,and modified my ArrayList Objects on a .txt file?
    text
    copied!<p>Here is the modified code, when i run the program it works,but it doesn't work as i expected. I don't know why it won't write the lines that i typed after typing "add", and also it didn't show anything when i typed "show". Seems like i might missing something :</p> <pre><code>import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.nio.charset.Charset; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; import java.util.List; public class unfinished { public static void main(String[] args) throws IOException { //String command; //int index = 0; Path path = FileSystems.getDefault().getPath("source.txt"); List&lt;String&gt; list = loadList(path); try(Scanner sc = new Scanner(System.in)){ // System.out.print("Enter the Command: "); String[] input = sc.nextLine().split(" "); while(input.length &gt; 0 &amp;&amp; !input[0].equals("exit")){ switch(input[0]){ case "add" : addToList(input, list); break; case "remove" : removeFromList(input, list); break; case "show": showList(input, list); break; } } input = sc.nextLine().split(" "); } saveList(path, list); } </code></pre> <p>here is the part of my old code for sorting and clear:</p> <pre><code>/** Collections.sort(MenuArray); int i = 0; for (String temporary : MenuArray) { System.out.println(++i + ". " + temporary); } //clear MenuArray.clear(); System.out.println("All objects have been cleared !"); */ private static void saveList(Path path, List&lt;String&gt; list) throws IOException { // TODO Auto-generated method stub Files.write(path, list, Charset.defaultCharset(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); } private static void removeFromList(String[] input, List&lt;String&gt; list) { // TODO Auto-generated method stub } private static void showList(String[] input, List&lt;String&gt; list) { // TODO Auto-generated method stub } private static void addToList(String[] input, List&lt;String&gt; list) { // TODO Auto-generated method stub } private static List&lt;String&gt; loadList(Path path) throws IOException { // TODO Auto-generated method stub return Files.readAllLines(path, Charset.defaultCharset()); } } </code></pre>
 

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