So when I write to my text file using FileOutputSstream
I have used both
fos.write(("0"+"\n").getBytes());
and
fos.write(System.getProperty("line.separator").getBytes());
and neither of them work... they still print on the same line...
Any thoughts?
Ok so I figured it out... for some reason when I tried every example here the only thing that worked was ps.print("one\\r\\n"); Its really strange since I have know idea why it works but it does...
use a PrintStream
System.out is a PrintStream for example.
You say you have a FileOutputStream. Then,
FileOutputStream f = new FileOutputStream("test.txt");
PrintStream ps = new PrintStream(f);
ps.print("line1");
ps.println(); //this writes your new line
ps.print("line2");
ps.close();
also you can use;
BufferedWriter fos = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)));
fos.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
fos.newLine();
Newlines are different in each platform. For unix systems (MacOS X, Linux, ... and Android, since it uses a modified linux kernel), the line-separator is 0x0A
; for MS Windows, it its two bytes long: 0x0D,0x0A
; for old-school MacOS, it is 0x0D
.
This means that an Android-newline written by Android will be invisible when opened by a particularly stupid Windows program such as Notepad.exe. A wiser program (such as a programming editor or an IDE) will display it without trouble.
You can use println method
PrintStream ps=new PrintStream(new FileOutputStream("outputPath"));
for(String line:lines)
ps.println(line);
Use this:
public byte[] newLine() {
byte[] temp = new byte[2];
temp[0] = 13;
temp[1] = 10;
return temp;
}
Just write this to your FileOuputStream. Like this:
FileOutputStream.write(newLine());
This will simply write the bytes: 13 and 10.
You have to append the line seperator / break like this.
Try
String separator = System.getProperty("line.separator");
fos.append(seperator);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.