读取文件demo1

呆在家没事 就看看基础的东西,写了个读取文件的demo代码下面,希望可以帮助大家

 

package test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class ReadDoc {

 public static void main(String []args) {
  
  String filename = "F:\\aa.txt";//当这里文件是什么编码下面的 31 行的 string编码应该对应的是什么编码
  File file = new File (filename);
  
  byte [] buffer1 = new byte[10000000];//静态数组的缺陷是不能扩展,这样不知道文件的大小就不能够完全读取
  Date startDate = null;
//     BufferedInputStream read = null;
     InputStream read = null;//InputStream是抽象类,需要他的子类去实现他,下面FileInputStream是他的子类对他进行实现
     try {
       startDate = new Date();
//      read = new BufferedInputStream(new FileInputStream(filename));
      read = new FileInputStream(file);
     } catch (FileNotFoundException e1) {
      e1.printStackTrace();
     }
     try {
     int b = read.read(buffer1);
     String str = new String(buffer1,0,b,"utf-8");//这里这能设置gbk如果文件是ascii 如果文件是utf-8则应该是utf-8
     Date endTime = new Date();
     
     System.out.println(str);
     System.out.println(endTime.getTime()-startDate.getTime()+"ms");
     
     } catch (IOException e) {
      e.printStackTrace();
     }
     finally{
      
      try{
       
       read.close();
      }catch(Exception e){
       
       System.out.println("关闭输入流错误!");
       
      }
      
     }
  
 }
 
}

 

你可能感兴趣的:(java,F#)