2

/** I have some methods likes add,display,sort,delete,and exit that implemented the ArrayList function. It works correctly, but the problem is that the objects that had been added were not saved on a .txt file, just the temporary objects. So I need to add them into text file,so that I can display and delete them later. Here's the part of the codes. */

public class testing {

    public static void main(String[] args) {
        String Command;
        int index = 0;
        Scanner input = new Scanner(System.in);
        ArrayList<String> MenuArray = new ArrayList<String>();
        boolean out = false;
        while (!out) {
            System.out.print("Enter your Command: ");
            Command = input.nextLine();
            // method ADD for adding object
            if (Command.startsWith("ADD ") || Command.startsWith("add ")) {
                MenuArray.add(Command.substring(4).toLowerCase());
                // indexing the object
                index++;
                /** i stuck here,it won't written into input.txt 
                BufferedWriter writer = new BufferedWriter(new FileWriter(
                        "input.txt"));
                try {
                    for (String save : MenuArray) {
                        int i = 0;
                        writer.write(++i + ". " + save.toString());
                        writer.write("\n");
                    }
                } finally {
                    writer.close();
                }*/
            } else if (Command.startsWith("EXIT") || Comand.startsWith("exit")) {
                out = true;
            }
        }
    }
}
Mohsen Heydari
  • 7,256
  • 4
  • 31
  • 46
lawZ
  • 39
  • 1
  • 3
  • 9

2 Answers2

4

FileUtils#writeLines seems to do exactly what you need.

sp00m
  • 47,968
  • 31
  • 142
  • 252
  • tq for answering the quest, but i do not really have much time to read the manuals. – lawZ Sep 28 '13 at 10:32
  • 1
    @user2826017 This is a complete answer, no need for reading any manual. Download the apache jar, import it in your project, and use `FileUtils.writeLines(new File("input.txt"), MenuArray);`. – Vincent van der Weele Sep 28 '13 at 10:40
  • okay, i'll try it ... I just learnt Java about a few days ago by my ownself, and now doing this tasks ... so there're so many stuffs that i don't really get it. – lawZ Sep 28 '13 at 10:45
  • It would be better, though, if we helped the OP to solve his original problem - why can't he use a Writer and a loop to do it? – Ingo Sep 28 '13 at 11:29
3

You can use ObjectOutputStream to write an object into a file:

try {
    FileOutputStream fos = new FileOutputStream("output");
    ObjectOutputStream oos = new ObjectOutputStream(fos);   
    oos.writeObject(MenuArray); // write MenuArray to ObjectOutputStream
    oos.close(); 
} catch(Exception ex) {
    ex.printStackTrace();
}
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • i tried the code, it works and create the ouput.txt,but this is what's inside the file :¬í sr java.util.ArrayListxÒ™Ça I sizexp w t hellox // I just typed the word 'hello' – lawZ Sep 28 '13 at 10:30
  • Dude,then how can i display the objects inside of the file ? here is the next method : else if(Command.startsWith("DISPLAY") || Command.startsWith("display") ) { int i=0; for(String temp: MenuArray){ System.out.println(++i + ". " + temp); } – lawZ Sep 28 '13 at 10:39
  • @lawZ: You read the file using `ObjectInputStream` see an example code `http://www.tutorialspoint.com/java/io/objectinputstream_readobject.htm` – anubhava Sep 28 '13 at 12:24
  • please take a look at here : http://stackoverflow.com/questions/19066952/how-can-i-put-read-and-modified-my-arraylist-objects-on-a-txt-file – lawZ Sep 28 '13 at 12:35
  • @lawZ: I see your related question. Do you want me to post same reply there? – anubhava Sep 28 '13 at 12:38