文件写数据怎么换行?

thatsinteresting 2009-08-11 01:59:02

class MyFile
{
public MyFile()
{}
public void write(String strName,
String strSex,
String strMobilePhone,
String strOfficePhone,
String strHousePhone,
String strAddress)
{
FileOutputStream fos=null;
try
{
fos=new FileOutputStream("message.txt",true);
}
catch(Exception ex)
{
ex.printStackTrace();
}

try{
fos.write((strName+" "+strSex+" "+strMobilePhone+" "+strOfficePhone+" "+strHousePhone+" "+strAddress+"\\u000a").getBytes());

fos.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}


}
public static void main(String[] args)
{
MyFile mf=new MyFile();
mf.write();
}

}


编译的时候总是有错误呀 怎么回事难道换行的转义字符不是 \n \u000a 这两个到底是写哪个??? 疯了。。。。
...全文
164 8 打赏 收藏 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
铁匠梁老师 2009-08-11
  • 打赏
  • 举报
回复

BufferedWriter bw = new BufferedWriter(new FileWriter("filename"));
bw.write("test");
bw.newLine();
铁匠梁老师 2009-08-11
  • 打赏
  • 举报
回复

BufferedWriter bw = new BufferedWriter(new FileWriter("filename"));
bw.write("test");
bw.newLine();
liulu525800 2009-08-11
  • 打赏
  • 举报
回复
是\r\n
你想写入文本文件给别人看的话,不要用\n或者newline(),因为记事本这些普通文本编辑器是不认识转义字符的,只能显一个黑方块
小猪头是猪 2009-08-11
  • 打赏
  • 举报
回复
好像不是那个哦 应该是%n %t
bigbug9002 2009-08-11
  • 打赏
  • 举报
回复
fos.write((strName+" "+strSex+"\r\n "+strMobilePhone+"\r\n "+strOfficePhone+"\r\n "+strHousePhone+"\r\n "+strAddress+"\\u000a\r\n").getBytes());

你这样运行一下程序,看看结果.

\r\n是两个分别表示回车和换行的特殊字符.

flyerwing 2009-08-11
  • 打赏
  • 举报
回复
想换行,写"\r\n"就可以了.
thatsinteresting 2009-08-11
  • 打赏
  • 举报
回复
怎么要写这两个 为什么啊
bigbug9002 2009-08-11
  • 打赏
  • 举报
回复
想换行,写"\r\n"就可以了.

62,620

社区成员

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

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