为什么此 Java 代码使用不同于 String split \r\n 的 BuffedReader

Why This Java Code Use BuffedReader different from String split \r\n

我将下面的纯文本作为 txt 文件存储在磁盘中

No.     Time           Source                Destination           Protocol Length Info
     93 3.505189000    192.168.1.125         204.79.197.200        HTTP     1160   GET /fd/ls/l?IG=C0F1A7E0A49E484DA6CAC4FA64BE2415&Type=Event.CPT&DATA={%22pp%22:{%22S%22:%22L%22,%22FC%22:12,%22BC%22:416,%22H%22:459,%22BP%22:647,%22CT%22:655,%22IL%22:8},%22ad%22:[-1,-1,1349,640,1349,1759,1]}&P=SERP&DA=Co3b HTTP/1.1

Frame 93: 1160 bytes on wire (9280 bits), 1160 bytes captured (9280 bits) on interface 0
Ethernet II, Src: HonHaiPr_8c:81:48 (e0:06:e6:8c:81:48), Dst: Nintendo_ce:a4:2d (00:22:aa:ce:a4:2d)
Internet Protocol Version 4, Src: 192.168.1.125 (192.168.1.125), Dst: 204.79.197.200 (204.79.197.200)
    Host: cn.bing.com\r\n
    Accept: image/webp,*/*;q=0.8
    User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.154 Safari/537.36 LBBROWSER\r\n
    Referer: http://cn.bing.com/search?q=wireshark%E5%88%86%E6%9E%90%E8%A7%86%E9%A2%91&go=%E6%8F%90%E4%BA%A4&qs=n&form=QBRE&pq=wireshark%E5%88%86%E6%9E%90%E8%A7%86%E9%A2%91&sc=2-13&sp=-1&sk=&cvid=6AAF0CC941DB44F2AFA26B89D6E6ABF6\r\n

当我使用 BuffeRedreader 处理这个文件时,我想去掉 '\r\n' 并将没有 '\r\n' 的新 txt 写入新文件。

像这样的代码

BufferedReader br = new BufferedReader(new FileReader("D:/wiresharkpack/a.txt"));
        String line = "";
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:/wiresharkpack/rlt/a.txt"));
        while ((line=br.readLine())!=null){
            System.out.println(line.replaceAll("\r\n", " "));
            bw.write(line+"\r\n");
            bw.newLine();
        }

但结果是'\r\n'还在。

我试图在 String 中处理这个问题。 像这样

  String lines = "http://b2.bst.126.net/newpage/r/c/c.css?v=1458632076134\r\n123"; 

     System.out.println(lines);
     //String[] arr = Pattern.compile("[\r\n]+|[\n\r]+|[\n]+").split(lines.trim());
     String[] arr = lines.split("\r\n");

     System.out.println();
     for (int i = 0; i < arr.length; i++) {
             System.out.println(arr[i]);
     }

结果是“\r\n”不再出现在结果中。

对于可变字符串,'\r\n'可以转义,而在文件中则不能。好迷茫

line.replaceAll() 创建一个新的字符串,所以 line 没有改变。 你必须做

line = line.replaceAll();

换行。

您可能希望从第一个解决方案中删除这一行。

bw.write(line+"\r\n");

这应该可以解决您的第一个解决方案。

另外,删除这个...

bw.newLine();

来自文档:

换行 public void newLine() 抛出 IOException 写入行分隔符。行分隔符字符串由系统定义 属性 line.separator,不一定是单个换行符 ('\n')。 投掷: IOException - 如果发生 I/O 错误

https://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html#newLine()

您的代码及注释:

// Use try-with-resources
BufferedReader br = new BufferedReader(new FileReader("D:/wiresharkpack/a.txt"));

 // No need to initialize. It's wasteful and misleading.
String line = "";

// Use try-with-resources
BufferedWriter bw = new BufferedWriter(new FileWriter("D:/wiresharkpack/rlt/a.txt"));

while ((line=br.readLine())!=null){

    // readLine() has already removed the \r\n, no need for the replaceAll()
    System.out.println(line.replaceAll("\r\n", " "));

    // You are specifically adding \r\n, so why are you confused to see them in the output?
    bw.write(line+"\r\n");

    // You are specifically writing a newline, so why are you confused to see double in the output?
    bw.newLine();
}

您的代码应该是:

try (BufferedReader br = new BufferedReader(new FileReader("D:/wiresharkpack/a.txt"));
     BufferedWriter bw = new BufferedWriter(new FileWriter("D:/wiresharkpack/rlt/a.txt"))) {
   String line;
   while ((line = br.readLine()) != null) {
      System.out.println(line);
      bw.write(line);
      bw.newLine(); // Remove to get rid of all \r\n in output
   }
}

在我看来,您正在尝试删除字符串 \r\n,它是您输入中的 四个 个字符,而不是 \r\n 这两个字符.

例如

String s = "Host: cn.bing.com\r\n";
String s2 = s2.replaceAll("\\r\\n", "");
System.out.println(s2);

打印

Host: cn.bing.com

简而言之,您有一个 \(反斜杠),然后是 r,然后是 \,然后是 n,而不是 \r(return) 和 \n(新行)个字符。