我想实现这样的功能
分别使用OutputStreamWriter、PrintWriter和BufferedWriter类将三行字符串写入到文本文件中。
我想输入到文本中的字符串应该是三行  import java.io.*;public class WriterDemo 
{
public static void main(String[] args) 
{
OutputStreamWriter osw=null;
PrintWriter pw=null;
BufferedWriter bw=null;
try{
osw=new OutputStreamWriter(new FileOutputStream("c:/write.txt",true),"GBK");
pw=new PrintWriter("c:/write.txt","GBK");
bw=new BufferedWriter(new FileWriter("c:/write.txt",true));
String str="9. 分别使用OutputStreamWriter、PrintWriter和BufferedWriter";
String str2="类将三行字符串写入到文本文件中,";
String str3="仔细分析有何差别"; osw.write(str);
osw.write(str2);
osw.write(str3); pw.write(str);
pw.write(str2);
pw.write(str3);
bw.write(str);
bw.newLine();
bw.write(str2);
bw.newLine();
bw.write(str3);
}
catch(UnsupportedEncodingException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
finally{
if(osw!=null)
try{
osw.close();
}catch(IOException e){} if(pw!=null)
pw.close(); if(bw!=null)
try{
bw.close();
}catch(IOException e){}
}
}
}为什么现在输出是这样的效果呢 他不自动换行啊 {9. 分别使用OutputStreamWriter、PrintWriter和BufferedWriter类将三行字符串写入到文本文件中,仔细分析有何差别9. 分别使用OutputStreamWriter、PrintWriter和BufferedWriter}(是在一行上的 )
类将三行字符串写入到文本文件中,
仔细分析有何差别

解决方案 »

  1.   

    自己输出 '\n'啊。Windows  \r\n
    Linux    \n
    Mac      \r
      

  2.   

    你写的程序这么小周末能判断出来差别啊,buffer本来就是来处理流的,你的就是几个简单的字符怎么可以啊
      

  3.   

    osw.write(str+“\n”);
    pw.println(str)
    bw.write(str)
    bw.newLine();
      

  4.   

    PrintWriter 的println(String str)可以换行
      

  5.   


    import java.io.*;public class WriterDemo 
    {
        public static void main(String[] args) 
        {
            OutputStreamWriter osw=null;
            PrintWriter pw=null;
            BufferedWriter bw=null;
            try{
                osw=new OutputStreamWriter(new FileOutputStream("c:/write.txt",true),"GBK");
                pw=new PrintWriter("c:/write.txt","GBK");
                bw=new BufferedWriter(new FileWriter("c:/write.txt",true));
                String str="9.    分别使用OutputStreamWriter、PrintWriter和BufferedWriter\r\n";
                String str2="类将三行字符串写入到文本文件中,\r\n";
                String str3="仔细分析有何差别\r\n";            osw.write(str);
                osw.write(str2);
                osw.write(str3);
        osw.flush();            pw.write(str);
                pw.write(str2);
                pw.write(str3);
        pw.flush();     bw.newLine();
                bw.write(str);
                bw.write(str2);
                bw.write(str3);
        bw.flush();
            }
            catch(UnsupportedEncodingException e){
                e.printStackTrace();
            }
            catch(IOException e){
                e.printStackTrace();
            }
            finally{
                if(osw!=null)
                try{
                    osw.close();
                }catch(IOException e){}            if(pw!=null)
                    pw.close();            if(bw!=null)
                try{
                    bw.close();
                }catch(IOException e){}
            }
        }
    }
    //运行结果:
    //9.    分别使用OutputStreamWriter、PrintWriter和BufferedWriter
    //类将三行字符串写入到文本文件中,
    //仔细分析有何差别//9.    分别使用OutputStreamWriter、PrintWriter和BufferedWriter
    //类将三行字符串写入到文本文件中,
    //仔细分析有何差别
    String str="9.    分别使用OutputStreamWriter、PrintWriter和BufferedWriter\r\n";
                String str2="类将三行字符串写入到文本文件中,\r\n";
                String str3="仔细分析有何差别\r\n";/r/n顺序不可逆,切记!
        OK解决问题!