[Solved] [Java] How to write multiple lines in a file

Discussion in 'Programming' started by snakedoctor101, Jul 2, 2018.

Thread Status:
Not open for further replies.
  1. Hey, I'm trying to make a system that logs phone numbers. I've googled this for about four hours, tried tons of things, and none of them have worked.

    What I want to do:
    I type something to log, it logs it
    Make it so that the next time I type something to log it goes on the line below //Part where I am stuck

    If you want, I'll post code. Thanks for reading.
     
  2. Choco

    Moderator

    Append a newline character, \n, to the end of each String you're writing to file. Or, if the writer you opted to use has the option to do so, println() (similar to System.out.println())
     
  3. I've tried both of those with no progress.

    Here is my class that deals with the file:
    Code (Text):
    static File file;
        public static void createFile() {
            try {
                file = new File("phoneLogs.txt");
               
                if(!file.exists()) {
                    file.createNewFile();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public static void printFile(String message) {
            try {
               
               
                PrintWriter printer = new PrintWriter(file);
               
               
                printer.println(message);
                printer.print("\r\n");;
                printer.flush();
                printer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        @SuppressWarnings({ "resource", "static-access" })
        public static void searchFile(String format) throws FileNotFoundException {
            Boolean found = false;
           
            Scanner scanner = new Scanner(file);
            while(scanner.hasNextLine()) {
                if(scanner.nextLine().equals(format)) {
                    found = true;
                    } else {
                    continue;
                }
            }
            if(found == true) {
                JOptionPane pane = new JOptionPane();
                pane.showMessageDialog(null, "Found a number matching " + format, "Number Search Complete", JOptionPane.INFORMATION_MESSAGE, null);
           
            } else {
                JOptionPane pane = new JOptionPane();
                pane.showMessageDialog(null, "Could not find a number matching " + format, "Number Search Complete", JOptionPane.INFORMATION_MESSAGE, null);
           
            }
        }
       
        public static void openFile() {
            try {
                Desktop.getDesktop().open(file);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
       
        public static void resetLogs() {
            file.delete();
            createFile();
        }
     
  4. This is a piece of code of one of my old plugins, hope it helps you:
    Files.write(Paths.get("document.txt"), (System.lineSeparator() + TEXT).getBytes(), StandardOpenOption.APPEND);
    Just change "document.txt" and "TEXT" and you should be fine.
     
  5. Nope, didn't work, same result.
     
  6. It works for me, can you tell more info about the problem? When you write the text is added in the same first line? It only writes the first time?
    If the problem is that only works for the first line its probably a problem with your server permissions, you have permissions to create but not modify. If the problem is that everything is written in the first line it doesnt make sense, I have a program running with the code I have provided you and it works fine.
     
  7. If I write something, it prints to the first line, erasing what's already there.
    I want it to go to the line below.
     
  8. Choco

    Moderator

    That would be because you're using a PrintWriter. When creating a new PrintWriter instance, it wipes the file clean. I recommend instead using FileWriter
     
  9. For me that just does the same thing.
     
  10. Choco

    Moderator

    Then you're looking at your file in a program incapable of rendering new lines. Notepad comes to mind. I literally cannot think of anything else.
     
  11. I'm using Mac's TextEdit (cuz it came with my computer)

    EDIT: Thank you all for your answers, I solved the problem. New code is below:

    Code (Text):
    try {

                BufferedWriter printer;
                printer = new BufferedWriter(new FileWriter(file, true));

                printer.append(message);
                printer.newLine();
                printer.flush();
                printer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
    #11 snakedoctor101, Jul 2, 2018
    Last edited: Jul 3, 2018
Thread Status:
Not open for further replies.