本文目錄一覽:
java讀txt方法
1).按行讀取TXT文件
package zc;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class readLine {
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File(“C:/zc.txt”);
BufferedReader reader = null;
String tempString = null;
int line =1;
try {
System.out.println(“以行為單位讀取文件內容,一次讀一整行:”);
reader = new BufferedReader(new FileReader(file));
while ((tempString = reader.readLine()) != null) {
System.out.println(“Line”+ line + “:” +tempString);
line ++ ;
}
reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
2).按位元組讀取TXT文件
package zc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class readerFileByChars {
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File(“c:/zc.txt”);
InputStream in = null;
byte[] tempByte = new byte[1024];
int byteread = 0;
try {
System.out.println(“以位元組為單位讀取文件內容,一次讀多個位元組:”);
in = new FileInputStream(file);
while ((byteread = in.read(tempByte)) != -1 ) {
System.out.write(tempByte, 0, byteread);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
java中怎樣從文件中讀取數據?
分為讀位元組,讀字元兩種讀法\x0d\x0a◎◎◎FileInputStream 位元組輸入流讀文件◎◎◎\x0d\x0apublic class Maintest {\x0d\x0a\x0d\x0apublic static void main(String[] args) throws IOException {\x0d\x0a\x0d\x0aFile f=new File(“G:\\just for fun\\xiangwei.txt”);\x0d\x0a\x0d\x0aFileInputStream fin=new FileInputStream(f);\x0d\x0a\x0d\x0abyte[] bs=new byte[1024];\x0d\x0a\x0d\x0aint count=0;\x0d\x0awhile((count=fin.read(bs))0)\x0d\x0a{\x0d\x0a\x0d\x0aString str=new String(bs,0,count);//反覆定義新變數:每一次都 重新定義新變數,接收新讀取的數據\x0d\x0a\x0d\x0aSystem.out.println(str);//反覆輸出新變數:每一次都 輸出重新定義的新變數\x0d\x0a}\x0d\x0afin.close();\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a◎◎◎FileReader 字元輸入流讀文件◎◎◎\x0d\x0apublic class Maintest {\x0d\x0apublic static void main(String[] args) throws IOException {\x0d\x0a\x0d\x0aFile f=new File(“H:\\just for fun\\xiangwei.txt”);\x0d\x0a\x0d\x0aFileReader fre=new FileReader(f);\x0d\x0a\x0d\x0aBufferedReader bre=new BufferedReader(fre);\x0d\x0a\x0d\x0aString str=””;\x0d\x0awhile((str=bre.readLine())!=null)//●判斷最後一行不存在,為空\x0d\x0a{\x0d\x0aSystem.out.println(str);\x0d\x0a}\x0d\x0abre.close();\x0d\x0a fre.close();\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a}
Java讀取文件的幾種方法
方式一:採用ServletContext讀取,讀取配置文件的realpath,然後通過文件流讀取出來。因為是用ServletContext讀取文件路徑,所以配置文件可以放入在web-info的classes目錄中,也可以在應用層級及web-info的目錄中。文件存放位置具體在eclipse工程中的表現是:可以放在src下面,也可放在web-info及webroot下面等。因為是讀取出路徑後,用文件流進行讀取的,所以可以讀取任意的配置文件包括xml和properties。缺點:不能在servlet外面應用讀取配置信息。
方式二:採用ResourceBundle類讀取配置信息,
優點是:可以以完全限定類名的方式載入資源後,直接的讀取出來,且可以在非Web應用中讀取資源文件。缺點:只能載入類classes下面的資源文件且只能讀取.properties文件。
方式三:採用ClassLoader方式進行讀取配置信息
優點是:可以在非Web應用中讀取配置資源信息,可以讀取任意的資源文件信息
缺點:只能載入類classes下面的資源文件。
方法4 getResouceAsStream
XmlParserHandler.class.getResourceAsStream 與classloader不同
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/272203.html