java io读取文件操作代码实例
作者:闻窗 发布时间:2023-04-12 08:53:57
标签:java,io,读取,文件,操作
这篇文章主要介绍了java io读取文件操作代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要分为字节读取和字符读取,字节读取可以一个一个读取和字节数组读取,字符读取同样之,字符读取适合文本读取,字节读取皆可以
这里直接上代码,读取文件的9个小demo
package com.io;
import org.junit.Test;
import java.io.*;
public class FileTest {
//1、字节流字节一个一个读取
@Test
public void test() throws IOException{
InputStream fis = new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"));
int len;
//按字节一个一个读取
while ((len = fis.read())!=-1){
System.out.print((char)len+"t");
};
fis.close();
}
//输出结果h e l l o w o r l d
//2、字节流字节数组读取
@Test
public void test1() throws IOException{
InputStream fis = new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"));
byte[] bytes = new byte[2];
int len ;
//按字节数组读取 bytes存储的是读取的数据
while ((len = fis.read(bytes))!=-1){
System.out.print((new String(bytes))+"t");
};
fis.close();
}
//输出结果 he ll ow or ld
//3、缓冲字节流字节一个一个读取
@Test
public void test2() throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")));
int len ;
while ((len = bis.read())!=-1){
System.out.print((char)len+"t");
};
bis.close();
}
//输出结果h e l l o w o r l d
//4、缓冲字节流字节数组读取
@Test
public void test3() throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")));
byte[] bytes = new byte[3];
int len ;
//按字节数组读取 bytes存储的是读取的数据
while ((len = bis.read(bytes))!=-1){
System.out.print(new String(bytes)+"t");
};
bis.close();
}
//输出结果hel low orl drl
//5、字符流一个一个读取
@Test
public void test4() throws IOException{
InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")));
int len ;
//按字节数组读取 bytes存储的是读取的数据
while ((len = isr.read())!=-1){
System.out.print((char)len+"t");
};
isr.close();
}
//6、字符流字符数组读取
@Test
public void test5() throws IOException{
InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")));
char[] chars = new char[3];
int len ;
//按字节数组读取 bytes存储的是读取的数据
while ((len = isr.read(chars))!=-1){
System.out.print(new String(chars)+"t");
};
isr.close();
}
//7、缓冲字符流字符一个一个读取
@Test
public void test6() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"))));
int len ;
//按字节数组读取 bytes存储的是读取的数据
while ((len = br.read())!=-1){
System.out.print((char)len+"t");
};
br.close();
}
//8、缓冲字符流字符数组读取
@Test
public void test7() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"))));
char[] chars = new char[3];
int len ;
//按字节数组读取 bytes存储的是读取的数据
while ((len = br.read(chars))!=-1){
System.out.print(new String(chars)+"t");
};
br.close();
}
//9、缓冲字符流按行读取
@Test
public void test8() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"))));
//按字节数组读取 bytes存储的是读取的数据
String str;
while ( (str = br.readLine())!=null){
System.out.print(str+"t");
};
br.close();
}
}
来源:https://www.cnblogs.com/tdyang/p/10834577.html


猜你喜欢
- (一)什么是微服务网关后端写完所有的微服务之后,最终是要交给前端去调用。我们都知道每个微服务都有各自的端口号,如果前端直接通过IP加端口的方
- 1、UUID类库UUID 根据时间戳实现自动无重复字符串定义// 获取UUIDpublic static UUID randomUUID()
- 引言相信伙伴们在日常的开发工作中,一定会遇到事件冲突的问题,e.g. 一个页面当手指滑动的时候,会翻到下一页;点击的时候,需要响应页面中的元
- 本文实例讲述了C#简单实现防止多个程序运行的方法。分享给大家供大家参考,具体如下:/// <summary>/// 应用程序的主
- Activities提供了一种方便管理的创建、保存、回复的对话框机制,例如 onCreateDialog(int), onPrepareDi
- <script>//验证身份证号方法var test=function(idcard){var Errors=new Array
- 本文实例讲述了android从系统图库中取图片的实现方法。分享给大家供大家参考。具体如下:在自己应用中,从系统图库中取图片,然后截取其中一部
- 初看 cgaolei 翻译的 Java技巧之双括弧初始化 一文,走马观花,只知用法,未细看后面的解释。蔚为惊艳,心里想 Java 竟然有这么
- 本文研究的主要是SpringMVC中使用Thymeleaf模板引擎的相关内容,具体介绍如下。Thymeleaf提供了一组Spring集成,允
- Android 8.0推出了PictureInPicture(画中画功能),目前只有在8.0以上的系统上支持。对比IOS,IOS的Pictu
- 本文实例为大家分享了Android studio点击跳转WebView的具体代码,供大家参考,具体内容如下代码文件import androi
- ThreadGroup的作用及方法ThreadGroup线程组,java对这个类的描述呢就是“线程组表示一组线程。此外,线程组还可以包括其他
- 面试题1:说一下你对ReentrantLock的理解?ReentrantLock是JDK1.5引入的,它拥有与synchronized相同的
- 本文研究的主要是Java中hashCode的正确求值方法的相关内容,具体如下。散列表有一项优化,可以将对象的散列码(hashCode)缓存起
- 引言前一段有幸参与到一个智能家居项目的开发,由于之前都没有过这方面的开发经验,所以对智能硬件的开发模式和技术栈都颇为好奇。智能可燃气体报警器
- Java当中的类和对象1. 类和对象的初步认知java 是一门面向对象的语言,所谓面向对象有别于面向过程,面向对象是只需对象之间的交互即可完
- 前提前段时间在做一个对外的网关项目,涉及到加密和解密模块,这里详细分析解决方案和适用的场景。为了模拟真实的交互场景,先定制一下整个交互流程。
- 博主最近在做一个内网项目,内部可以访问外部数据,但是外部访问不了内部数据,这也就造成了可能文件无法上传,所以博主另辟蹊径,在本地服务器上建立
- 开篇Mybatis有个实用的功能就是逆向工程,能根据表结构反向生成实体类,这样能避免手工生成出错。市面上的教程大多都很老了,大部分都是针对m
- 本文实例为大家分享了Android实现图片点击 * 效果的具体代码,供大家参考,具体内容如下实现效果:需要注意的点:ValueAnimator