码迷,mamicode.com
首页 > 编程语言 > 详细

java读取本地text文件

时间:2016-04-12 19:35:01      阅读:163      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!