Java读取文件方式

news/2024/7/1 2:55:00

IO流读取

文本内容

 

按行读取文件内容

指定编码格式(推荐)

 public static void main(String[] args) throws UnsupportedEncodingException {
        read("D:\\test.txt");

    }

    public static void read(String path) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(
                    new InputStreamReader(new FileInputStream(path), "UTF-8"));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

 注意,如果控制台输出为乱码,需要度idea进行配置,依次选择file-Setting-editor-File Encoding

 不指定编码格式

 public static void main(String[] args) throws IOException {
        read("D:\\test.txt");

    }

    public static void read(String path) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(path));
        String line;
        while ((line = reader.readLine()) != null)
        {
            System.out.println(line);
        }
        reader.close();
    }

可能出现乱码。

按字符读取文件内容

 public static void read(String path) throws IOException {
        Reader reader = new InputStreamReader(new FileInputStream(path));
        int tempchar;
        while ((tempchar = reader.read()) != -1) {
            System.out.print((char) tempchar);
        }
        reader.close();
    }

按字节读取文件内容

常用于读取图片,声音,影像等

单字节读取

 public static void read(String path) throws IOException {
        FileInputStream reader = new FileInputStream(path);
        int tempchar;
        while ((tempchar = reader.read()) != -1) {
            System.out.print((char) tempchar);
        }
        reader.close();
    }

多字节读取

 public static void read(String path) throws IOException {
        FileInputStream reader = new FileInputStream(path);
        byte[] tempByte = new byte[1024];
        int len = 0 ;
        while((len = reader.read(tempByte))!= -1){
            for (int i = 0; i <len; i++) {
                System.out.print((char)tempByte[i]);
            }
        }
        reader.close();
    }

Scanner

第一种方式是Scanner,从JDK1.5开始提供的API,特点是可以按行读取、按分割符去读取文件数据,既可以读取String类型,也可以读取Int类型、Long类型等基础数据类型的数据。

  public static void read(String path) throws IOException {
        try (Scanner sc = new Scanner(new FileReader(path))) {
            while (sc.hasNextLine()) {  //按行读取字符串
                String line = sc.nextLine();
                System.out.println(line);
            }
        }
    }

JDK1.7提供的NIO读取文件

小文件

 public static void read(String path) throws IOException {
        final String CHARSET_NAME = "UTF-8";

        List<String> content = new ArrayList<>(0);

        try {
            content = Files.readAllLines(Paths.get(path), Charset.forName(CHARSET_NAME));
        } catch (Exception e) {
            e.printStackTrace();
        }

       content.forEach(System.out::println);

    }

