关于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/n/313762.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
JSNYHJSNYH
上一篇 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

发表回复

登录后才能评论