導航:首頁 > 編程語言 > java讀取大文件txt

java讀取大文件txt

發布時間:2022-04-15 19:20:42

A. 如何用java讀取txt文件的內容

import java.io.*; public class Read{ public static void main(String [] args) { try{

FileReader f = new FileReader("D:\\1.txt"); // 注意路徑這里可以有兩種表示方法一個種\\一種/就可以了 BufferedReader buf = new BufferedReader(f);

String s; while((s = buf.readLine() ) != null){ System.out.println(s); } f.close(); buf.close();

}catch(IOException e){} }}

B. java如何讀取txt文件內容

給你兩個方法,你可以看看;
//獲取值返回String文本
publicStringtxt2String(StringfilePath){
Filefile=newFile(filePath);
StringBuilderresult=newStringBuilder();
try{
BufferedReaderbr=newBufferedReader(newFileReader(file));//構造一個BufferedReader類來讀取文件
Strings=null;
while((s=br.readLine())!=null){//使用readLine方法,一次讀一行
result.append(s+System.lineSeparator());
}
br.close();
}catch(Exceptione){
e.printStackTrace();
}
returnresult.toString();
}

//獲取值不返回String文本
publicvoidreadTxtFile(StringfilePath){
try{
Stringencoding="GBK";
Filefile=newFile(filePath);
if(file.isFile()&&file.exists()){//判斷文件是否存在
InputStreamReaderread=newInputStreamReader(
newFileInputStream(file),encoding);//考慮到編碼格式
BufferedReaderbufferedReader=newBufferedReader(read);
StringlineTxt=null;
while((lineTxt=bufferedReader.readLine())!=null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
}catch(Exceptione){
System.out.println("讀取文件內容出錯");
e.printStackTrace();
}
}

C. Java中如何高效的讀取大文件

讀取文件行的標准方式是在內存中讀取,Guava 和Apache Commons IO都提供了如下所示快速讀取文件行的方法:

Files.readLines(new File(path), Charsets.UTF_8); FileUtils.readLines(new File(path));
這種方法帶來的問題是文件的所有行都被存放在內存中,當文件足夠大時很快就會導致程序拋出OutOfMemoryError 異常。

D. java如何讀取大容量的txt文件

java讀取txt文件內容。可以作如下理解:
首先獲得一個文件句柄。File file = new File(); file即為文件句柄。兩人之間連通電話網路了。接下來可以開始打電話了。
通過這條線路讀取甲方的信息:new FileInputStream(file) 目前這個信息已經讀進來內存當中了。接下來需要解讀成乙方可以理解的東西
既然你使用了FileInputStream()。那麼對應的需要使用InputStreamReader()這個方法進行解讀剛才裝進來內存當中的數據
解讀完成後要輸出呀。那當然要轉換成IO可以識別的數據呀。那就需要調用位元組碼讀取的方法BufferedReader()。同時使用bufferedReader()的readline()方法讀取txt文件中的每一行數據哈。

E. java 如何讀取大文件

以下將從常規方法談起,通過對比來說明應該如何使用java讀取大文件。

1、常規:在內存中讀取
讀取文件行的標准方式是在內存中讀取,Guava 和Apache Commons IO都提供了如下所示快速讀取文件行的方法:
Files.readLines(new File(path), Charsets.UTF_8);
FileUtils.readLines(new File(path));
這種方法帶來的問題是文件的所有行都被存放在內存中,當文件足夠大時很快就會導致程序拋出OutOfMemoryError 異常。
例如:讀取一個大約1G的文件:
@Test
public void givenUsingGuava_whenIteratingAFile_thenWorks() throws IOException {
String path = ...
Files.readLines(new File(path), Charsets.UTF_8);
}
這種方式開始時只佔用很少的內存:(大約消耗了0Mb內存)
然而,當文件全部讀到內存中後,我們最後可以看到(大約消耗了2GB內存):
這意味這一過程大約耗費了2.1GB的內存——原因很簡單:現在文件的所有行都被存儲在內存中。
把文件所有的內容都放在內存中很快會耗盡可用內存——不論實際可用內存有多大,這點是顯而易見的。
此外,我們通常不需要把文件的所有行一次性地放入內存中——相反,我們只需要遍歷文件的每一行,然後做相應的處理,處理完之後把它扔掉。所以,這正是我們將要做的——通過行迭代,而不是把所有行都放在內存中。

2、文件流
FileInputStream inputStream = null;
Scanner sc = null;
try {
inputStream = new FileInputStream(path);
sc = new Scanner(inputStream, "UTF-8");
while (sc.hasNextLine()) {
String line = sc.nextLine();
// System.out.println(line);
}
// note that Scanner suppresses exceptions
if (sc.ioException() != null) {
throw sc.ioException();
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (sc != null) {
sc.close();
}
}
這種方案將會遍歷文件中的所有行——允許對每一行進行處理,而不保持對它的引用。總之沒有把它們存放在內存中:(大約消耗了150MB內存)

3、Apache Commons IO流
同樣也可以使用Commons IO庫實現,利用該庫提供的自定義LineIterator:
LineIterator it = FileUtils.lineIterator(theFile, "UTF-8");
try {
while (it.hasNext()) {
String line = it.nextLine();
// do something with line
}
} finally {
LineIterator.closeQuietly(it);
}
由於整個文件不是全部存放在內存中,這也就導致相當保守的內存消耗:(大約消耗了150MB內存)

F. java如何讀取一個txt文件的所有內容

importjava.io.BufferedInputStream;
importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.InputStreamReader;
importjava.io.Reader;


publicclassH{
/**
*功能:Java讀取txt文件的內容
*步驟:1:先獲得文件句柄
*2:獲得文件句柄當做是輸入一個位元組碼流,需要對這個輸入流進行讀取
*3:讀取到輸入流後,需要讀取生成位元組流
*4:一行一行的輸出。readline()。
*備註:需要考慮的是異常情況
*@paramfilePath
*/
publicstaticvoidreadTxtFile(StringfilePath){
try{
Stringencoding="GBK";
Filefile=newFile(filePath);
if(file.isFile()&&file.exists()){//判斷文件是否存在
InputStreamReaderread=newInputStreamReader(
newFileInputStream(file),encoding);//考慮到編碼格式
BufferedReaderbufferedReader=newBufferedReader(read);
StringlineTxt=null;
while((lineTxt=bufferedReader.readLine())!=null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
}catch(Exceptione){
System.out.println("讀取文件內容出錯");
e.printStackTrace();
}

}

publicstaticvoidmain(Stringargv[]){
StringfilePath="L:\20121012.txt";
//"res/";
readTxtFile(filePath);
}}

G. java如何讀取txt文件

讀取txt文件(一整個獲取)


H. java如何讀取txt文件

java讀取txt文件內容。可以作如下理解:

1、首先獲得一個文件句柄。File file = new File(); file即為文件句柄。兩人之間連通電話網路了。接下來可以開始打電話了。

2、通過這條線路讀取甲方的信息:new FileInputStream(file) 目前這個信息已經讀進來內存當中了。接下來需要解讀成乙方可以理解的東西

3、既然你使用了FileInputStream()。那麼對應的需要使用InputStreamReader()這個方法進行解讀剛才裝進來內存當中的數據

4、解讀完成後要輸出呀。那當然要轉換成IO可以識別的數據呀。那就需要調用位元組碼讀取的方法BufferedReader()。同時使用bufferedReader()的readline()方法讀取txt文件中的每一行數據哈。


package com.campu;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
* @author 碼農小江
* H20121012.java
* 2012-10-12下午11:40:21
*/
public class H20121012 {
/**
* 功能:Java讀取txt文件的內容
* 步驟:1:先獲得文件句柄
* 2:獲得文件句柄當做是輸入一個位元組碼流,需要對這個輸入流進行讀取
* 3:讀取到輸入流後,需要讀取生成位元組流
* 4:一行一行的輸出。readline()。
* 備註:需要考慮的是異常情況
* @param filePath
*/
public static void readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判斷文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考慮到編碼格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
System.out.println(lineTxt);
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("讀取文件內容出錯");
e.printStackTrace();
}

}

public static void main(String argv[]){
String filePath = "L:\Apache\htdocs\res\20121012.txt";
// "res/";
readTxtFile(filePath);
}}
閱讀全文

與java讀取大文件txt相關的資料

熱點內容
it165網源碼 瀏覽:397
什麼是資料庫伺服器地址 瀏覽:424
atomicinteger源碼 瀏覽:632
程序員打架活動視頻 瀏覽:224
視頻網站加密原理 瀏覽:206
python圖像區域識別 瀏覽:263
javaweb項目html源碼 瀏覽:463
cad三點畫圓命令 瀏覽:241
安卓手機中英文怎麼設置 瀏覽:880
安卓怎麼做試卷 瀏覽:887
xp系統怎麼做域伺服器 瀏覽:304
九游游戲app叫什麼 瀏覽:624
無壓縮機除濕機 瀏覽:335
柱為什麼加密區 瀏覽:621
結晶機pdf 瀏覽:111
bim中刪除命令快捷方式 瀏覽:981
php生成一個文件 瀏覽:420
c運行php 瀏覽:71
為什麼我的世界搜索不到伺服器 瀏覽:99
幫忙拍牌照是什麼APP 瀏覽:934