将本地的code.txt文件内容存放到StringBuffer变量中:
}
private static final String txtFile="F:/txtFile/code.txt";
BufferedReader br;
StringBuffer sb;
@Test
public void f() throwsIOException {
try{
br=newBufferedReader(new InputStreamReader(newFileInputStream(txtFile),"GBK"));
sb=newStringBuffer();
Stringstr="";
while((str=br.readLine())!=null)
{
sb.append(str);
System.out.println(str);
}
}catch(Exceptione){
System.out.println("获取文本出错");
}finally {
br.close();
}
}
执行结果:
code:第一行
code:第二行