0

So im not sure if there are other questions like this, but from what i have sen none of them seem to help me.

so I have a string that say has the words "hello","good day", and "good bye" and i have it declared like this

String content="hello\n good day\n good bye";

Now I would like to make a txt file in the project sub directory under a folder called "output", so its directory is user/project1/output

the thing is I would like this txt file to print each word on a separate line, I have the following code so far that does make a txt file with content string, but everything is on the same line

File file = new File("output1.txt");
FileOutputStream outputStream = new FileOutputStream(file, false);
PrintWriter out = new PrintWriter(outputStream, true);
out.println(content);

and the output looks like this

hello good day food bye

how can I have it so the txt file looks like this

hello
good day
good bye
Zong
  • 6,160
  • 5
  • 32
  • 46
nafis
  • 5
  • 2
  • Basically, different OS use different line break symbols. You could split your string on `\n`, then use your PrintWriter's `println()` (notice the *ln* for *line*) methods to write each element as a line to the file. It will append the correct line ending for you. – domsson Apr 16 '17 at 22:53
  • ah thanks, I believe I have figured it out by just calling out.println 3 times, each for the different parts – nafis Apr 16 '17 at 22:58

2 Answers2

0

Different platforms use different line endings, but you can use a regular expression to split the content by \n, and if you have spaces after the newline you can match them too. Also, you haven't shown how you close your PrintWriter - I would prefer a try-with-resources. Like,

String content="hello\n good day\n good bye";
File file = new File("output1.txt");
try (PrintWriter out = new PrintWriter(new FileOutputStream(
        file, false), true)) {
    for (String line : content.split("\\n\\s*")) {
        out.println(line);
    }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I now what \n means, but what does \\n\\s* mean? – nafis Apr 16 '17 at 23:03
  • guess new line and space the extra back slash \ is to let some characters to be accepted inside double qoute – Maytham Fahmi Apr 16 '17 at 23:37
  • Sort of. In a regular expression, ``\`` indicates a special character. As it does in a Java `String`. So it has to be escaped. And the escape is another ``\\``. So `\n` becomes `\\n`. As for `\\s*`, that matches (optional) white space after the newline. Otherwise, your second and third lines would start with a space. – Elliott Frisch Apr 16 '17 at 23:48
0

Change String content to this.

String content = "hello" + System.lineSeparator() + "good day" + System.lineSeparator() + "good bye";

edit - this code works for me:

public static void main(String[] args) throws FileNotFoundException {
    String content = "hello" + System.lineSeparator() + "good day" + System.lineSeparator() + "good bye";
    File file = new File("output1.txt");
    FileOutputStream outputStream = new FileOutputStream(file, false);
    PrintWriter out = new PrintWriter(outputStream, true);
    out.println(content);
}
Moe
  • 91
  • 2
  • 10