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 这两个到底是写哪个??? 疯了。

解决方案 »

  1.   

    fos.write((strName+" "+strSex+"\r\n "+strMobilePhone+"\r\n "+strOfficePhone+"\r\n "+strHousePhone+"\r\n "+strAddress+"\\u000a\r\n").getBytes());你这样运行一下程序,看看结果.\r\n是两个分别表示回车和换行的特殊字符.
                    
     
      

  2.   

    是\r\n 
    你想写入文本文件给别人看的话,不要用\n或者newline(),因为记事本这些普通文本编辑器是不认识转义字符的,只能显一个黑方块
      

  3.   


    BufferedWriter bw = new BufferedWriter(new FileWriter("filename"));
    bw.write("test");
    bw.newLine();
      

  4.   


    BufferedWriter bw = new BufferedWriter(new FileWriter("filename"));
    bw.write("test");
    bw.newLine();