JAVA_IO流四大家族体系:

JAVA_IO流四大家族(1)

JAVA_IO流四大家族(2)

文章目录

  • JAVA_IO流四大家族体系:
    • JAVA_IO流四大家族(1)
    • JAVA_IO流四大家族(2)
  • FileReader
    • 实现代码
    • 查看一下读取出来的每个字符输出:
  • FileWriter
    • 直接写入字符串:
    • FileReader和FileWriter实现普通文本文件拷贝
      • 执行前:
      • 执行后:
  • BufferedReader
    • 第一种(直接塞入字符流,不需转换)
      • 简介
      • 构造方法
      • 代码
      • 常识普及
      • 注意点:
    • 第二种(塞入一个字节流,经过转换成为字符流)
      • 细看构造函数
      • 转换问题
      • 解决转换问题
      • 实现代码
      • 转换代码合并
      • 注意
  • BufferedWriter
    • 第一种(直接塞入字符流,不需转换)
    • 第二种(塞入一个字节流,经过转换成为字符流)
  • DataOutputStream
    • 简介
    • 实现代码
  • DataInputStream
    • 简介
    • 注意
    • 读取代码
  • PrintStream
    • 输出控制台如何实现的呢?
      • 注意:
    • 改变标准输出流的输出方向
    • 如何写一个日志

这里主要跟FileInputStream和FileOutputStream用法一样,这里就不在多多赘述,详情请看 JAVA_IO流四大家族,直接上代码

FileReader

实现代码

