四种读写方案IO流 (JAVA)
File类用于访问文件或目录的属性
流:指一连串流动的字符,是以先进先出的方式发送信息的通道。程序和数据源之间是通过流联系起来的。
第一套:字节流读取写入方案
FileInputStream :字节流方式读取文本文件

FileInputStream fis=new FileInputStream("E:\\读取文件.txt");
byte[]bytes=new byte[1024];
int data;
while((data=fis.read(bytes))!=-1)
{
String str=new String(bytes,0,data);
System.out.println(str);
}
fis.close();
}

FileOutputStream:字节流写入硬盘

FileOutputStream fos=new FileOutputStream("E:\\1.txt");
String word="高考是人生的分水岭";
byte[] bytes = word.getBytes();
fos.write(bytes);
fos.close();
System.out.println("写入成功!");
}
}

第二套:字符流读取写入方案
FileReader:字符流读取文本

FileReader fr=new FileReader("E:\\读取文件.txt");
char[]chars=new char[1024];
int data;
while((data=fr.read(chars))!=-1)
{
String str=new String(chars);
System.out.println(str);
}
}

FileWriter:字符流写入文本

FileWriter fw=new FileWriter("E:\\2.txt");
fw.write("新的6月");
System.out.println("写入成功!");
fw.close();
}

第三套:<BufferedReader、BufferedWriter>一般和FileReader和FileWriter结合使用
BufferedReader:自定义缓存大小,读取文本 8192个char

FileReader fr=new FileReader("E:\\读取文件.txt");
BufferedReader br=new BufferedReader(fr);
String line;
while((line=br.readLine())!=null)
{
System.out.println(line);
}
br.close();
fr.close();
}

BufferedWriter:写入文本

FileWriter fw=new FileWriter("E:\\5.txt");
BufferedWriter bw=new BufferedWriter(fw);
bw.write("OK!!");
bw.close();
fw.close();
System.out.println("写入成功!!");
}

第四套:可以读取二进制(img图片等 )
DataInputStream:将本地的img加载到内存中

FileInputStream fis=new FileInputStream("E:\\5.txt");
FileOutputStream fos=new FileOutputStream("D:\\55.txt");
DataInputStream dis=new DataInputStream(fis);
DataOutputStream dos=null;
byte[]bytes=new byte[1024];
int data;
while((data=dis.read(bytes))!=-1)
{
dos=new DataOutputStream(fos);
dos.write(bytes);
}
dos.close();
dis.close();
fos.close();
fis.close();
System.out.println("copy succes!!!");
}

DataOutputStream:将内存中的二进制数据写入到硬盘上的某个文件中

