一.文件专属流

凡是类名带stream的都是字节流,带Reader和Writer的都是字符流

1.字节流

a:输入:FileInputStream:

负责将硬盘中的文件读取到内存之中,每次只读取一个字节,下面是具体的读取方法:

   public static void main(String[] args) {FileInputStream fis=null;try {fis=new FileInputStream("tempFile") ;//""内为文件名byte[] bytes=new byte[1024] ;//每次读取一个byte数组大小的内容int count =0 ;//因为fis的read方法返回的是每次成功读取bytes的长度,当返回-1时表示没有读取到任何字节,读取结束while ((count=fis.read(bytes))!=-1){System.out.println(new String(bytes,0,count)); //new 个String给他输出}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {if(fis!=null){try {fis.close();//所有流都要在最后关闭,try,catch这些编译器就可以解决啦} catch (IOException e) {e.printStackTrace();}}}}

D:\Java\jdk\bin\java.exe
狂肝Java语言,做合格攻城狮!Process finished with exit code 0

FileInputStream的常用方法:

 int available() :返回该文件可读取的字节数,可以用在byte数组的初始化长度上,直接

byte[] bytes =new byte[ fis.available() ] ,

这样就用不到循环了,但是文件太大的时候不合适

long skip(long n) :读取时跳过n个长度的字节

            int count =0 ;fis=new FileInputStream("tempFile") ;fis.skip(13);byte[] bytes=new byte[fis.available()] ;count=fis.read(bytes) ;System.out.print(new String(bytes,0,count));D:\Java\jdk\bin\java.exe
言,做合格攻城狮!Process finished with exit code 0

b:输出:FileOutputStream:

负责将内存中的内容一个字节一个字节的写入硬盘,一般用法

public static void main(String[] args) {FileOutputStream fil =null ;try {//无参构造会将源文件内容清空//fil=new FileOutputStream("file") ;//以追加的方式写入文件的末尾fil=new FileOutputStream("tempFile",true);String s="\n我是一个中国人!  " ;byte[] bytes=s.getBytes();fil.write(bytes);//输出完成后要及时刷新fil.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {if(fil!=null){try {fil.close();} catch (IOException e) {e.printStackTrace();}}}}

字节的输出输入方式,几乎所有的文件都能复制,怎么复制呢?将两个结合一下就行

    public static void copyDoc(File from, File to) {FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream(from);fos = new FileOutputStream(to, true);byte[] bytes = new byte[1024 * 512];int count = 0;while ((count = fis.read(bytes)) != -1) {fos.write(bytes, 0, count);}fos.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (fis!=null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}if (fos != null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}

2.字符流

a:输入:FileReader

负责从硬盘中一个字符一个字符的读取文件到内存中,一般用法和上面的字节输入流差不多,byte换成char

public static void main(String[] args) {FileReader fir =null ;try {fir=new FileReader("tempFile") ;char[] chars=new char[4] ;int count=0;while ((count=fir.read(chars))!=-1){System.out.print(new String(chars,0,count));}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(fir!=null){try {fir.close();} catch (IOException e) {e.printStackTrace();}}}}

b:输出:FileWriter:

public static void main(String[] args) {FileWriter fiw=null ;try {fiw=new FileWriter("file",true) ;//直接写入字符串char[] chars={'\n','我','是','一','名','J','a','v','a','攻','城','狮','!'};fiw.write(chars);//写入字符串的一部分fiw.write(chars,5,8);//直接写入字符串String s="\n好好学软件,将来进鹅厂!" ;fiw.write(s);fiw.flush();} catch (IOException e) {e.printStackTrace();}finally {if(fiw!=null){try {fiw.close();} catch (IOException e) {e.printStackTrace();}}}}

二.转换流

a:输入:InputStreamReader,输出:OutputStreamWriter

分别实现将字节流转换为字符流, 一般建议定义在下面的缓冲流里面

public static void main(String[] args)throws Exception {FileInputStream fis=new FileInputStream("File") ;FileReader fr=null ;fr= (FileReader) new InputStreamReader(fis);//FileReader fr =new InputStreamReader(New FileInoutStream("File"))FileOutputStream fos=new FileOutputStream("file") ;FileWriter fw=null;fw= (FileWriter) new OutputStreamWriter(fos);//FileWriter fw=new OutputStreamWriter(new FileOutputStream("File")) ;}

三.缓冲流

1.字符缓冲流

a:BufferedReader

BufferedReader(Reader in)创建使用默认大小的输入缓冲区的缓冲字符输入流。

BufferedReader(Reader in, int sz)创建使用指定大小的输入缓冲区的缓冲字符输入流。

构造方法中传入的是Reader的实现类,Reader接口是字符流专属,所以传入的参数要是字符流型,比如FileReader.

同时也可以将字节流转换成字符流当参数。

 BufferedReader bfr= null;try {bfr = new BufferedReader(new InputStreamReader(new FileInputStream("file")));} catch (FileNotFoundException e) {e.printStackTrace();}String readline=null ;//一行一行的读,readline的返回类型是String,所以结束条件是返回NULLtry {while ((readline = bfr.readLine()) !=null) {System.out.println(readline);}} catch (IOException e) {e.printStackTrace();}finally {if(bfr!=null){try {bfr.close();} catch (IOException e) {e.printStackTrace();}}}}

b:BufferedWriter

BufferedWriter(Writer out)创建使用默认大小的输出缓冲区的缓冲字符输出流。

BufferedWriter(Writer out, int sz)创建一个新的缓冲字符输出流,使用给定大小的输出缓冲区。

public static void main(String[] args) {BufferedWriter bfw= null;try {bfw = new BufferedWriter(new OutputStreamWriter(new                 FileOutputStream("file")));String s="顶级套娃\n" ;String s2="FileOutputStream fos=new FileOutputStream();\n" +"OutputStreamWriter osw=new OutStreamWriter(fos);\n" +"BufferedWriter bfw=new BufferedWriter(osw);\n" ;bfw.write(s);bfw.write(s2);bfw.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(bfw!=null){try {bfw.close() ;} catch (IOException e) {e.printStackTrace();}}}}

2.字节缓冲流

a:BufferedInputStream

b:BufferedOutStream

用法基本和字符缓冲流的一样,只不过传入的参数要是字节流类型的

BufferedInputStream(InputStream in)

创建一个 BufferedInputStream并保存其参数,输入流 in ,供以后使用。

BufferedOutputStream(OutputStream out)

创建一个新的缓冲输出流,以将数据写入指定的底层输出流。

四.标准输出流

1. printWriter

记得关闭文件,不然写的日志没得内容,它还不报错

printWriter

PrintWriter out=null ;try {//建立一个printStream指向文件loggerout =new PrintWriter(new FileOutputStream("logger",true)) ;//改变输出方向//System.setOut(out);Date nowTime=new Date();SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss") ;String strTime=sdf.format(nowTime);out.write(strTime+":"+doSomething);// System.out.println(strTime+":"+doSomething);} catch (FileNotFoundException e) {e.printStackTrace();}finally {out.close();}}public static void main(String[] args) {log("写Java PrintStream 的方法测试") ;log("学习Java io流") ;log("学习Java 多线程") ;log("发布csdn关于io流的博客") ;log("学习Java 文档注释") ;log("学习操作系统原理") ;System.out.println("----------------");}

2.printStream

默认输出到控制台,可以创建对象输出到指定文件,没有文件会创建文件

prtintStream

public class PrintStreamTest {public static void main(String[] args) {PrintStream ps=System.out ;ps.println("这就是流弊的标准输出流");try {PrintStream printStream=new PrintStream("file") ;printStream.println("将输出流转入文件file,不向控制台输出");String s="PrintString同时可以写入,相当于打印print" ;byte[] bytes={'l','o','v','e','y','o','u'} ;printStream.write(bytes);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}System.out.println("控制台我来喽@!");}
}

五.序列化和反序列化

1.序列化:

ObjectOutputStream

可以序列化的类必须实现Serializable接口,这样jvm才会给该类生成serialVersionUID,即序列化版本号,jvm根据这个版本号序列化。版本号在后期修改类的代码时会发生变化,导致以前写的代码报错,所以建议对要进行序列化的类编写时,把版本号静态化,idea可以自动生成。

public class Dancer implements Serializable {private static final long serialVersionUID = 5446290058445484067L;public transient int age ;public String name ;public  int no ;public Dancer(int age, String name) {this.age = age;this.name = name;}@Overridepublic String toString() {return "Dancer{" +"age=" + age +", name='" + name + '\'' +'}';}

创建对象来序列化,

ObjectOutputStream()为完全重新实现ObjectOutputStream的子类提供一种方法,不必分配刚刚被ObjectOutputStream实现使用的私有数据。

ObjectOutputStream(OutputStream out)创建一个写入指定的OutputStream的ObjectOutputStream。传入的参数要是字节流的

       Dancer dancer=new Dancer(22,"dapiaoliang") ;Dancer dancer1=new Dancer(29,"baihu");ObjectOutputStream oos=null ;try {//写到文件fileoos=new ObjectOutputStream(new FileOutputStream("file")) ;oos.writeObject(dancer);oos.writeObject(dancer1);oos.flush();} catch (IOException e) {e.printStackTrace();}finally {try {oos.close();} catch (IOException e) {e.printStackTrace();}}

也可以序列化一组对象

        Dancer dancer=new Dancer(22,"dapiaoliang") ;Dancer dancer1=new Dancer(29,"baihu");Dancer dancer2=new Dancer(18,"daxiong") ;List<Dancer> l=new ArrayList<>() ;l.add(dancer) ;l.add(dancer1) ;l.add(dancer2) ;ObjectOutputStream Oos=null ;try {Oos=new ObjectOutputStream(new FileOutputStream("file3")) ;Oos.writeObject(l);Oos.flush();} catch (IOException e) {e.printStackTrace();}finally{try {Oos.close() ;} catch (IOException e) {e.printStackTrace();}}

序列化后的对象是无法正常读出来的,必须要反序列化才行

2.反序列化:

ObjectInputStream

 ObjectInputStream ois=null;try {ois=new ObjectInputStream(new FileInputStream("file3")) ;List<Dancer> dancers=(List<Dancer>)ois.readObject();for(Dancer d:dancers) {System.out.println(d);}}  catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} finally{try {ois.close();} catch (IOException e) {e.printStackTrace();}}D:\Java\jdk\bin\java.exe
Dancer{age=0, name='dapiaoliang'}
Dancer{age=0, name='baihu'}
Dancer{age=0, name='daxiong'}Process finished with exit code 0

Java 文件操作#常用io流方法解读相关推荐

  1. Java基础:常用IO流

    1. 数据流 1.1 概述 数据流是操作基本数据类型的流,分为数据输入流,数据输出流. 1.2 数据输入流 1.DataInputStream:数据输出流允许应用程序以适当方式将基本 Java 数据类 ...

  2. C#文件操作(IO流 摘抄)

    11 文件操作概述 11.1 驱动器 在Windows操作系统中,存储介质统称为驱动器,硬盘由于可以划分为多个区域,每一个区域称为一个驱动器..NET Framework提供DriveInfo类和 D ...

  3. Java 持久化操作之 --io流与序列化

    摘自:http://www.cnblogs.com/lsy131479/p/8728724.html 1)File类操作文件的属性 1.File类的常用方法 1. 文件的绝对完整路径:getAbsol ...

  4. Java文件操作-I/O流

    文章目录 File类 概述 构造方法 创建功能 判断功能 获取功能 删除功能 绝对路径和相对路径的区别 递归 概述 递归求阶乘 IO流 IO流概述 IO流分类 字节流 字节流抽象基类 字节输出流 字节 ...

  5. Python中文件操作(IO流)及文件备份

    IO流主要作用与计算机中的输入与输出操作,一般来说,常见的IO流操作是内存和磁盘之间的输入和输出,它是一种持久操作,将数据持久化在磁盘上 在计算机中,它只能识别0和1,因此我们的文本文件要被计算机识别 ...

  6. python io流a+_Python基础——文件操作及IO流

    一.文件的基本操作:打开.读取.关闭 # 绝对路径 # f = open(r'G:\Envs\Bilitest\笔记\test.txt', 'r') # 相对路径 f = open(r'test.tx ...

  7. Java IO流学习总结(一)—— IO流分类和常用IO流汇总

    Java IO流学习总结(一)-- IO流分类和常用IO流汇总 IO流的分类: - 按流向分类:输入流.输出流 - 按操作对象分类:字节流.字符流 - 按功能分类:节点流.处理流 IO流的设计模式为装 ...

  8. java字节流读取文件_字节流读取文件 java的几种IO流读取文件方式

    java字节流怎么读取数据 字节流读取数据例子如下: import java.io.File;import java.io.FileInputStream;import java.io.FileNot ...

  9. Java 文件操作(File类)

    在Java中,文件操作和流操作经常结合在一起进行,其中,文件操作主要是针对一个文件的增删改查和重命名,不涉及一个文件的内容的更改,关于具体文件内容的操作属于流操作的范畴.这里,先对文件操作进行的一些方 ...

  10. 打怪升级之小白的大数据之旅(二十五)<Java面向对象进阶之IO流三 其他常见流>

    打怪升级之小白的大数据之旅(二十五) Java面向对象进阶之IO流三 其他常见流 上次回顾 上一章,我们学习了常用的字节流与字符流,本章,我会将其他的一些常见的流进行分享,IO流很多,我介绍不完,就挑 ...

最新文章

  1. 谈一谈算法工程师的落地能力
  2. js一些稀奇古怪的写法-带你装逼带你飞
  3. 适用于 macOS 下 2K 显示器开启 HiDPI 的简便解决方案
  4. 《JAVA与模式》之适配器模式
  5. C# 接口的作用浅谈举例(转)
  6. android RefBase、sp、wp
  7. java 对 mongoDB 分组统计操作 以及一些常用操作
  8. 计算机的网络技术的普及,计算机网络技术的普及与应用-网络技术论文-计算机论文(7页)-原创力文档...
  9. java jnlp 靠谱吗_获取当前的JNLP信息
  10. 详细整理Spring事务失效的具体场景及解决方案
  11. mysql 信号_MySQL
  12. C语言二级考试都是从题库抽取吗,c語言二級考試題庫_全國計算機等級考試二級C語言的考試題目都是從《C語言題庫》裡面抽取的題目嗎_淘題吧...
  13. 数据之路 - Python爬虫 - Json模块与JsonPath
  14. Makefile 教程(超详细)
  15. 全球15个免费卫星遥感数据源
  16. matlab 马氏距离 实例,MATLAB求马氏距离(Mahalanobis distance)
  17. libdmtx结合OpenCV识别DataMatrix二维码
  18. 2021--ICPC网络预选第一场--A Busiest Computing Nodes
  19. 301跳转有什么用?为什么要做301跳转?
  20. MSCap: Multi-Style Image Captioning with Unpaired Stylized Text

热门文章

  1. Android地图跑步项目,通过Weex 300行代码开发一款简易的跑步App
  2. windows下手动修改/修复启动项
  3. Android音量系统分析
  4. E20-591考试必备资料分享
  5. c语言 愚人节题目,愚人节整人题目大全
  6. java给服务器创建文件夹,java服务器创建文件夹
  7. Nginx 跳转到www二级域名,域名重定向配置方法
  8. SEO理论实践的10大误区
  9. AWK手册(ZYF译)
  10. 开篇一:基于ESP8266的电子墨水屏万年历