package FileReader;import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;public class FileReaderTest01 {public static void main(String[] args) {FileReader reader=null;try {//创建文件reader=new FileReader("src//tempfile");//开始读char [] chars=new char[4];//一次性读取4个字符int readCount=0;while((readCount=reader.read(chars))!=-1){System.out.print(new String(chars,0,readCount));}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(reader!=null){try {reader.close();} catch (IOException e) {e.printStackTrace();}}}}
}

查看一下读取出来的每个字符输出:

package FileReader;import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;public class FileReaderTest01 {public static void main(String[] args) {FileReader reader=null;try {//创建文件reader=new FileReader("src//tempfile");//准备一个char数组char [] chars=new char[4];//往char数组中读取reader.read(chars);for(char c:chars){System.out.println(c);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if(reader!=null){try {reader.close();} catch (IOException e) {e.printStackTrace();}}}}
}

FileWriter

FileWriter: 文件字符输出流,写只能输出普通文本

package FileWriter;import java.io.FileWriter;
import java.io.IOException;public class FileWriterTest {public static void main(String[] args) {FileWriter out=null;try {out=new FileWriter("src//tempfile");//开始写char[]chars={'中','国','最','棒'};out.write(chars);//刷新out.flush();} catch (IOException e) {e.printStackTrace();}finally {if(out!=null){try {out.close();} catch (IOException e) {e.printStackTrace();}}}}
}

直接写入字符串:

package FileWriter;import java.io.FileWriter;
import java.io.IOException;/*
* FileWriter:
* 文件字符输出流,写
* 只能输出普通文本
* */
public class FileWriterTest {public static void main(String[] args) {FileWriter out=null;try {out=new FileWriter("src//tempfile");//开始写out.write("我是一名大学生,冲冲冲");//刷新out.flush();} catch (IOException e) {e.printStackTrace();}finally {if(out!=null){try {out.close();} catch (IOException e) {e.printStackTrace();}}}}
}


换行:

out.write("\n");

FileReaderFileWriter只能没办法读声音,图片,视频等这些流媒体文件只能读普通文本,切记:Word不是普通文本

FileReader和FileWriter实现普通文本文件拷贝

java文件是普通文本文件(能用记事本编辑的都是普通文本文件),普通文本文件和后缀无关

package Filecopy01;import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class copy02 {public static void main(String[] args) {FileReader  in=null;FileWriter out=null;try {in=new FileReader("src//tempfile");out=new FileWriter("tempfile");//一边读一边写:char []chars=new char[1024*512];//1MBint readCount=0;while((readCount=in.read(chars))!=-1){out.write(chars,0,readCount);}//刷新out.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {if(in!=null){try {in.close();} catch (IOException e) {e.printStackTrace();}}if(out!=null){try {out.close();} catch (IOException e) {e.printStackTrace();}}}}
}

执行前:

执行后:


BufferedReader

第一种(直接塞入字符流,不需转换)

简介

BufferedReader:
带有缓冲区的字符输入流,使用这个流的时候不需要自定义char数组,或者说不需要自定义byte数组。自带缓冲区

构造方法

代码

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;public class BufferedReaderTest01 {public static void main(String[] args) throws Exception {FileReader reader=new FileReader("src//Filecopy01//copy02.java");BufferedReader br=new BufferedReader(reader);//读一行String line=null;while((line=br.readLine())!=null){//当读到最后一行的下一行时,返回值为null(即读不到数据就返回为null)System.out.println(line);//记住,读出来的时候,并没有把换行符读取出来}br.close();//关闭流//对于包装流来说,只需要关闭最外层流就行,里面的节点流会自动关闭(可以自己看源代码)}
}

 BufferedReader br=new BufferedReader(reader);

这个reader被bufferedReader当做一个成员变量



注意:因为Reader是一个抽象类,没办法实例化,所以只能挑选继承了Reader 的类来进行实例化

对于包装流来说,只需要关闭最外层流就行,里面的节点流会自动关闭(可以自己看下图)

常识普及

当一个流的构造方法中需要一个流的时候,这个被传进来的流叫做:节点流外部负责包装的这个流,叫做包装流还有一个名字叫做处理流,像当前这个程序来说:FileReader就是一个节点流,BufferedReader就是包装流/处理流

注意点:

  1. readline读出来的时候,并不会把换行符读取出来
  2. 当readline读到最后一行的下一行时,返回值为null(即读不到数据就返回为null

第二种(塞入一个字节流,经过转换成为字符流)

细看构造函数

转换问题

这个构造函数只能传入一个字符流,如果要想传入一个字节流的话,那么有没有办法呢?看下图:

解决转换问题

通过转换流转换,把inputStream字节流转换为字符流

 FileInputStream in=new FileInputStream("src//Filecopy01//Copy01.java");InputStreamReader reader=new InputStreamReader(in);
  1. 通过转换流转换(InputStreamReader将字节流转换成字符流)
  2. in是节点流,reader是包装流

InputStreamReader将字节流转换成字符流

 BufferedReader br=new BufferedReader(reader);
  1. 这个构造方法只能传一个字符流,不能传字节流
  2. reader是节点流,br是包装流
  3. 总结:节点流和包装流是相对而言的

实现代码

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;public class BufferedReaderTest02 {public static void main(String[] args) throws Exception {//字节流FileInputStream in=new FileInputStream("src//Filecopy01//Copy01.java");//通过转换流转换(InputStreamReader将字节流转换成字符流)//in是节点流,reader是包装流InputStreamReader reader=new InputStreamReader(in);BufferedReader br=new BufferedReader(reader);String line=null;while((line=br.readLine())!=null){System.out.println(line);}//关闭最外层br.close();}
}

转换代码合并

        BufferedReader br1=new BufferedReader(new InputStreamReader(new FileInputStream("src//Filecopy01//Copy01.java"))) ;

注意

关闭流时,只需要关闭最外层即可

      //关闭最外层br.close();

BufferedWriter

第一种(直接塞入字符流,不需转换)

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;public class BufferedWriterTest01 {public static void main(String[] args)throws  Exception {BufferedWriter out=new BufferedWriter(new FileWriter("copy",true));out.write("hello world");out.write("\n");out.write("hello kitty");//刷新out.flush();//关闭最外层out.close();}
}

第二种(塞入一个字节流,经过转换成为字符流)

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;public class BufferedWriterTest01 {public static void main(String[] args)throws  Exception {BufferedWriter out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("copy",true)));out.write("hello world");out.write("\n");out.write("hello kitty");//刷新out.flush();//关闭最外层out.close();}
}

DataOutputStream

简介

java.io.DataOutputStream:数据专属的流, 这个流可以将数据连同数据的类型一并写入文件, 注意:这个文件不是普通文本文档(用记事本打不开)


需要传入一个OutputStream,OutputStream是一个抽象类,我们没法new,只能去找继承它的子类,可以new一个FileOutputStream("data");

实现代码

import java.io.DataOutputStream;
import java.io.FileOutputStream;public class DataOutputStreamTest {public static void main(String[] args)throws  Exception {DataOutputStream dos=new DataOutputStream(new FileOutputStream("data"));//写数据byte b=100;short s=200;int i=300;long l=400L;float f=3.0f;double d=3.14;boolean sex=false;char a='e';dos.writeByte(b);//把数据以及数据的类型一并写入文件当中dos.writeShort(s);dos.writeInt(i);dos.writeLong(l);dos.writeFloat(f);dos.writeDouble(d);dos.writeBoolean(sex);dos.writeChar(a);//刷新dos.flush();//关闭dos.close();}
}



用记事本打开之后,显示如下

印证了:这个文件不是普通文本文档(用记事本打不开) ,读取出来的话,需要借助DataInputStream

DataInputStream

简介

java.io.DataOutputStream:数据专属的流,这个流可以将数据连同数据的类型一并写入文件,注意:这个文件不是普通文本文档(用记事本打不开)

注意

使用DataInputStream时,DataOutputStream这个流的存储顺序必须已知,否则没法读取,存储顺序如下:

 dos.writeByte(b);//把数据以及数据的类型一并写入文件当中dos.writeShort(s);dos.writeInt(i);dos.writeLong(l);dos.writeFloat(f);dos.writeDouble(d);dos.writeBoolean(sex);dos.writeChar(a);

读取代码

import java.io.DataInputStream;
import java.io.FileInputStream;public class DataInputStreamTest01 {public static void main(String[] args)throws Exception {DataInputStream dis=new DataInputStream(new FileInputStream("data"));//开始读byte b=dis.readByte();short s=dis.readShort();int i=dis.readInt();long l=dis.readLong();float f=dis.readFloat();double d=dis.readDouble();boolean sex=dis.readBoolean();char a=dis.readChar();dis.close();System.out.println(b);System.out.println(s);System.out.println(i);System.out.println(l);System.out.println(f);System.out.println(d);System.out.println(sex);System.out.println(a);}
}

PrintStream

输出控制台如何实现的呢?

联合写:

        System.out.println("hello biaobiao");

分开写:

        PrintStream ps=System.out;ps.println("hello liu");ps.println("hello world");

注意:

标准输出流不需要手动close

改变标准输出流的输出方向

import java.io.FileOutputStream;
import java.io.PrintStream;public class PrintStreamTest01 {public static void main(String[] args)throws Exception {//标准输出流不再指向控制台,指向“log”文件PrintStream psm=new PrintStream(new FileOutputStream("log"));//修改输出方向,将输出方向修改到log文件System.setOut(psm);//再输出System.out.println("hello world66666666");}
}

如何写一个日志

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;public class LogUtil {public static void log(String msg){/*
* 记录日志的方法
* */try {//指向一个日志文件PrintStream out =new PrintStream(new FileOutputStream("log.txt",true));//改变输出方向System.setOut(out);//日期当前时间Date nowTime=new Date();SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");String strTime=sdf.format(nowTime);System.out.println(strTime+":"+msg);} catch (FileNotFoundException e) {e.printStackTrace();}}
}

main方法:

public class LogTest {public static void main(String[] args) {//测试工具类是否好用LogUtil.log("调用了System类的gc()方法,建议启动垃圾回收");LogUtil.log("调用了UserService的dosome()方法");LogUtil.log("用户尝试进行登录,验证失败");}
}

JAVA_IO流四大家族(2)相关推荐

  1. JAVA_IO流四大家族(1)

    JAVA_IO四大家族体系: JAVA_IO流四大家族(1) JAVA_IO流四大家族(2) 文章目录 JAVA_IO四大家族体系: JAVA_IO流四大家族(1) JAVA_IO流四大家族(2) 常 ...

  2. Java IO流的“四大家族”

    1. IO流的概述 理论部分 简单概括就是文件的输入与输出. 磁盘中文件的东西放到内存中的这个过程称为输入(Input)或称读(read),由于这个输入的过程会有数据的流动,就有了输入流(InputS ...

  3. 《CSS世界》笔记二:盒模型四大家族

    上一篇:<CSS世界>笔记一:流/元素/尺寸 下一篇:<CSS世界>笔记三:内联元素与对齐 写在前面 在读<CSS世界>第四章之前,粗浅的认为盒模型无非是margi ...

  4. NoSQL数据库的四大家族

    NoSQL,泛指非关系型的数据库,全称Not Only SQL,意即"不仅仅是SQL". NoSQL数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑战,尤其是大数据应用难 ...

  5. 【图文解析】带你看清全球机器人四大家族现状,四家企业瓜分中国57%、全球50%的市场份额...

    来源:前瞻经济学研究院 摘要:工业机器人是智能制造业最具代表性的装备. 工业机器人是智能制造业最具代表性的装备.工业机器人集精密化.柔性化.智能化.软件应用开发等先进制造技术于一体,通过对过程实施检测 ...

  6. 东莞厚街工业机器人展会_工业机器人四大家族齐聚!东莞将在12月举办智博会...

    "这一届广东国际机器人及智能装备博览会"(简称"智博会")比上届有所缩小,原因有两个,第一是智博会调整策略聚焦高端发展,第二是因疫情影响,海外参展商参展难度加大 ...

  7. Java_IO流(精讲)包含练习题及答案

    Java_IO流(精讲)包含练习题及答案--建议收藏 包含常用的所有属性与函数的示例,并且对[字符流][字节流]分别做的[读写]操作示例. 目录 1.Java Io流的概念 按照流的流向分,可以分为输 ...

  8. 安川g7接线端子图_ABB、KUKA、FANUC、安川四大家族机器人安全回路小结

    很多新人在机器人安装调试的时候不知道如何下手,在此个人分享一下机器人安装调试的流程如下: 一个机器人项目的安装调试是否能顺利的通过客户验收,机器人的安全回路非常重要. 在此分享下ABB.KUKA.FA ...

  9. 盘点机器人四大家族——KUKA机器人

    此为临时链接,仅用于预览,将在短期内失效.关闭 盘点机器人四大家族--KUKA机器人 特盖德智能装备 特盖德机器人租赁 今天 德国KUKA机器人(库卡)是Johann Josef Keller和Jak ...

最新文章

  1. python读取excel某一行-Python 读取csv的某行
  2. PMWiki安装教程
  3. Cartographer安装
  4. VTK:图片之ImageFFT
  5. Spring4:没有默认构造函数的基于CGLIB的代理类
  6. solid测序列原理_SOLID原理简介
  7. 选择适合自己的 OLAP 引擎,干货
  8. web前端的主要学习什么,2020年还有前途吗?一般工资是多少?
  9. 获得一个日期在当周是否有节日并返回日期
  10. MapReduce 规划 系列十 采用HashPartitioner调整Reducer计算负荷
  11. 现代软件工程_团队项目_阿尔法阶段_第二次会议记录_2017.11.13
  12. 联想台式主机拆机教程_联想台式电脑主机怎么拆 联想b5040一体机拆机
  13. 消费金融公司可开展哪些业务类型?
  14. Remote使用出现的问题及解决办法
  15. 20吉大计算机/软件考研经验贴!
  16. JVM垃圾回收——三色标记法
  17. python如何读取excel表中的日期与时间
  18. Android移动开发:第一章Android系统概述
  19. vue图片压缩不失真_vue图片压缩(不失真)
  20. gateway 内存溢出问题_内存溢出和内存泄漏、产生原因以及解决方案

热门文章

  1. python处理文本格式_python linecache 处理固定格式文本数据的方法
  2. 台湾高校首创气体灭火数位实境教育馆
  3. 成功解决ValueError: Cannot feed value of shape (1, 10, 4) for Tensor Placeholder:0 , which has shape
  4. 成功解决VMware虚拟机中的please remove the installation medium then press enter
  5. Py之lightgbm:lightgbm的简介、安装、使用方法之详细攻略
  6. Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(Listbox/Scrollbar)
  7. Native C++ _isnan()函数的应用
  8. Several ports (8005, 80, 8009) required by Tomcat v6.0 Server at localhost are already in use
  9. win10添加新用户
  10. 【开源】接口管理平台eoLinker AMS 开源版3.1.5同步线上版!免费增加大量功能!...