通过Java编码获取String分行字符串的内容

时间:2021-09-12 23:39:38

代码案列:

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset; import net.sf.json.JSONObject; public class Test { public static void main(String[] args) throws IOException {
String s="11\r\n22\r\n33\r\n\nabd\r\n"; BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(s.getBytes(Charset.forName("utf8"))), Charset.forName("utf8")));
String line;
StringBuffer strbuf=new StringBuffer();
while ( (line = br.readLine()) != null ) {
if(!line.trim().equals("")){
line="<br>"+line;//每行可以做加工
strbuf.append(line+"\r\n");
}
}
System.out.println(strbuf.toString()); } }

结果:

<br>11
<br>22
<br>33
<br>abd