String str[] = {"1000458551||A210171046D86F9F6EE21B66FE9B1441E20EC1DEF9654A2D092162591C01D26F||1||7707||0||"
+ "0||1002||1373569142000||HTC One||val||4.1.2||0||1.01.20130206.15441^^1000458551||A210171046D86F9F6EE21B66FE9B1441E20EC1DEF9654A2D092162591C01D26F||"
+ "1||7707||0||0||1002||1373569142000||HTC One||val||4.1.2||0||1.01.20130206.15441","1000458551||A210171046D86F9F6EE21B66FE9B1441E20EC1DEF9654A2D092162591C01D26F||1||7707||0||"
+ "0||1002||1373569142000||HTC One||val||4.1.2||0||1.01.20130206.15441^^1000458551||A210171046D86F9F6EE21B66FE9B1441E20EC1DEF9654A2D092162591C01D26F||"
+ "1||7707||0||0||1002||1373569142000||HTC One||val||4.1.2||0||1.01.20130206.15441"};
ByteArrayOutputStream baos = new ByteArrayOutputStream();
for(String p:str){
String Recordstore[] = p.split("\\^\\^");
long len = Recordstore.length;
long counter = 0;
StringBuffer finalRecord = new StringBuffer();
for (String rec : Recordstore) {
rec = rec.replaceAll("\\|\\|", "|");
if (counter != len - 1)
finalRecord.append(rec).append(System.lineSeparator());
else
finalRecord.append(rec);
counter++;
}
baos.write(finalRecord.toString().getBytes());
}
ByteArrayInputStream object = new ByteArrayInputStream(
baos.toByteArray());
String pr="";
for(int y = 0 ; y < 1; y++ ) {
while(( c= object.read())!= -1) {
pr+=(char)c;
}
System.out.println(pr);
object.reset();
}
将字符串转换为字节并重新检查字节后,我看到新行字符丢失,字符串组合在一行中。 如何在转换为字节后保留新行字符?
示例输出是: 将新行添加到字符串并打印字符串给出:
1000458551 | A210171046D86F9F6EE21B66FE9B1441E20EC1DEF9654A2D092162591C01D26F | 1 | 7707 | 0 | 0 | 1002 | 1373569142000 | HTC One | val | 4.1.2 | 0 | 1.01.20130206.15441 1000458551 | A210171046D86F9F6EE21B66FE9B1441E20EC1DEF9654A2D092162591C01D26F | 1 | 7707 | 0 | 0 | 1002 | 1373569142000 | HTC One | val | 4.1.2 | 0 | 1.01.20130206.15441
将其转换为字节并打印字符串
1000458551 | A210171046D86F9F6EE21B66FE9B1441E20EC1DEF9654A2D092162591C01D26F | 1 | 7707 | 0 | 0 | 1002 | 1373569142000 | HTC一个| VAL | 4.1.2 | 0 | 1.01.20130206.154411000458551 | A210171046D86F9F6EE21B66FE9B1441E20EC1DEF9654A2D092162591C01D26F | 1 | 7707 | 0 | 0 | 1002 | 1373569142000 | HTC一| VAL | 4.1.2 | 0 | 1.01.20130206.15441
感谢您提前回复
答案 0 :(得分:0)
因为这条线永远不会被执行。
finalRecord.append(rec).append(System.lineSeparator());
我不确定你为什么要像这样拆分字符串:
String Recordstore[] = p.split("\\^\\^");
显然,数组Recordstore.length()
将始终为1,因为原始字符串中没有^^
。
所以counter != len - 1
始终为false
。
更新
我在这一行做了一些改动:
baos.write(finalRecord.append(System.lineSeparator()).toString().getBytes());