目录

字节流和字符流

1. 流操作流程

2. 字节输出流(OutputStream)

2.1 输出方法

3. 自动关闭支持AutoCloseable-JDK1.7

4. 字节输入流(InputSream)

4.1 输入方法

5. 字符流

5.1 字符输出流(Writer)

5.2 字符输入流(Reader)

6. 字节流与字符流

7. 转换流


字节流和字符流

字节流(byte):InputStream、OutputStream

字符流(char):Reader、Writer

  • 字节流与字符流的区别:

字节流是原生的操作,而字符流是字节流经过处理后的操作。

  • 一般使用字节流——无论是网络传输还是磁盘的数据保存均以字节为单位,只有处理中文文本时才会用到字符流。

1. 流操作流程

  • 输入流:
  1. 准备文件
  2. 实例化FileInputSream对象
  3. read()读取数据
  4. 业务处理数据
  5. 关闭输入流-finally
  • 输出流:
  1. 准备文件,文件不存在自动创建,但是目录一定要存在
  2. 实例化FileOutputSream对象
  3. 业务处理数据
  4. write()输出数据
  5. 关闭输出流-finally
  • 业务处理

    读取数据 -> while( ){ },读到末尾返回-1

package com.qqy.io;import java.io.*;
import java.nio.file.Paths;/*** 将文件当作输入流,读取内容并处理,再输出到另一个文件中* Author: qqy*/
public class Test {public static void main(String[] args) {File inputFile = Paths.get("E:" , "JAVA" ,"input.txt").toFile();File outputFile = Paths.get("E:" , "JAVA" ,"output.txt").toFile();if(!outputFile.getParentFile().exists()){outputFile.getParentFile().mkdirs();}FileInputStream ins=null;FileOutputStream out=null;try {ins=new FileInputStream(inputFile);//输出流中的内容向后追加//out=new FileOutputStream(outputFile,true);//默认false,不追加out=new FileOutputStream(outputFile);//若读取结束,则返回-1int value=-1;//输入//从输入流读取数据的下一个字节while((value=ins.read())!=-1){//将大写变为小写value= value +32;//输出out.write(value);}} catch (IOException e) {e.printStackTrace();}finally{if(ins!=null) {//关闭数据流try {ins.close();} catch (IOException e) {e.printStackTrace();}}if(out!=null) {try {out.close();} catch (IOException e) {e.printStackTrace();}}}}
}
  • I/O操作属于于资源处理,所有的资源处理操作(IO操作、数据库操作、网络)使用后必须要进行关闭。

2. 字节输出流(OutputStream)

public abstract class OutputStream implements Closeable, Flushable
//Closeable接口:
public void close() throws IOException;
//Flushable接口:
public void flush() throws IOException;
  • 2.1 输出方法

//将给定的字节数组内容全部输出
public void write(byte b[]) throws IOException
//将部分字节数组内容输出
public void write(byte b[], int off, int len) throws IOException
//输出单个字节
public abstract void write(int b) throws IOException;
  • 要进行文件内容的输出,使用FileOutputStream()
//文件内容覆盖
public FileOutputStream(File file) throws FileNotFoundException
//文件内容追加
public FileOutputStream(File file,boolean append) throws FileNotFoundException 
  • 当使用FileOutputStream进行文件内容输出时,只要文件父路径存在,FileOutputStream会自动创建文件
import java.io.*;/*** 字节输出流* Author: qqy*/
public class Test {public static void main(String[] args) throws IOException {//1.取得终端对象File file = new File("E:"+File.separator+"JAVA"+File.separator+"Test.txt");//只要文件父路径存在,FileOutputStream会自动创建文件//OutputStream为抽象方法,需要借用子类进行实例化OutputStream out =null;OutputStream out1 =null;//2.取得终端对象的输出流try {out = new FileOutputStream(file);//允许内容的追加out1 = new FileOutputStream(file,true);//3.进行数据的输出String msg="你好世界!!!\r\n";//当出现中文时,最好全部输出,避免出现乱码out.write(msg.getBytes(),0,6);  //你好out1.write(97); //a} catch (FileNotFoundException e) {e.printStackTrace();}catch (IOException e) {e.printStackTrace();}finally{//4.关闭流out.close();out1.close();}}
}

3. 自动关闭支持AutoCloseable-JDK1.7

