首页 > 编程语言 > 详细

java读取本地text文件

时间:2016-04-12 19:33:59      阅读:170      评论:0      收藏:0      [点我收藏+]

//按字节读取

public static void readByBytes(String url) {
  File file = new File(url);
  InputStream in = null;
  try {
    in = new FileInputStream(file);
    int temp;
    while ((temp = in.read()) != -1) {
      System.out.println(temp);
    }
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}

//按字符读取

public static void readByChar(String url){
  File file = new File(url);
  Reader read = null;
  StringBuffer buff = new StringBuffer();
  char[] arr = new char[1024];
  int cha=0 ;
  try {
    read =new InputStreamReader(new FileInputStream(file),"utf-8");
    while((cha=read.read(arr))!=-1){
      buff.append(new String(arr,0,cha));
    }
    System.out.println(buff.toString());
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}

//按行读取

public static void readByLine(String url){
  File file = new File(url);
  try {
    InputStreamReader in = new InputStreamReader(new FileInputStream(file),"UTF-8");
    BufferedReader buff = new BufferedReader(in);
    String text =null;
    while((text = buff.readLine())!=null){
      System.out.println(text);
    }
    } catch (FileNotFoundException e) {
  } catch (UnsupportedEncodingException e) {
  } catch (IOException e) {
  }
}

java读取本地text文件

原文:http://www.cnblogs.com/lansetuerqi/p/5383794.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!