關於java讀取較大txt文件內容的信息

本文目錄一覽:

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

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStreamReader;

import java.io.Reader;

 

public class H {

    /**

     * 功能: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:\\20121012.txt”;

//      “res/”;

        readTxtFile(filePath);

    }

     

     

 

}

java讀取txt文件

import java.io.File;

public class Test {

    public static void main(String[] args) {

        try {

            File file=new File(“info.txt”);

            new Read().readFile(file);

        }catch (Exception e){

            e.printStackTrace();

        }

    }

}

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.util.ArrayList;

public class Read {

    public void readFile(File file){

        ArrayListString arrayList=new ArrayList();

        try {

            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

            int i=1;

            String line=null;

            String person=””;

            while ((line=bufferedReader.readLine())!=null){

                String[] strings = line.split(“\\s+”);

                for(String s:strings){

                    if(i!=4) {

                        person += s + “,”;

                    }

                    else{

                        person+=s;

                        arrayList.add(person);

                        person=””;

                        i=0;

                    }

                    i++;

                }

            }

            System.out.println(“{“);

            for(i=0;iarrayList.size();i++){

                String s=arrayList.get(i);

                if(i!=arrayList.size()-1)

                  System.out.print(“[“+s+”];”);

                else

                    System.out.print(“[“+s+”]”);

            }

            System.out.println(“}”);

        }catch (Exception e){

            e.printStackTrace();

        }

    }

}

java怎麼讀取txt文件內容

讀取text文件就是用inputStream以及outputStream流進行讀取和寫入:代碼如下: import java.io.BufferedReader; import java.io.FileReader; public class MyFileReader { public static void main(String[] args)throws Exception{ //文件絕對

java可以讀取多大txt文件

如果邊讀取邊處理,無需駐留內存,可以讀取無限大的txt文件。

java怎麼從txt文件中讀取數據

1.package txt;

2.

3.import java.io.BufferedReader;

4.import java.io.File;

5.import java.io.FileInputStream;

6.import java.io.InputStreamReader;

7.

8./**

9. * 讀取TXE數據

10. */

11.public class ReadTxtUtils {

12. public static void main(String arg[]) {

13. try {

14. String encoding = “GBK”; // 字符編碼(可解決中文亂碼問題 )

15. File file = new File(“c:/aa.txt”);

16. if (file.isFile() file.exists()) {

17. InputStreamReader read = new InputStreamReader(

18. new FileInputStream(file), encoding);

19. BufferedReader bufferedReader = new BufferedReader(read);

20. String lineTXT = null;

21. while ((lineTXT = bufferedReader.readLine()) != null) {

22. System.out.println(lineTXT.toString().trim());

23. }

24. read.close();

25. }else{

26. System.out.println(“找不到指定的文件!”);

27. }

28. } catch (Exception e) {

29. System.out.println(“讀取文件內容操作出錯”);

30. e.printStackTrace();

31. }

32. }

33.}

java讀取TXT文件中的數據,每一行就是一個數,返回一個數組,代碼?

?

List list=new ArrayList();

BufferedReader br=new BufferReader(new InputStreamReader(new FileInputStream(new File(“in.txt”))));

String str=null;

while((str=br.readLine())!=null)

{

list.add(new Integer(str));

}

Integer[] i=new Integer[list.size()];

list.toArray(i);

TXT文本中如據形如:

123

456

789

讀入二維數組效果為:

temp[0][]={1,2,3};

temp[1][]={4,5,6};

temp[2][]={7,8,9};

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.*;

public class xx{

public static void main(String[]args){

String s;

int[][]save=new int[3][3];

try{

BufferedReader in =new BufferedReader(new FileReader(“C:\\txt.txt”));

int i=0;

while((s=in.readLine())!=null){

save[i][0]=Integer.parseInt(s.substring(0,1));

save[i][1]=Integer.parseInt(s.substring(1,2));

save[i][2]=Integer.parseInt(s.substring(2,3));

i++;

}

}

catch(FileNotFoundException e){

e.printStackTrace();

}

catch(IOException e){

e.printStackTrace();

}

for(int i=0;i3;i++)

{

for(int j=0;j3;j++){

System.out.print(save[i][j]);

}

System.out.println();

}

}

}

?

BufferedReader bf=new BufferedReader(new FileReader(“Your file”));

String lineContent=null;

int i = 0;

int [][] temp = new int [3][];

while((lineContent=bf.readLine())!=null){

String [] str = lineContent.split(“\\d”);// 將 lineContent 按數字拆分

for(int j = 0; j str.length(); j++){

int [i][j] = Integer.parseInt(str[j]);

}

i++;

}

scp|cs|ff|201101

這是d:\\a.txt的數據,與「|」分割取數據出來,保存在變量a;b;c;d里

import java.io.*;

public class Test{

public static void main(String[] args)throws Exception{

String a, b, c, d;

StringBuffer sb = new StringBuffer();

BufferedReader br = new BufferedReader(new FileReader(“d:\\a.txt”));

String s = br.readLine();

while(s != null){

sb.append(s);

s = br.readLine();

}

s = sb.toString();

String[] str = s.split(“|”);

a = str[0];

b = str[0];

c = str[0];

d = str[0];

}

}

java如何讀取txt文件內容?

通常,可以直接通過文件流來讀取txt文件的內容,但有時可能會出現亂碼!此時只要設置一下文件字符編碼即可。

(1)JAVA 讀取txt文件內容

(2)讀取文件效果:

原創文章,作者:JSNYH,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/313762.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
JSNYH的頭像JSNYH
上一篇 2025-01-07 09:44
下一篇 2025-01-07 09:44

相關推薦

