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;
            }
        }
    }
}
3
  • 2
    Please fix code formatting first! You should respect Java coding guidelines (e.g. lowercase for variables etc.)! Commented Sep 28, 2013 at 10:20
  • You should not ask the same question twice Commented Sep 28, 2013 at 19:14
  • yes,next time it won't happened again. Commented Sep 29, 2013 at 5:27

2 Answers 2

4

FileUtils#writeLines seems to do exactly what you need.

Sign up to request clarification or add additional context in comments.

4 Comments

tq for answering the quest, but i do not really have much time to read the manuals.
@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);.
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.
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?
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();
}

5 Comments

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'
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: You read the file using ObjectInputStream see an example code http://www.tutorialspoint.com/java/io/objectinputstream_readobject.htm
please take a look at here : stackoverflow.com/questions/19066952/…
@lawZ: I see your related question. Do you want me to post same reply there?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.