利用InputStream读取文档内容实例

 1 import java.io.*;
 2 
 3 public class file2{
 4 
 5     public static void main(String[] args)throws Exception{
 6     File f=new File("e:"+File.separator+"test.txt");   
 7     InputStream input=new FileInputStream(f);  //InputStream 创建input对象 并实例化;
 8     byte a[]=new byte[(int)f.length()];   //定义一个Byte数组;[]里面为数组大小由文件确定
 9     int len=input.read(a);     //将内容读到a数组之中,同时返回读入的个数
10     input.close();                  //关闭输入流
11     System.out.println(new String(a));  //将a数组内容强制转化为String类型输出
12     }
13 }

      // 文件需要自己事先创建好,并且有内容

 

******************************开头 import java.io.*  //导入输入(I)/输出(O)的所有包;*******************************************************

******************************读取的文件需要事先创建好、有内容,并且路径保证无误**************************************************

猜你喜欢

转载自www.cnblogs.com/l666/p/9125403.html