Java IO流相关知识代码解析
作者:在成为巨擘的路上 发布时间:2023-08-05 05:48:04
标签:java,io流
一、IO流的分类
字符流
Reader
InputStreamReader(节点流)
BufferedReader(处理流)
Writer
OutputStreamWriter(节点流)
BufferedWriter(处理流)
PrintWriter
字节流
InputStream
FileInputStream(节点流)
BufferedInputStream(处理流)
ObjectInputStream(处理流)
PrintStream
OutputStream
FileOutputStream(节点流)
BufferedOutputStream(处理流)
ObjectOutputStream(处理流)
断点处理的流
RandomAccessfile
二、IO流的用法
1、转换流的用法
FileInputStream in = new FileInputStream(newFile(""));
Readerreader = new InputStreamReader(in);//字节转字符
FileOutputStreamout = new FileOutputStream(newFile(""));
Writer writer = new OutputStreamWriter(out);//字符转字节
2、对象序列化,对象需要实现Serializable接口
FileOutputStreamfileOutputStream = new FileOutputStream("C:\\Users\\lx\\Desktop\\Record.txt");
ObjectOutputStreamobjectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(object);//向指定文件写入对象object
objectOutputStream.close();
FileInputStreamfileInputStream = new FileInputStream("C:\\Users\\lx\\Desktop\\Record.txt");
ObjectInputStreamobjectInputStream = new ObjectInputStream(fileInputStream);
object = objectInputStream.readObject();//读取得到对象object
fileInputStream . lose();
3、断点的运用
public class Copy extends Thread{
//可以利用多线程实现拷贝
longstart;
longend;
Filesorce;
Filetargetdir;
publicCopy() {
}
publicCopy(longstart,long end, File sorce, File targetdir) {
//利用构造方法传递需要拷贝的长度,拷贝开始位置,以及目标文件和源文件
super();
this.start= start;
this.end= end;
this.sorce= sorce;
this.targetdir= targetdir;
}
@Override
publicvoid run(){
try{
RandomAccessFilesouceRaf = new RandomAccessFile(sorce,"r");
RandomAccessFiletargetRaf = new RandomAccessFile(newFile(targetdir,sorce.getName()),"rw");
souceRaf.seek(start);
targetRaf.seek(start);
intlen= 0;
byte[]bs = new byte[1024];
longseek;
System.out.println(start+"---->"+end+this.getName());
while((len= souceRaf.read(bs))!=-1){
targetRaf.write(bs, 0, len);
seek= souceRaf.getFilePointer();
//获取断点位置
if(seek== end){
break;
}
}
targetRaf.close();
souceRaf.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
4、字节流的用法
public class Test_InputStream {
//利用字节流获取文本文件内容,但是容易出现问题
/*
//可能出现int长度越界
public static void main(String[] args) throws IOException {
InputStream inputStream = new FileInputStream(new File("C:\\Users\\lx\\Desktop\\test\\33.txt"));
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
String str = new String(b);
System.out.println(str);
}
*/
//可能出现乱码
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\lx\\Desktop\\test\\33.txt");
InputStream inputStream = new FileInputStream(file);
//统计每次读取的实际长度
int len = 0;
//声明每次读取1024个字节
byte[] b = new byte[2];
StringBuffer sBuffer = new StringBuffer();
while((len=inputStream.read(b))!=-1){
sBuffer.append(new String(b,0,len));
}
System.out.println(sBuffer.toString());
}
}
//利用字节流拷贝文件
public void copy(File sourceFile, File targetDir) {
//
FileInputStreamfileInputStream = null;
FileOutputStreamfileOutputStream = null;
fileInputStream= new FileInputStream(sourceFile);
FiletargetFile = new File(targetDir,sourceFile.getName());
fileOutputStream= new FileOutputStream(targetFile);
byte[]b = new byte[1024];
intlen = 0;
while((len= fileInputStream.read(b)) != -1) {
fileOutputStream.write(b, 0, len);
}
}
5、缓存字符流的用法
publicstatic void main(String[] args) throws IOException {
//缓存字符流实现写入文件
InputStreamin = System.in;
Readerreader = new InputStreamReader(in);
BufferedReaderbr = new BufferedReader(reader);
BufferedWriterbw = new BufferedWriter(new FileWriter(new File("src/1.txt")));
Strings="";
while((s=br.readLine())!=null) {
bw.write(s);
bw.newLine();
bw.flush();
//字符流千万不要忘了flush!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}
}
来源:http://blog.csdn.net/qq_24065713/article/details/76407833
0
投稿
猜你喜欢
- 简介对于一个APP来说,肯定会有一个AppBar,这个AppBar一般包含了APP的导航信息等。虽然我们可以用一个固定的组件来做为AppBa
- 简介官方API文档Scaffold的of方法说明有说明调用Scaffold.of方法是在Scallfold的子组件的Build方法中,也就是
- 由于我们在eclipse ee中把项目部署在web端经常会出现报404错误。原因为:404状态码是一种http状态码,其意思是: 所请求的页
- 本文实例为大家分享了Android自定义View之组合控件,仿电商app顶部栏的相关代码,供大家参考,具体内容如下效果图:分析:左右两边可以
- 在前面的《基于任务的异步编程模式(TAP)》文章中讲述了.net 4.5框架下的异步操作自我实现方式,实际上,在.net 4.5中部分类已实
- 前言gps定位服务的学习是这段时间gps课程的学习内容,之前老师一直在将概念,今天终于是实践课(其实就是给了一个案例,让自己照着敲).不过在
- 如何高效地进行数组复制?如果把一个变量值复制给另外一个数组变量,那么2个变量指向托管堆上同一个引用。如果想在托管堆上创建另外的一份数组实例,
- 在基于UI元素的自动化测试中, 无论是桌面的UI自动化测试,还是Web的UI自动化测试. 首先我们需要查找和识别UI
- SpringBootWeb开发回顾一下:springboot帮助我们配置了什么,能不能进行修改,能修改哪些,能否扩展?xxxAutoConf
- 本文实例为大家分享了java实现简单石头剪刀布游戏的具体代码,供大家参考,具体内容如下问题描述Alice, Bob和Cindy一起玩猜拳的游
- WCF实例(带步骤) <xmlnamespace prefix ="o" ns ="urn:schema
- 背景产品想对多次快速点击做一下优化,想要的效果就是双击不会打开多次但是从开发角度来说,我可以用kotlin的拓展方法来调整这个,但是之前的历
- 1. 在原有工程目录右键-> new ->Module->:2. 选择library:3. 一路next,最后finish
- 本文实例为大家分享了Android绝对布局AbsoluteLayout的具体代码,供大家参考,具体内容如下1>AbsoluteLayo
- 对Jpa Entity关系映射中mappedBy的理解mappedBy 单向关系不需要设置该属性,双向关系必须设置,避免双方都建立外键字段数
- 前言回想一下,在学Java时接触的正则表达式,其实Kotlin中也是类似。只不过使用Kotlin 的语法来表达,更为简洁。正则(Regex)
- 说到多渠道,这里不得不提一下友盟统计,友盟统计是大家日常开发中常用的渠道统计工具,而我们的打包方法就是基于友盟统计实施的。按照友盟官方文档说
- Android的应用被限制为最多占用16m的内存,至少在T-Mobile G1上是这样的(当然现在已经有几百兆的内存可以用了——译者注)。它
- flutter material widget组件之信息展示组件,供大家参考,具体内容如下widget分为两类:widgets librar
- 一、导入和导出导入:通过解析excel表格中的数据,然后将数据放到一个集合中,接着通过对持久层操作,将数据插入到数据库中,再加载一下页面,从