DataOutputStream out=null;
DataInputStream dis=null;
try {
//创建输入流对象
FileInputStream fis=new FileInputStream("c:\\范宁.jpg");
dis=new DataInputStream(fis);
//创建输出流对象
FileOutputStream outFile=new FileOutputStream("c:\\范宁小美女33.jpg");
out=new DataOutputStream(outFile);
int temp=dis.read();
while (temp!=-1) {
out.write(temp);
temp=dis.read();
}
System.out.println("复制成功");
fis.close();
outFile.close();
} catch (Exception e) {
System.out.println("文件不存在");
}finally{
try {
if (dis!=null) {
dis.close();
}
if (out!=null) {
out.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}

注:在java中,byte数组和String字符串如何转换?
1、string 转 byte[]
String str = "Hello";
byte[] srtbyte = str.getBytes();
2、byte[] 转 string
byte[] srtbyte;
String str = new String(srtbyte);
System.out.println(str);
四种读写方案IO流 (JAVA)的更多相关文章
- Java四种引用--《深入理解Java虚拟机》学习笔记及个人理解(四)
Java四种引用--<深入理解Java虚拟机>学习笔记及个人理解(四) 书上P65. StrongReference(强引用) 类似Object obj = new Object() 这类 ...
- java 四种方式实现字符流文件的拷贝对比
将D:\\应用软件\\vm.exe 拷贝到C:\\vm.exe 四种方法耗费时间对比 4>2>3>1 package Copy; import java.io.Buffere ...
- Java成长第四集--文本处理IO流
Java IO流在实际业务中使用的频率还是蛮高的,一些业务场景比如,文件的上传和导出,文件的读取等基本都是通过操作IO流来实现的,所以IO流是我们现在学习过程中必须要掌握的技能之一,熟练的使用IO流, ...
- Java中的四套读写方案
一.字节流读写方案 FileInputStream:字节流方式读取文本文件 FileoutputStream:字节流写入硬盘 二.字符流读写方案 FileReader:字符流读取文本 FileWrit ...
- java-mybaits-012-mybatis-Interceptor-拦截器读写分离四种实现方案
一.概述 基本项目搭建 技术框架:spring web mvc .日志[slf4j.log4j2].mybatis.druid.jetty插件启动.mybatis-generator逆向配置生产dao ...
- 数据读写API——IO流
理清一些概念 1.Java 中的IO是干啥的? IO指的是Input和Output,主要目的是实现数据在存储介质之间的传输.[流:数据流,类比与水流的流动] 2.IO分类 按照操作单元来划分,可以分为 ...
- 总结:视频播放的四种实现方案(Native)
一.来自 AVFoundation的 AVPlayer对象 特点: 1. AVPlayer > 优点: 可以自定义UI, 进行控制 > 缺点: ...
- PHP四种序列化方案
原文地址:https://t.ti-node.com/thread/... 数据的序列化是一个非常有用的功能,然而目测很多人跟我一样,在刚接触这玩意的时候压根就不理解这货色到底是干啥用的,反正老师说了 ...
- 基于redis实现的四种常见的限流策略
引言 在web开发中功能是基石,除了功能以外运维和防护就是重头菜了.因为在网站运行期间可能会因为突然的访问量导致业务异常.也有可能遭受别人恶意攻击 所以我们的接口需要对流量进行限制.俗称的QPS也是对 ...
随机推荐
- 卸载已经安装的rpm包
两个关键点: 1.如果提示有xxx.rpm包已经被installed了,那么先用rpm -e --nodeps xxx来卸载 2.如果存在多个版本的话,用rpm -e --allmatches来卸载 ...
- 一些常用的C++标准函数
一些常用的C++标准函数 double atof(const char* p); int atoi(const char* p); long atol(const char* p); cstdlib ...
- java大数取模
题目链接:http://lightoj.com/volume_showproblem.php?problem=1214 用java写大数果然是方便多了! import java.math.BigInt ...
- loj 1379(最短路变形)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27087 思路:题目的意思是求S->T的所有路径中花费总和小于 ...
- WEB前端知识体系脑图
说在开始的话: 我上大学那会,虽说主要是学Java语言,但是web前端也稍微学了一些,那时候对前端也没多在意,因为涉入的不深,可以搞一个差不多可以看的界面就可以了,其他也没过多在意. 因为稍微了解一点 ...
- LoadRunner 事务函数
status 包括LR_PASS, LR_FAIL, LR_AUTO, LR_STOP(这个没用过) lr_set_transaction_instance_status(status); 可以根 ...
- SoapUI接口测试之实战运用操作(五)
SoapUI接口测试之实战运用操作(五)
- HTML轉PDF - 使用Pechkin套件
剛好跟人討論到HTML轉PDF需求,便對工具進行簡單評估以備不時之需. 網路上比較多人推的是WkHtmlToPdf,如果是用.NET開發,已經有人包成NuGet套件,直接搜尋pechkin就可找到,它 ...
- css样式—字体垂直、水平居中
“来,老板娘,给个div瞅瞅”: “好的,宇哥,来了了了”: <div class="tt">啦啦啦</div> “给各样啊,我去”: “是”: .tt{ ...
- 2016.8.27 JavaScript入门之四
1.比较运算符“>”也可以,用数字和字符串进行比较: 2.比较运算符“&&”,表示并且: 3.比较运算符“||”,表示或者: 4.顺序是重要的,循环和if判断的顺序决定了,程序的 ...