JAVA-IO 读取文档

来源:互联网 发布:绝艺 围棋 软件 编辑:程序博客网 时间:2024/06/06 10:49

如何用IO读取文档:

代码奉上:


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;


public class Fetch {


public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub


Fetch fetch = new Fetch();
fetch.fetchio();


}
public void fetchio() throws IOException{
File file = new File("E:/test.txt");
FileInputStream fi = new FileInputStream(file);
byte[] by = new byte[fi.available()];
fi.read(by);
String str = new String(by);
fi.close();
System.out.println(str);
}


}

下面我们来分开解读每条代码:

1.File file =new File (""E:/test.txt"");

       将文件转化为file 可用程序操作

2.FileInputStream fi=new FileInputStream(file);
//将文件转化为 字节流

3.byte[] b=new byte[fi.available()];
//用字节数组接收字节流内容

4.fi.read(b);
//向字节数组里面写入字节流内容  

5.String str =new String(b);
//将字节数组转化为String

6.fi.close();


7.System.out.println(str);

         //打印



0 0