我将以下纯文本存储为磁盘中的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'可以转义,而在文件中则不能转义。如此困惑
答案 0 :(得分:1)
您的评论代码:
// 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
}
}
答案 1 :(得分:0)
line.replaceAll()
创建一个新字符串,因此不会更改line
。
你必须这样做
line = line.replaceAll();
更改行。
答案 2 :(得分:0)
您可能希望从第一个解决方案中删除此行。
bw.write(line+"\\r\\n");
这应该是你第一个解决方案的诀窍。
另外,删除此...
bw.newLine();
来自文档:
换行 public void newLine() 抛出IOException 写一个行分隔符。行分隔符字符串由系统属性line.separator定义,不一定是单个换行符(&#39; \ n&#39;)字符。 抛出: IOException - 如果发生I / O错误
https://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html#newLine()
答案 3 :(得分:0)
在我看来,您正在尝试删除输入中四个字符的字符串\r\n
,而不是\r\n
这两个字符。
e.g。
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
(返回)和{{1 (新行)字符。