I want to write some texts in a new line into an existing file.I tried following code开发者_如何学C but failed,Can any one suggest how can I append to file in a new row.
private void writeIntoFile1(String str) {
try {
fc=(FileConnection) Connector.open("file:///SDCard/SpeedScence/MaillLog.txt");
OutputStream os = fc.openOutputStream(fc.fileSize());
os.write(str.getBytes());
os.close();
fc.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and calling
writeIntoFile1("aaaaaaaaa");
writeIntoFile1("bbbbbb");
Its successfully writing to the file I simulated(SDCard) but its appears in the same line. How can I write "bbbbbb" to new line?
Write a newline (\n
) after writing the string.
private void writeIntoFile1(String str) {
try {
fc = (FileConnection) Connector.open("file:///SDCard/SpeedScence/MaillLog.txt");
OutputStream os = fc.openOutputStream(fc.fileSize());
os.write(str.getBytes());
os.write("\n".getBytes());
os.close();
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
N.B. a PrintStream
is generally better-suited for printing text, though I'm not familiar enough with the BlackBerry API to know if it's possible to use a PrintStream
at all. With a PrintStream
you'd just use println()
:
private void writeIntoFile1(String str) {
try {
fc = (FileConnection) Connector.open("file:///SDCard/SpeedScence/MaillLog.txt");
PrintStream ps = new PrintStream(fc.openOutputStream(fc.fileSize()));
ps.println(str.getBytes());
ps.close();
fc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
精彩评论