  • 使用前提:结合try...catch...
/*** 自动关闭* Author: qqy*/
public class Test1 {public static void main(String[] args) {//try-with-resources //实现AutoCloseable接口后,对资源的一些释放、关闭,JVM可以自动调用close()//try(实例化对象的代码,实例化对象的类实现AutoCloseable接口)//实例化多个对象时,用分号隔开,最后一个不用加分号try(Msg msg1=new Msg()){//自动调用close()msg.print();  //normal method...   auto close...}catch (Exception e){}}
}class Msg implements AutoCloseable{@Overridepublic void close() throws Exception {System.out.println("auto close...");}public void print(){System.out.println("normal method...");}
}

4. 字节输入流(InputSream)

public abstract class InputStream implements Closeable
  • 4.1 输入方法 

//读取数据到字节数组b中
public int read(byte b[]) throws IOException
//读取单个字节
public int read() throws IOException 
  • 返回值:

返回b长度:读取数据大小>字节数组大小,返回字节数组大小

返回大于0但是小于b长度:读取数据大小<字节数组大小,返回真正读取个数

返回-1:数据读取完毕

import java.io.*;/*** 字节输入流和输出流的缓存方式读取和写入* Author: qqy*/
public class Test{public static void main(String[] args) throws IOException {//1.取得File对象File file = new File("E:" + File.separator + "JAVA" + File.separator + "input.txt");File destFile = new File("E:" + File.separator + "JAVA" + File.separator + "output.txt");//2.取得输入流、输出流InputStream in = null;OutputStream out = null;try {in = new FileInputStream(file);out = new FileOutputStream(destFile);} catch (FileNotFoundException e) {e.printStackTrace();}//3.读取文件内容、写入内容byte[] data = new byte[5];int value = -1;try {//从输入流读取数据的一些字节数,并将其存储到缓冲区data//每次循环,先开辟一个长度为5的数组,读取文件中从头开始的3个字节,放入该数组的第1个位置// value存放每次读的内容的长度,b是每次数组中存放的内容while ((value = in.read(data, 1, 3)) != -1) {System.out.println(value);    //3  1System.out.println(new String(data));  // ABC   DBCout.write(data);  //写入为ABCDBC//缓冲方式写数组的时候,偏移量为0,res为每一批读的数据的量out.write(data, 0, value);}} catch (IOException e) {e.printStackTrace();}//4.关闭流in.close();out.close();}}

5. 字符流

  • 5.1 字符输出流(Writer)

实现了Closeable接口,可以自动关闭

public abstract class Writer implements Appendable, Closeable, Flushable

除了参数为字符数组外,多了一个传入String对象的方法

public void write(String str) throws IOException
import java.io.*;/*** 字符输出流* Author: qqy*/
public class Test3 {public static void main(String[] args) throws IOException {//1.取得File对象File file = new File("E:"+File.separator+"JAVA"+File.separator+"Test.txt");//2.取得输出流Writer writer=new FileWriter(file);//3.写入数据String str="你好 Bonjour !!";writer.write(str);writer.write(new char[]{'a','b','c','\n'});writer.write(new char[]{'A','B','C'},2,1);//4.关闭流writer.close();}
}
  • 5.2 字符输入流(Reader)

Reader类中没有方法可以直接读取字符串,只能通过字符数组来读取

import java.io.*;/*** 字符输入流* Author: qqy*/
public class Test4 {public static void main(String[] args) throws IOException {//1.取得File对象File file = new File("E:"+File.separator+"JAVA"+File.separator+"Test.txt");//2.取得输入流Reader reader= new FileReader(file);//3.读取数据char[] data=new char[1024];int result=reader.read(data);System.out.println(result);System.out.println(new String(data,0,result));//4.关闭流reader.close();}
}

6. 字节流与字符流

