import java.io.*;
public class testform {
public static void main(String args[]) {


try {
OutputStreamWriter f=new OutputStreamWriter 
(new FileOutputStream("C:\\Documents and Settings\\Administrator\\桌面\\t5.txt"));
f.write("i am a happy boy i love myself!");
System.out.println(f.getEncoding());
f.close();

OutputStreamWriter m=new OutputStreamWriter 
(new FileOutputStream("C:\\Documents and Settings\\Administrator\\桌面\\t5.txt",true)); 
//m.write("  \n  and you love me too?");

m.close();


} catch (IOException e) {
System.out.println("");
e.printStackTrace();
System.exit(-1);


}

}

}就是注释部分,\t显示是没问题的,为什么换行就不行呢

解决方案 »

  1.   


    我是用\n啊,\t是显示table 结果输入文件的确是table,我就是不明白\n就不能显示换行呢?你有方法的话,直接告诉我怎么改
      

  2.   

    楼主啊,人家三楼都说了,Windows文件里面用\r\n表示换行啊 f.write("i am a happy boy \r\n i love myself!");
      

  3.   


    上面说了用\r\n,如果楼主不光是在windows操作系统中用的话,就用下面这种方法写文件吧:
            PrintStream out = null;
            try {
                File file = new File("C:/test.txt");//可以用\\分隔路径,不过/省事
                out = new PrintStream(file);
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            }
            if (out != null) {
                out.println("i am a happy boy i love myself!");//写入字符串并换行
                out.print("and you love me too?");//写不字符串不换行
                out.println();//无参数只写入一个换行
                out.printf("I am %d years old", 29);//格式化写入
            }
      

  4.   

    PrintStream 还有个好处是除了创建时可能抛出FileNotFoundException异常外,使用过程中不会抛出异常。代码可以写在try块外。然后PrintStream 支持C中的printf,我非常喜欢。