0

Trying to preserve my string formatting. When I open my .txt file, everything just gets written to one line.

String page = t.toString(); //this is my formatting String
        String fileName = t.getSymbol(); //just helps me name the file (not important)
        File file = new File("C:/Users/user1234212/desktop/folder1" + fileName + ".txt"); //not important here
        file.getParentFile().mkdirs(); // also not important
        FileWriter fw = new FileWriter(file.getAbsoluteFile()); //why does this reformat page ?
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(page);
            bw.close();

RESULT SHOULD BE: title: other info:

Name: joe john

title: person

Summary... Joe john is a person.

Question... no question

Answer...

^ This is the String "page"

xTehOnex
  • 29
  • 1
  • 6

1 Answers1

0

If I understand your issue

you have to use

The java.io.BufferedWriter.newLine(): Writes a line separator. The line separator string is defined by the system property line.separator, and is not necessarily a single newline ('\n') character..

if you want to each content of your data file be written in each line, you must call newLine()

source: http://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html#newLine()

I found this from a post

newLine is environment agnostic \r isn't.

So newLine will give you \r\n on windows but \n on another environment

source: difference between newLine() and carriage return("\r")

Difference between \r and \n

The difference is not Java-specific, but platform specific. Historically UNIX-like OSes have used \n as newline character, some other deprecated OSes have used \r and Windows OSes have employed \r\n.

Source: What are the differences between char literals '\n' and '\r' in Java?

Community
  • 1
  • 1
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
  • Is there another Java writing method that would allow me to write multiple lines with a single String? – xTehOnex Sep 17 '14 at 03:18
  • what do you mean? can you clarify that? this is the only method that gives you the new line – Kick Buttowski Sep 17 '14 at 03:19
  • I have a String that uses "\r" to make multiple lines, and I want to print that String to the .txt file saving the formatting (spacing). – xTehOnex Sep 17 '14 at 03:20
  • @xTehOnex I found this link. take a look at it and lemme know http://stackoverflow.com/questions/3307747/difference-between-newline-and-carriage-return-r – Kick Buttowski Sep 17 '14 at 03:24
  • It seems like it may be easier to use a different method in order to retain my line separated text. Unfortunately I cannot share my entire code for you to look at. – xTehOnex Sep 17 '14 at 03:31