  • 开发中,优先考虑字节流,只有处理中文时才考虑使用字符流。
  • 所有字符流操作,无论是写入还是输出,数据都先保存在缓存中——字符输出流不关闭,文件中无内容;字节输出流不关闭,文件中有内容。
  • 想要将字符输出流在文件中显示,要么正常关闭—out.close(),要么强制清空缓存区—out.flush()

练习:文件复制(字节流)

import java.io.*;
import java.nio.file.Paths;/*** 文件复制* Author: qqy*/
public class Test6 {public static void main(String[] args) throws IOException {String src="";String dest="";long start=System.currentTimeMillis(); //毫秒数copy(src,dest);long end=System.currentTimeMillis();System.out.println(end-start/1000+"s");  //秒数}public static void copy(String srcFilePath,String destFilePath){//参数校验if (srcFilePath==null||srcFilePath.isEmpty()) {throw new IllegalArgumentException("srcFilePath not null/empty!");}if (destFilePath==null||destFilePath.isEmpty()) {throw new IllegalArgumentException("destFilePath not null/empty!");}File srcFile =Paths.get(srcFilePath).toFile();File destFile =Paths.get(destFilePath).toFile();//文件校验以及准备工作if(!srcFile.exists()||!srcFile.isFile()){throw new IllegalArgumentException("srcFilePath not exists or not file!")}File parentFile= destFile.getParentFile();if(!parentFile.exists()){if(!parentFile.mkdirs()){throw new RuntimeException("can't create"+parentFile.getAbsolutePath()+"directory");}}//文件复制try(FileInputStream in=new FileInputStream(srcFile);FileOutputStream out =new FileOutputStream(destFile)){//缓冲数组//引入缓冲区byte[] buff = new byte[1024];  //1k,2k,4k,8kint len=-1;//一次只读一个字节,再输出一个字节while((len=in.read(buff))!=-1){//为了避免数据读取不全,使用下列方式进行写入out.write(buff,0,len);}}catch(IOException e){System.out.println(e.getMessage());}}
}

7. 转换流

字节流—>字符流,用于将底层的字节流转为字符流供子类使用

OutputStreamWriter:字节输出流—>字符输出流

InputStreamReader:字节输入流—>字符输入流

import java.io.*;/*** 转换流* Author: qqy*/
public class Test7 {public static void main(String[] args) throws IOException {//1.取得文件对象File file = new File ("E:"+File.separator+"JAVA"+File.separator+"Test.txt");//2.取得输出流OutputStream outputStream=new FileOutputStream(file);//3.字节流变为字符流,注意编码格式OutputStreamWriter out=new OutputStreamWriter(outputStream);out.write("你好 Bonjour");//4.关闭流out.close();}
}

Java——I/O(字节流、字符流与转换流 )相关推荐

  1. 菜鸟学习笔记:Java提升篇5(IO流1——IO流的概念、字节流、字符流、缓冲流、转换流)

    菜鸟学习笔记:Java IO流1--IO流的概念.字节流.字符流.缓冲流.转换流 IO流的原理及概念 节点流 字节流 文件读取 文件写出 文件拷贝 文件夹拷贝 字符流 文件读取 文件写出 处理流 缓冲 ...

  2. 一文读懂Java中File类、字节流、字符流、转换流

    一文读懂Java中File类.字节流.字符流.转换流 第一章 递归:File类: 1.1:概述 java.io.File 类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作. ...

  3. Java 字节流、字符流和转换流之间的关系

    1. 字节流.字符流和转换流之间的关系 (1)字节流:读取字节到内存或将字节写入硬盘文件中. (2)字符流:将字节转换为字符(读入)或将字符转换为字节(写出). (3)转换流:将字节按照指定的编码格式 ...

  4. c语言将数据写入文件后乱码_html文件的下载,如何使用字节流,如何使用转换流...

    package com.jd.io;import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileO ...

