2

I have been looking around but could not find what I need. I am writing a simple code to save some strings into a .txt file.

I am using:

File archivo = new File(FileName);
fileOutputStream.write(toString().getBytes());
fileOutputStream.flush();

When I do this the .txt is succesfully created and save the info I need to save, BUT it save all in 1 single huge line. How I can save it in diffent lines?

example when I open the txt:

This is line 1This is line 2This is line 3This is line 4

I have add '\n' at the end of each string but it doesn't work.

toString() return a String: "This is line 1"; OS: Windows 7

3
  • What is the result of the method toString()? Can you show us the code of this method? Commented Sep 2, 2013 at 14:39
  • Are you running on a Windows OS? Commented Sep 2, 2013 at 14:41
  • 1
    Check this out: docs.oracle.com/javase/7/docs/api/java/lang/… Commented Sep 2, 2013 at 14:46

3 Answers 3

4

You could try doing something like this:

public void write(final File file, final List<String> lines) throws IOException{
    final BufferedWriter writer = new BufferedWriter(new FileWriter(file)); //new FileWriter(file, true) if you want to append the file (default is false)
    for(final String line : lines){
        writer.write(line);
        writer.newLine();
    }
    writer.flush();
    writer.close();
}

If you are using Java 8, feel free to try using lambdas:

public void write(final File file, final List<String> lines) throws IOException{
    final BufferedWriter writer = new BufferedWriter(new FileWriter(file)); //new FileWriter(file, true) if you want to append the file (default is false)
    lines.forEach(
            l -> {
                try{
                    writer.write(l);
                    writer.newLine();
                }catch(IOException ex){
                    ex.printStackTrace();
                }
            }
    );
    writer.flush();
    writer.close();
}

For usage, you could try this:

write(new File(fileName), Arrays.asList("This is line 1", "This is line 2", "This is line 3", "This is line 4"));

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

Comments

0

Use System.getProperty("line.separator"), which will return sequence used by operating system to separate lines in text files.

Try like this.

File archivo = new File(FileName);
fileOutputStream.write(toString().getBytes());
fileOutputStream.write(System.getProperty("line.separator").getBytes());
fileOutputStream.flush();

Ref : http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

Comments

-1

I think you should add '\n' to a StringBuilder before write.

StringBuilder sb = new StringBuilder();
sb.append("line 1" + "\n");
sb.append("line 2" + "\n");
sb.append("line 3" + "\n");

fileOutputStream.write(sb.toString().getBytes());

Comments

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.