程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA編程入門知識 >> java讀取本地text文件

java讀取本地text文件

編輯:JAVA編程入門知識

//按字節讀取

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) {
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved