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

java 解析der文件_[Java]读取文件方法大全

public class ReadFromFile {

/**

* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。

*/

public static void readFileByBytes(String fileName) {

File file = new File(fileName);

InputStream in = null;

try {

System.out.println("以字节为单位读取文件内容,一次读一个字节:");

// 一次读一个字节

in = new FileInputStream(file);

int tempbyte;

while ((tempbyte = in.read()) != -1) {

System.out.write(tempbyte);

}

in.close();

} catch (IOException e) {

e.printStackTrace();

return;

}

try {

System.out.println("以字节为单位读取文件内容,一次读多个字节:");

// 一次读多个字节

byte[] tempbytes = new byte[100];

int byteread = 0;

in = new FileInputStream(fileName);

ReadFromFile.showAvailableBytes(in);

// 读入多个字节到字节数组中,byteread为一次读入的字节数

while ((byteread = in.read(tempbytes)) != -1) {

System.out.write(tempbytes, 0, byteread);

}

} catch (Exception e1) {

e1.printStackTrace();

} finally {

if (in != null) {

try {

in.close();

} catch (IOException e1) {

}

}

}

}

/**

* 以字符为单位读取文件,常用于读文本,数字等类型的文件

*/

public static void readFileByChars(String fileName) {

File file = new File(fileName);

Reader reader = null;

try {

System.out.println("以字符为单位读取文件内容,一次读一个字节:");

// 一次读一个字符

reader = new InputStreamReader(new FileInputStream(file));

int tempchar;

while ((tempchar = reader.read()) != -1) {

// 对于windows下,\r\n这两个字符在一起时,表示一个换行。

// 但如果这两个字符分开显示时,会换两次行。

// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。

if (((char) tempchar) != '\r') {

System.out.print((char) tempchar);

}

}

reader.close();

} catch (Exception e) {

e.printStackTrace();

}

try {

System.out.println("以字符为单位读取文件内容,一次读多个字节:");

// 一次读多个字符

char[] tempchars = new char[30];

int charread = 0;

reader = new InputStreamReader(new FileInputStream(fileName));

// 读入多个字符到字符数组中,charread为一次读取字符数

while ((charread = reader.read(tempchars)) != -1) {

// 同样屏蔽掉\r不显示

if ((charread == tempchars.length)

&& (tempchars[tempchars.length - 1] != '\r')) {

System.out.print(tempchars);

} else {

for (int i = 0; i < charread; i++) {

if (tempchars[i] == '\r') {

continue;

} else {

System.out.print(tempchars[i]);

}

}

}

}

} catch (Exception e1) {

e1.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e1) {

}

}

}

}

/**

* 以行为单位读取文件,常用于读面向行的格式化文件

*/

public static void readFileByLines(String fileName) {

File file = new File(fileName);

BufferedReader reader = null;

try {

System.out.println("以行为单位读取文件内容,一次读一整行:");

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

String tempString = null;

int line = 1;

// 一次读入一行,直到读入null为文件结束

while ((tempString = reader.readLine()) != null) {

// 显示行号

System.out.println("line " + line + ": " + tempString);

line++;

}

reader.close();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e1) {

}

}

}

}

/**

* 随机读取文件内容

*/

public static void readFileByRandomAccess(String fileName) {

RandomAccessFile randomFile = null;

try {

System.out.println("随机读取一段文件内容:");

// 打开一个随机访问文件流,按只读方式

randomFile = new RandomAccessFile(fileName, "r");

// 文件长度,字节数

long fileLength = randomFile.length();

// 读文件的起始位置

int beginIndex = (fileLength > 4) ? 4 : 0;

// 将读文件的开始位置移到beginIndex位置。

randomFile.seek(beginIndex);

byte[] bytes = new byte[10];

int byteread = 0;

// 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。

// 将一次读取的字节数赋给byteread

while ((byteread = randomFile.read(bytes)) != -1) {

System.out.write(bytes, 0, byteread);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

if (randomFile != null) {

try {

randomFile.close();

} catch (IOException e1) {

}

}

}

}

/**

* 显示输入流中还剩的字节数

*/

private static void showAvailableBytes(InputStream in) {

try {

System.out.println("当前字节输入流中的字节数为:" + in.available());

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

String fileName = "C:/temp/newTemp.txt";

ReadFromFile.readFileByBytes(fileName);

ReadFromFile.readFileByChars(fileName);

ReadFromFile.readFileByLines(fileName);

ReadFromFile.readFileByRandomAccess(fileName);

}

}

相关文章:

  • java中成员变量的分类_Java基础—变量分类、类与对象
  • java语言字符串代码_JAVA语言实现字符串排序的代码教程
  • java findcontour_cvFindContours获取轮廓数目和轮廓图像
  • java if 没有_使用if语句时,为什么我的代码没有给出正确的答案? - java
  • php时间格式转换成时间戳,php时间格式转换成时间戳的方法
  • echarts php 数据处理,PHP 使用Echarts生成数据统计报表详解
  • conv php,php metaphone()函数及php localeconv() 函数实例解析
  • php 配置 error_reporting,关于php中的错误配置display_errors与error_reporting说明
  • php计算器源码,php 简单计算器
  • 学生考勤管理系统php,php158 学生考勤管理系统
  • linux php-mbstring下载,linux下php加载mbstring的方法 | 快享
  • php 與或非,js逻辑与,或,非
  • java邮件代码,java发送邮件(示例代码)
  • linux下mysql乱码,Linux下mysql中文乱码解决方案
  • matlab里怎么计算期望,§7.4.2 利用MATLAB计算随机变量的期望和方差.pdf
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • Mybatis初体验
  • Nacos系列:Nacos的Java SDK使用
  • react 代码优化(一) ——事件处理
  • Sass Day-01
  • vue+element后台管理系统,从后端获取路由表,并正常渲染
  • 猴子数据域名防封接口降低小说被封的风险
  • 记一次删除Git记录中的大文件的过程
  • 目录与文件属性:编写ls
  • 入门到放弃node系列之Hello Word篇
  • 数组大概知多少
  • 微服务入门【系列视频课程】
  • 远离DoS攻击 Windows Server 2016发布DNS政策
  • ‌[AI问答] Auto-sklearn‌ 与 scikit-learn 区别
  • #Linux(帮助手册)
  • #QT(一种朴素的计算器实现方法)
  • #控制台大学课堂点名问题_课堂随机点名
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (Charles)如何抓取手机http的报文
  • (echarts)echarts使用时重新加载数据之前的数据存留在图上的问题
  • (保姆级教程)Mysql中索引、触发器、存储过程、存储函数的概念、作用,以及如何使用索引、存储过程,代码操作演示
  • (四)Tiki-taka算法(TTA)求解无人机三维路径规划研究(MATLAB)
  • (淘宝无限适配)手机端rem布局详解(转载非原创)
  • (限时免费)震惊!流落人间的haproxy宝典被找到了!一切玄妙尽在此处!
  • (已解决)报错:Could not load the Qt platform plugin “xcb“
  • (正则)提取页面里的img标签
  • (转) ns2/nam与nam实现相关的文件
  • (转) SpringBoot:使用spring-boot-devtools进行热部署以及不生效的问题解决
  • (转)VC++中ondraw在什么时候调用的
  • (转载)Linux网络编程入门
  • ***linux下安装xampp,XAMPP目录结构(阿里云安装xampp)
  • .NET Core中Emit的使用
  • .NET Framework 的 bug?try-catch-when 中如果 when 语句抛出异常,程序将彻底崩溃
  • .NET 将混合了多个不同平台(Windows Mac Linux)的文件 目录的路径格式化成同一个平台下的路径
  • .NET 设计模式初探
  • :中兴通讯为何成功
  • @RequestMapping 的作用是什么?
  • [2018-01-08] Python强化周的第一天
  • [BROADCASTING]tensor的扩散机制
  • [BUG]vscode插件live server无法自动打开浏览器