JAVA写文本文件时怎么换行?

bolobolobolobolo 2003-10-09 01:25:03
小弟用“\r\n”表示换行来向一文本文件写入数据,但是该文本文件打开后里面更本没有换行,还是一长串字符,是不是我表示回车的方法不对?究竟用什么表示换行呢?请各位指教!
...全文
2278 17 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
xqi8 2003-11-10
  • 打赏
  • 举报
回复

同意moumouren(某某人)
woxinyongheng 2003-11-10
  • 打赏
  • 举报
回复
RandomAccessFile fw = new RandomAccessFile(filename, "rw");
fw.writeBytes("\r\n");
我这样换行也可以
jndszl 2003-11-09
  • 打赏
  • 举报
回复
上面写的到是不错,能不能改一下,改成追加的
fanter 2003-10-20
  • 打赏
  • 举报
回复
这个问题我刚解决:
FileOutputStream fos = new FileOutputStream("d:\\test.txt");
String nextLine = System.getProperty("line.separator");
String testStr = "abcd"+nextLine+"efgh"+nextLine+"igkl";
byte[] c = testStr.getBytes();
fos.write(c);
fos.close();
bolobolobolobolo 2003-10-10
  • 打赏
  • 举报
回复
是不是getBytes()?也不行啊,写进去的全是乱码。我发现记事本只认ASCII码的回车符,ASC码的回车是什么?
starr0110 2003-10-10
  • 打赏
  • 举报
回复
ASC码的回车是013
city1000 2003-10-10
  • 打赏
  • 举报
回复
File file = new File("c:/aa.txt");
try{
FileOutputStream fout = new FileOutputStream(file);
String str = "aaaaaaa\r\n";
for(int i=0;i<10;i++){
fout.write(str.getByte());
}
fout.close();
}
catch(Exception e){

}

这样写肯定能换行的,给分吧!!!
bolobolobolobolo 2003-10-09
  • 打赏
  • 举报
回复
我也不知道为什么,但是我按照各位的建议做了,但是写好的文件还是只有一行。
zengchangliu 2003-10-09
  • 打赏
  • 举报
回复
String lineSeparator = System.getProperty("line.separator", "\n");
不同的操作系统,换行的代码是不一样的。这是获取系统\n的方法。

再要换行的地方 加个lineSeparator 就好了
wangyanqiu 2003-10-09
  • 打赏
  • 举报
回复


up
ajiao 2003-10-09
  • 打赏
  • 举报
回复
要用格式化输出,这样\r\n才有用
用PrintStream试试
PrintStream out = new PrintStream(
new BufferedOutputStream(
new FileOutputStream(filename,append)));

out.println(msg);
bolobolobolobolo 2003-10-09
  • 打赏
  • 举报
回复
没有用啊,是不是记事本不支持“\r\n”?
用 bw.newLine()也没用啊。
晕了
warren04 2003-10-09
  • 打赏
  • 举报
回复
同意moumouren(某某人)
aku0708 2003-10-09
  • 打赏
  • 举报
回复
是这样吧
text=text.substring(0,index)+"<br>"+text.substring(index+2);
aku0708 2003-10-09
  • 打赏
  • 举报
回复
<%
String text=数据;
int index=text.indexOf("\r\n") ;
while(index!=-1)
{
text=text.substring(0,index)+"\n"+text.substring(index+2);
index=text.indexOf("\r\n") ;
}
%>


moumouren 2003-10-09
  • 打赏
  • 举报
回复
Class BufferedWriter

newLine()
hj12 2003-10-09
  • 打赏
  • 举报
回复
不会吧!你是不是搞错了

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