java中read()方法的使用

来源:百度知道 编辑:UC知道 时间:2024/09/23 05:03:56
用read()方法如何读完指定整个文件,read()方法的参数必须是数组吗

Java从文件中读取内容使用read方法,如下:

//测试文件流的写入读出
public void fileTest()
{
try {
FileOutputStream out2 = new FileOutputStream("hello.txt");

out2.write("a".getBytes());
out2.write("b".getBytes());
out2.write("c".getBytes());
out2.write("d".getBytes());
out2.write("e".getBytes());
out2.close();

File file = new File("hello.txt");
FileInputStream in = new FileInputStream(file);

byte[] buf = new byte[12];
int len1 = in.read(buf,0,3);
int len2 = in.read(buf,0,4);
in.close();

System.out.println(buf.toString());
System.out.println("len1="+len1+"len2="+len2);
Sys