java IO流 之 输入流 InputString()的使用
作者:Bigerf 时间:2023-08-22 07:44:31
本文主要给大家介绍java的InputStream 流的使用。
(1)FileInputstream: 子类,读取数据的通道
使用步骤:
1.获取目标文件:new File()
2.建立通道:new FileInputString()
3.读取数据:read()
4.释放资源:close()
//一些默认要导入的包
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//分别调用方法查看效果
test1();
System.out.println("-------------------------------------------");
test2();
System.out.println("-------------------------------------------");
test3();
System.out.println("-------------------------------------------");
test4();
}
(2)读取数据的三种方式
1.直接读取 (一次只能一个字节)
int date = fileInputStream.read();
char date3 = (char)fileInputStream.read();
//方式一 直接打印
public static void test1() throws IOException{
//(1)获取目标文件路径
File file = new File("C:\\Users\\joke\\Desktop\\Demo1.java");
//(2)根据目标文件路径 建立通道: new FileInputStream(file)
FileInputStream fileInputStream = new FileInputStream(file);
//(3)读取数据 :read();
int date = fileInputStream.read();//这里是int类型
int date2 = fileInputStream.read();//
char date3 = (char)fileInputStream.read(); //以char类型显示
System.out.println(date+"\\"+date2+"\\"+date3);
//(4)释放资源
fileInputStream.close();
}
2.单独使用for循环(效率低)
for(int i = 0; i < file.length();i++){
System.out.print((char)fileInputStream.read());
}
//方式二 循环遍历
public static void test2() throws IOException{
//通过时间测试效率
long startTime = System.currentTimeMillis();
File file = new File("C:\\Users\\joke\\Desktop\\Demo1.java");
FileInputStream fileInputStream = new FileInputStream(file);
//for循环
for(int i = 0; i < file.length();i++){
System.out.print((char)fileInputStream.read());
}
fileInputStream.close();
long endTime = System.currentTimeMillis();
System.out.println("读取文件所花时间:"+(endTime-startTime));
}
3.Byte[ ] 缓冲区(只能读取指定的字节数不能读取一个完整的文件)
byte[] bt = new byte[1024];
int count = fileInputStream.read(bt);
System.out.println(new String (bt,0,count));
//方式三 创建缓冲区(只能读取制定的大小,不能读取一个完整的文件)
public static void test3() throws IOException{
File file = new File("C:\\Users\\joke\\Desktop\\Demo1.java");
FileInputStream fileInputStream = new FileInputStream(file);
//创建缓冲区,加快读取数据,确定要读取的字节大小
byte[] bt = new byte[1024];
//read() 读取字节
int count = fileInputStream.read(bt);
System.out.println(count); //显示读取到的字节数
System.out.println(new String (bt,0,count));//将字节转为字符串显示
fileInputStream.close();
}
4.缓冲区和循环结合。缓冲区一般设置为1024的倍数。理论上设置的缓冲区越大,读取效率越高
byte[] bt = new byte[1024];
int count = 0;
while((count = fileInputStream.read(bt)) != -1){
System.out.println(new String (bt,0,count));
}
//方式四 循环与缓冲区结合(效率高)
public static void test4() throws IOException{
//通过时间测试效率
long startTime = System.currentTimeMillis();
File file = new File("C:\\Users\\joke\\Desktop\\Demo1.java");
FileInputStream fileInputStream = new FileInputStream(file);
//缓冲区一般设置为1024的倍数。理论上设置的缓冲区越大,读取效率越高
byte[] bt = new byte[1024];
int count = 0;
//read返回 -1 时,证明已经遍历完
while((count = fileInputStream.read(bt)) != -1){
//字符串型显示(从bt中的第0个字节开始遍历count个长度)
System.out.println(new String (bt,0,count));
}
fileInputStream.close();
long endTime = System.currentTimeMillis();
System.out.println("读取文件所花时间:"+(endTime-startTime));
}
陌陌说:
在以上,对比第二个和第四个方法,会发现方法四的效率是比较高的,所以推荐使用的四个方法
在这里我们是直接抛出异常,除了抛出之外我们还可以使用
try{}cater{}finally{}
的方式来处理异常
以上所述是小编给大家介绍的java IO流 之 输入流 InputString()的使用,希望对大家有所帮助
来源:http://www.cnblogs.com/bigerf/archive/2016/12/06/6136557.html
标签:java,io,输入流,inputstring
0
投稿
猜你喜欢
Java后台批量生产echarts图表并保存图片
2023-11-25 03:24:39
C#中SQL参数传入空值报错解决方案
2023-12-14 14:28:59
Android仿开心消消乐大树星星无限循环效果
2021-10-04 07:19:06
Android AlertDialog的几种用法详解
2023-12-02 18:15:50
基于C#实现简单的随机抽奖小程序
2023-07-14 10:15:50
使用Java实现三种等级的扫雷游戏(完整版)
2023-05-10 07:34:17
详解MybatisPlus中@Version注解的使用
2023-11-09 23:49:17
基于mybatis逆向工程的使用步骤详解
2022-10-28 09:27:26
用C# 实现鼠标框选效果的实现代码
2023-04-18 14:08:26
SprinBoot如何集成参数校验Validator及参数校验的高阶技巧
2023-10-23 16:24:31
C#中的委托和事件
2023-11-27 12:33:34
Android 源码浅析RecyclerView ItemAnimator
2022-04-20 19:47:41
详解Java目录操作与文件操作教程
2023-12-05 13:45:48
android和服务器的URLEncodedUtils乱码编码问题的解决方案
2021-11-19 08:13:12
Android图片处理实例分析
2022-09-10 20:34:50
Flutter 自定义Drawer 滑出位置的大小实例代码详解
2021-09-18 23:18:19
Java基于Tcp/ip连接的多人交互聊天室
2022-03-31 17:47:35
了解Java虚拟机JVM的基本结构及JVM的内存溢出方式
2023-02-20 03:08:51
JAVA关键字及作用详解
2022-05-03 03:50:14
Spring MVC中使用Controller如何进行重定向
2022-05-12 04:09:35