  5. Java基础之——缓冲流、转换流、序列化流、打印流

    缓冲流.转换流.序列化流 1.缓冲流 见识一些更强大的流.比如能够高效读写的缓冲流,能够转换编码的转换流,能够持久化存储对象的序列化流等等.这些功能更为强大的流,都是在基本的流对象基础之上创建而来的, ...

  6. java 流 改变编码_Java-IO流之转换流的使用和编码与解码原理

    一.理论: 1.字符流和字节流区别是什么? 字符流=字节流+编码集,在实际读取的时候其实字符流还是按照字节来读取,但是会更具编码集进行查找编码集字典解析相应的字节,使得一次读取出一个字符: 2.什么是 ...

  7. 25.java中IO流的应用---缓冲流、转换流以及对象流过程(附讲解和练习)

    处理流之一:缓冲流  为了提高数据读写的速度, Java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组,缺省使用8192个字节(8Kb)的缓冲区.  缓冲流要" ...

  8. java(十)【属性集,缓冲流、转换流、序列化流】

    day10[缓冲流.转换流.序列化流] 今日目标 IO资源的处理. finnally释放资源 jdk 1.7开始的新技术 try-with-resources 缓冲流 提高字节流和字符流读写数据的性能 ...

  9. 【02】Java进阶:13-IO资源的处理、属性集、缓冲流、转换流、序列化、打印流、装饰设计模式、commons-io工具包

    day13[Properties类.缓冲流.转换流.序列化流.装饰者模式.commons-io工具包] 今日内容 IO异常处理 Properties类 缓冲流 转换流 序列化\反序列化流 打印流 装饰 ...

最新文章

  1. python数据去噪声_Logreduce:用Python和机器学习去除日志噪音
  2. java字面量和符号引用_java -- JVM的符号引用和直接引用
  3. 当退出python时是否释放全部内存_Python面试题:高级特性考察
  4. 基于裁判文书与犯罪案例文本挖掘项目
  5. Kickstart无人职守安装RHEL6.4
  6. 让FLASH背景透明-可运用于在网页内的FLASH内嵌入另一个网页
  7. Spring之RMI 远程方法调用 (Remote Method Invocation)
  8. Inpaint2021一款非常强大图片去水印神器
  9. CSS border-radius:50%和100%的区别
  10. php match_PHP8.0新功能之Match表达式的使用
  11. html页面悬浮提示框,js实现页面悬浮框
  12. 如何成为一名企业满意的UI设计师
  13. 小荷尖 - 发现更优质的企业服务
  14. Java自学教程!mysql环境变量配置好了之后怎么办
  15. win10右键闪退到桌面_win10打开控制面板总闪退弹回桌面怎么办
  16. 微信分享不显示右边缩略图
  17. macd的python代码同花顺_超牛MACD(代编写程序化交易模型)-同花顺公式 -程序化交易(CXH99.COM)...
  18. ubuntu 误使用dpkg --clear-selections修复
  19. 计算机重装系统的方法,电脑怎么刷机重装系统 电脑刷机重装系统的方法
  20. Rman配置DataGuard using Backup-based duplication with a target connection with filesystem

热门文章

  1. wangEditor富文本编辑器addRange(): The given range isn‘t in document解决方法
  2. android 华为荣耀v8不能上传视频,华为荣耀V8能拍摄1080p视频吗
  3. html单元格中的文字竖排显示文字居中,Word表格中为什么竖排文字不能居中?
  4. 多多自走棋改动_多多自走棋:重大更新,一直被玩家念叨的两个地方终于改了...
  5. 2021年熔化焊接与热切割考试题及熔化焊接与热切割最新解析
  6. Python提取pps文件中的音乐或视频
  7. 电梯ic卡信息服务器的线怎么接,电梯IC卡分层制器安装接线图.doc
  8. Tech Ed游记(一)
  9. 四柱汉诺塔 -- 动态规划求解最优移动次数
  10. 有关品牌微信APP营销中你需要懂得一些事。