当前位置: 首页 > news >正文

Java代审之常见的文件读取方法

文章目录

  • Files类
  • FileReader
  • ReadBufferedReader
  • ReadCommonsIo
  • ReadScanner
  • ReadRandomAccessFile

Files类

java代码:

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;public class ReadFiles {public static void main(String[] args) throws IOException {String fileName = "C:\\Users\\XXX\\Desktop\\test.txt";//使用Java 7中的Files类处理小文件,获取完整的文件数据readUsingFiles(fileName);}private static void readUsingFiles(String fileName) throws IOException {Path path = Paths.get(fileName);//将文件读取到字节数组byte[] bytes = Files.readAllBytes(path);System.out.println("使用File类读取文件.........");@SuppressWarnings("unused")List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);System.out.println(new String(bytes));}
}

运行截图:
在这里插入图片描述

FileReader

java代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;public class ReadFileReader {public static void main(String[] args) throws IOException {String fileName = "C:\\Users\\xxx\\Desktop\\test.txt";//使用FileReader读取,没有编码支持,效率不高readUsingFileReader(fileName);}private static void readUsingFileReader(String fileName) throws IOException {File file = new File(fileName);FileReader fr = new FileReader(file);BufferedReader br = new BufferedReader(fr);String line;System.out.println("使用FileReader读取文本文件......");while ((line = br.readLine()) != null) {//逐行读取System.out.println(line);}br.close();fr.close();}
}

运行截图:
在这里插入图片描述

ReadBufferedReader

java代码:

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;public class ReadBufferedReader {public static void main(String[] args) throws IOException {String fileName = "C:\\Users\\xxx\\Desktop\\test.txt";//使用BufferedReader读取,逐行读取,并设置编码为UTF_8readUsingBufferedReader(fileName, StandardCharsets.UTF_8);}private static void readUsingBufferedReader(String fileName, Charset cs)throws IOException {File file = new File(fileName);FileInputStream fis = new FileInputStream(file);InputStreamReader isr = new InputStreamReader(fis, cs);BufferedReader br = new BufferedReader(isr);String line;System.out.println("使用BufferedReader读取文本文件......");while ((line = br.readLine()) != null) {//逐行读取System.out.println(line);}br.close();}
}

运行截图:
在这里插入图片描述

ReadCommonsIo

java代码:

import org.apache.commons.io.FileUtils;import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;public class ReadCommonsIo {public static void main(String[] args) throws IOException {String fileName = "C:\\Users\\xxx\\Desktop\\test.txt";readUsingCommonsIo(fileName);}private static void readUsingCommonsIo(String fileName) throws IOException {File file = new File(fileName);System.out.println("使用Commons-io读取文件......");System.out.println(FileUtils.readFileToString(file,StandardCharsets.UTF_8));}
}

maven依赖:

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

运行截图:
在这里插入图片描述

ReadScanner

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;public class ReadScanner {public static void main(String[] args) throws IOException {String fileName = "C:\\Users\\xxx\\Desktop\\test.txt";//使用Scanner类来处理大文件,逐行读取readUsingScanner(fileName);}private static void readUsingScanner(String fileName) throws IOException {Path path = Paths.get(fileName);Scanner scanner = new Scanner(path);System.out.println("使用Scanner读取文本文件.....");//逐行读取while (scanner.hasNextLine()) {//逐行处理String line = scanner.nextLine();System.out.println(line);}scanner.close();}
}

运行截图:
在这里插入图片描述

ReadRandomAccessFile

import java.io.IOException;
import java.io.RandomAccessFile;public class ReadRandomAccessFile {public static void main(String[] args) throws IOException {String fileName = "C:\\Users\\xxx\\Desktop\\test.txt";//使用RandomAccessFile来实现断点续传读取/下载文件readUsingRandomAccessFile(fileName);}private static void readUsingRandomAccessFile(String fileName) throwsIOException {RandomAccessFile file = new RandomAccessFile(fileName, "r");String str;while ((str = file.readLine()) != null) {System.out.println("使用RandomAccessFile来实现断点续传读取/下载文件......");System.out.println(str);}file.close();}
}

运行截图:
在这里插入图片描述
ps:这个方法我想说明一下,通过printIn输出的文本我们可以看出这里他的空白行并不为空,而是\r\n


http://www.mrgr.cn/news/80155.html

相关文章:

  • 方法引用和lambda表达式的奥妙
  • 电感2222
  • #渗透测试#漏洞挖掘#红蓝攻防#护网#sql注入介绍01
  • freeswitch(开启支持MCU视频会议,使用mod_av模块)
  • 设计一个基础JWT的多开发语言分布式电商系统
  • Python课设-谁为影狂-豆瓣数据【数据获取与预处理课设】
  • 前端(五)css属性
  • C++知识整理day5容器——string容器
  • SQL server学习03-创建和管理数据表
  • 【arm】程序跑飞,SWD端口不可用修复(N32G435CBL7)
  • 40 list类 模拟实现
  • C#使用实体类Entity Framework Core操作mysql入门:从数据库反向生成模型2 处理连接字符串
  • 利用ESP-01S中继实现STM32F103C8T6与MQTT服务器的串口双向通信
  • Linux:Git
  • canal安装使用
  • [C++]多态
  • ORB-SLAM3源码学习:G2oTypes.cc: void EdgeInertial::computeError 计算预积分残差
  • 代码随想录算法训练营第一天 | 数组理论基础,977.有序数组平方结果再排序
  • 玛哈特精密矫平机:制造业的隐秘
  • C语言数组和字符串笔记