大文件

 public static void read(String path) throws IOException {
        final String CHARSET_NAME = "UTF-8";

        List<String> content = new ArrayList<>(0);


        try (BufferedReader br = Files.newBufferedReader(Paths.get(path), Charset.forName(CHARSET_NAME))) {
            String line;
            while ((line = br.readLine()) != null) {
                content.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

       content.forEach(System.out::println);

    }

JDK1.4提供的NIO读取文件(适用于超大文件)

public static void read(String path) throws IOException {
        final String CHARSET_NAME = "UTF-8";

        final int ASCII_LF = 10; // 换行符
        final int ASCII_CR = 13; // 回车符

        List<String> content = new ArrayList<>();

        try (FileChannel fileChannel = new RandomAccessFile(path, "r").getChannel()) {
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 100);
            byte[] lineByte;

            byte[] temp = new byte[0];

            while (fileChannel.read(byteBuffer) != -1) {
                // 获取缓冲区位置,即读取长度
                int readSize = byteBuffer.position();

                // 将读取位置置0,并将读取位置标为废弃
                byteBuffer.rewind();

                // 读取内容
                byte[] readByte = new byte[readSize];
                byteBuffer.get(readByte);

                // 清除缓存区
                byteBuffer.clear();

                // 读取内容是否包含一整行
                boolean hasLF = false;

                int startNum = 0;

                for (int i = 0; i < readSize; i++) {
                    if (readByte[i] == ASCII_LF) {
                        hasLF = true;
                        int tempNum = temp.length;
                        int lineNum = i - startNum;

                        // 数组大小已经去掉换行符
                        lineByte = new byte[tempNum + lineNum];
                        System.arraycopy(temp, 0, lineByte, 0, tempNum);
                        temp = new byte[0];
                        System.arraycopy(readByte, startNum, lineByte, tempNum, lineNum);

                        String line = new String(lineByte, 0, lineByte.length, CHARSET_NAME);

                        content.add(line);

                        // 过滤回车符和换行符
                        if (i + 1 < readSize && readByte[i + 1] == ASCII_CR) {
                            startNum = i + 2;
                        } else {
                            startNum = i + 1;
                        }
                    }
                }
                if (hasLF) {
                    temp = new byte[readByte.length - startNum];
                    System.arraycopy(readByte, startNum, temp, 0, temp.length);
                } else {
                    // 单次读取的内容不足一行的情况
                    byte[] toTemp = new byte[temp.length + readByte.length];
                    System.arraycopy(temp, 0, toTemp, 0, temp.length);
                    System.arraycopy(readByte, 0, toTemp, temp.length, readByte.length);
                    temp = toTemp;
                }
            }

            // 最后一行
            if (temp.length > 0) {
                String lastLine = new String(temp, 0, temp.length, CHARSET_NAME);

                content.add(lastLine);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
       content.forEach(System.out::println);

    }

 JDK1.7 Files.readAllBytes

 public static void read(String path) throws IOException {

        byte[] bytes = Files.readAllBytes(Paths.get(path));

        String content = new String(bytes, StandardCharsets.UTF_8);
        System.out.println(content);
    }

 JDK1.8 Stream流

可能会出现内存溢出问题 java.lang.OutOfMemoryError

Files.lines

public static void read(String path) throws IOException {
        // 读取文件内容到Stream流中,按行读取
        Stream<String> lines = Files.lines(Paths.get(path));

        // 随机行顺序进行数据处理
        lines.forEach(System.out::println);
    }

forEach获取Stream流中的行数据不能保证顺序,但速度快。如果你想按顺序去处理文件中的行数据,可以使用forEachOrdered,但处理效率会下降。


lines.forEachOrdered(System.out::println);

或者利用CPU多和的能力,进行数据的并行处理parallel(),适合比较大的文件


lines.parallel().forEachOrdered(System.out::println);

Files.readAllLines

public static void read(String path) throws IOException {
    // 读取文件内容到Stream流中,按行读取
    List<String> lines =  Files.readAllLines(Paths.get(path),
            StandardCharsets.UTF_8);

    // 随机行顺序进行数据处理
    lines.forEach(System.out::println);
}

 JDK 11 Files.readString()

文件不能超过2G,同时要注意你的服务器及JVM内存。这种方法适合快速读取小文本文件。

     System.out.println(Files.readString(Paths.get(path))); 

依赖hutool

IoUtil工具类

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-core</artifactId>
    <version>5.8.10</version>
</dependency>

或者:
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.10</version>
</dependency>
 public static void read(String path) throws IOException {
        final String CHARSET_NAME = "UTF-8";


        List<String> content = new ArrayList<>();

        try {
            IoUtil.readLines(new FileInputStream(path), CharsetUtil.charset(CHARSET_NAME), content);
        } catch (Exception e) {
            e.printStackTrace();
        }
       content.forEach(System.out::println);

    }

 FileUtil工具类

public static void fileOfHutool() {
    
        final String CHARSET_NAME = "UTF-8";

        List<String> content = FileUtil.readLines(path, CHARSET_NAME);

        content.forEach(System.out::println);
      
    }

依赖cmmons-io

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.11.0</version>
</dependency>

FileUtils工具类

    public static void read(String path) throws IOException {
        final String CHARSET_NAME = "UTF-8";
        List<String> content = new ArrayList<>(0);

        try {
            content = FileUtils.readLines(new File(fileName), CHARSET_NAME);
        } catch (Exception e) {
            e.printStackTrace();
        }
       content.forEach(System.out::println);

    }

IOUtils工具类

public static void read(String path) throws IOException {
        final String CHARSET_NAME = "UTF-8";
        List<String> content = new ArrayList<>(0);
        try {
            content = IOUtils.readLines(new FileInputStream(fileName), CHARSET_NAME);
        } catch (Exception e) {
            e.printStackTrace();
        }
       content.forEach(System.out::println);

    }


http://lihuaxi.xjx100.cn/news/1052864.html

相关文章

2023年陕西中级工程师职称怎么评?

夏季开始了&#xff0c;又到了职称评审正式申报提交资料的时候了&#xff0c;很多想评职称的小伙伴都不知道中级职称要怎么评&#xff1f;需要本人提供什么资料&#xff1f;配合哪些事情&#xff1f;怎么评一个中级职称呢&#xff1f;接下来小周跟您一起来看看&#xff1a; 想要…

NumberPicker分析(二)

NumberPicker分析(二) NumberPicker继承自LinearLayout。一般而言&#xff0c;无论是继承自View&#xff0c;还是继承自ViewGroup&#xff0c;必然会经过如下的几个阶段&#xff1a; onMeasureonLayoutonDraw onMeasure 在onMeasure方法测量当前控件大小&#xff0c;为正式…

K8s集群搭建-Kubeadm方式搭建集群【1.23.0版本】

文章目录 一、初始化准备二、安装kubeadm三、初始化Master集群四、将新的Node节点加入集群五、部署CNI网络插件六、其他配置 Kubernetes1.24(包括1.24)之后不在兼容docker,如果有需要兼容docker的需求&#xff0c;则安装一个 cri-docker的插件&#xff0c;本文使用的是kuberne…

混淆电路(GC)

基本概念 在混淆电路框架下&#xff0c;任意功能函数可被表示为一个与门和异或门组成的布尔电路&#xff0c;协议的参与方由生成方&#xff08;Garbler&#xff09;和计算方&#xff08;Evaluator&#xff09;组成。 **大致的流程&#xff1a;**生成方生成密钥并加密查找表&am…

codemirror 5前端代码编辑器资料整理。

CodeMirror 是基于js的源代码编辑器组件&#xff0c;它支持javascript等多种高级语言&#xff0c;tampermonkey内置的代码编辑器就是基于它。它的按键组合方式兼容vim&#xff0c;emacs等&#xff0c;调用者还可自定义”自动完成“的列表窗口&#xff0c;自由度极高&#xff0c…

【测试开发】第一节.测开入门(附常考面试题)

文章目录 前言 一、什么是测试开发 1.1 常考面试题 二、软件测试的基础概念 2.1 需求 2.2 测试用例 3、BUG 三、生命周期 3.1 软件的生命周期 3.2 软件测试的生命周期 四、软件工程中的几种常见的开发模型 4.1 瀑布模型 4.2 螺旋模型 4.3 增量模型和迭代模型 4.4 敏捷…

基于matlab评估机场监控雷达上 5G 新无线电 (NR) 信号的干扰

一、前言 随着5G NR系统的频率范围超出LTE中使用的频段&#xff0c;频谱管理变得更加复杂。对扩大5G覆盖范围的需求是由更高的数据速率和更低的延迟的好处推动的。新5G基站的实施反过来又推动了了解这些信号如何影响在相同频段上运行的已安装系统的需求。其中一个系统是空中交通…

无线洗地机哪款性价比高?高性价比的洗地机分享

虽说现在市面上清洁工具很多&#xff0c;但是要说清洁效果最好的&#xff0c;肯定非洗地机莫属。它集合了吸&#xff0c;洗&#xff0c;拖三大功能&#xff0c;干湿垃圾一次清理&#xff0c;还能根据地面的脏污程度进行清洁&#xff0c;达到极致的清洁效果&#xff0c;省时省力…