FileReader读取出来的Char数组转换为字符串3种方法

来源:互联网 发布:金星 知乎 编辑:程序博客网 时间:2024/05/17 03:14
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


import org.junit.Test;


public class Test_zifuliu {


@Test
public void test1() {
File file = new File("dbcp.txt");
FileReader fr = null;
try {
fr = new FileReader(file);
char[] cbuf = new char[1024];
int len = 0;
while ((len = fr.read(cbuf)) != -1) {
System.out.print(new String(cbuf));     //将char数组转换为String字符串3种方法

System.out.println(String.valueOf(cbuf));

StringBuffer sb=new StringBuffer();
sb.append(cbuf);
System.out.println(sb);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


}
0 0
原创粉丝点击