  • vue下載無後綴名的文件被加上後綴.txt,有後綴名的文件下載正常問題的解決

    本文旨在解決vue下載無後綴名的文件被加上後綴.txt,有後綴名的文件下載正常的問題,提供完整的代碼示例供參考。 一、分析問題 首先,需了解vue中下載文件的情況。一般情況下,我們…

    編程 2025-04-29
  • 如何在Java中拼接OBJ格式的文件並生成完整的圖像

    OBJ格式是一種用於表示3D對象的標準格式,通常由一組頂點、面和紋理映射坐標組成。在本文中,我們將討論如何將多個OBJ文件拼接在一起,生成一個完整的3D模型。 一、讀取OBJ文件 …

    編程 2025-04-29
  • Python中讀入csv文件數據的方法用法介紹

    csv是一種常見的數據格式,通常用於存儲小型數據集。Python作為一種廣泛流行的編程語言,內置了許多操作csv文件的庫。本文將從多個方面詳細介紹Python讀入csv文件的方法。…

    編程 2025-04-29
  • 為什麼用cmd運行Java時需要在文件內打開cmd為中心

    在Java開發中,我們經常會使用cmd在命令行窗口運行程序。然而,有時候我們會發現,在運行Java程序時,需要在文件內打開cmd為中心,這讓很多開發者感到疑惑,那麼,為什麼會出現這…

    編程 2025-04-29
  • Python程序文件的拓展

    Python是一門功能豐富、易於學習、可讀性高的編程語言。Python程序文件通常以.py為文件拓展名,被廣泛應用於各種領域,包括Web開發、機器學習、科學計算等。為了更好地發揮P…

    編程 2025-04-29
  • Python zipfile解壓文件亂碼處理

    本文主要介紹如何在Python中使用zipfile進行文件解壓的處理,同時詳細討論在解壓文件時可能出現的亂碼問題的各種解決辦法。 一、zipfile解壓文件亂碼問題的根本原因 在P…

    編程 2025-04-29
  • Python將矩陣存為CSV文件

    CSV文件是一種通用的文件格式,在統計學和計算機科學中非常常見,一些數據分析工具如Microsoft Excel,Google Sheets等都支持讀取CSV文件。Python內置…

    編程 2025-04-29
  • Python如何導入py文件

    Python是一種開源的高級編程語言,因其易學易用和強大的生態系統而備受青睞。Python的import語句可以幫助用戶將一個模塊中的代碼導入到另一個模塊中,從而實現代碼的重用。本…

    編程 2025-04-29
  • Python合併多個相同表頭文件

    對於需要合併多個相同表頭文件的情況,我們可以使用Python來實現快速的合併。 一、讀取CSV文件 使用Python中的csv庫讀取CSV文件。 import csv with o…

    編程 2025-04-29
  • Python寫文件a

    Python語言是一種功能強大、易於學習、通用並且高級編程語言,它具有許多優點,其中之一就是能夠輕鬆地進行文件操作。文件操作在各種編程中都佔有重要的位置,Python作為開發人員常…

    編程 2025-04-29

發表回復

登錄後才能評論