/* 获得socket输入流 ,允许每次读取一行 */
br = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
/* 获得文件输出流 ,允许每次写取一行 */
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(savePath)));
/* 接收文件 */
System.out.println("开始接收文件!" + "\n");
String line = null;

while ((line = br.readLine()) != null) {
/*向本地写入文件*/
bw.write(line + "\r\n");
//bw.newLine();
}
bw.flush();以上代码不能向txt文件写入换行,几种方法都试过了,还是不行,我用下面代码获得系统的换行符时,返回的是null,本人系统是win7 64位。请大神们指教,是什么原因?String endLine = System.getProperty("line.separator", "\n"); // 获取换行符
String endLine2 = System.getProperty("line.separator"); // 获取换行符

System.out.println("换行符为" + endLine);
System.out.println("换行符为" + endLine2);

解决方案 »

  1.   

    改为
    bw.write(line);
     bw.write("\r\n");
      

  2.   

    String line="";
    String str="";
    while ((line = br.readLine()) != null) {
                   str+=line+"\n";   
                }
    bw.write(str);
    bw.flush();
    这样试试
      

  3.   

    while ((line = br.readLine()) != null) {             
    bw.write(line + "\r\n");               
    //bw.newLine();            
    }
    把这个 注释去掉 //bw.newLine();            
      

  4.   

    /**
     * 生成.bat文件
     */
    public static File proCmd(String ips) {
     StringBuffer cmd = new StringBuffer();
     Properties pro = null;
    try {
    pro = PropertiesUtils.getProps("scann.properties");
    } catch (Exception e) {
    e.printStackTrace();
    }

     String disk = (String) pro.get("DiskName");
     String path = (String) pro.get("Path");
     cmd.append(disk).append(":").append("\r\n").append("cd ").append(path).append("\r\n");
     cmd.append("Xscan.exe -host "+ips+" -all");
     System.out.println();
     File file = new File(disk+":\\"+path+"\\"+ips+".bat");
     FileWriter filew = null;
     try {
    filew = new FileWriter(file);
    filew.write(cmd.toString());
    } catch (IOException e) {

    e.printStackTrace();
    } finally{
    try {
    filew.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    return file;

    } 我以前写的方法,你自己改下
      

  5.   

    用bw.newLine();就可以的,你试试直接写文件看行不行,再改成从文件读
      

  6.   

    直接用打印流PrintWriterPrintWriter pw=new PrintWriter(new FileWriter(savePath,true),true);
    while((line=br.readLine())!=null){
       pw.println(line);
    }
      

  7.   

    本人知道问题再那里了,原因就是在,socket流就一行,while循环中一次就把流中的所有字符读下来了,所以读到的永远都是一行。可这也不是我想要的结果,大家还有什么好的方法?
      

  8.   

    对写入流的字符串用Base64编码一下就没问题了。
      

  9.   

    public class InputLineToTextFile { /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    File file=new File("D:\\aa.txt");
    FileOutputStream outputStream=new FileOutputStream(file);
    String s="aa\r\nbb";
    byte[]b=s.getBytes();
    outputStream.write(b);

    outputStream.close(); }
      

  10.   

    你是说对接收的到的流进行Base64编码,还是对发出的流进行Base64编码?
      

  11.   

    哈哈哈哈,问题解决了,不要怀疑"\r\n"的换行能力!!!!!!必须在流的发出方(Server)和流的接收方(Client)同时使用bw.write(line + "\r\n")才能将接收的流一行一行的写入本地文件!