菜鸟之路Day15一一IO流(一)
- IT业界
- 2025-09-03 07:39:01

菜鸟之路Day15一一IO流(一)
作者:blue
时间:2025.2.8
文章目录 菜鸟之路Day15一一IO流(一)0.概述1.初识IO流1.1.什么是IO流?1.2.IO流的作用1.3.IO流的分类 2.IO流的体系结构3.字节输出流的基本用法4.字节输入流的基本用法5.异常处理(了解即可)6.字符集7.字符输入流7.1FileReader7.2FileWriter 8.IO流综合练习8.1练习一8.2练习二8.3练习三 0.概述文章内容,学习自黑马程序员BV1yW4y1Y7Ms
1.初识IO流 1.1.什么是IO流?答:存储和读取数据的解决方案
1.2.IO流的作用答:用于读写数据(本地文件,网络)
1.3.IO流的分类按流的方向分
按操作文件的类型分
2.IO流的体系结构 3.字节输出流的基本用法FileOutputStream :操作本地文件的字节输出流,可以把程序中的数据写到本地文件中
书写步骤:
①创建字节输出流对象
细节1:参数是字符串表示的路径或者File对象都是可以的
细节2:如果文件不存在会创建一个新的文件,但是要保证父级路径是存在的
细节3:如果文件已经存在,则会清空文件
②写数据
细节:write方法的参数是整数,但是实际上写到本地文件中的是整数在ASCII上对应的字符
③释放资源
细节:每次使用完流之后都要释放资源
package IOStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileOutputStreamDemo1 { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("src\\IOStream\\a.txt");//①创建字节输出流对象 fos.write(97);//②写数据 fos.close();//③释放资源 } }写出数据的三种方式
public class FileOutputStreamDemo2 { public static void main(String[] args) throws IOException { /* * void write(int b) 一次写一个字节数据 * void write(byte[] b) 一次写一个字节数组数据 * * void write(byte[] b,int off,int len) 一次写一个字节数组的部分数据 * 参数一:数组 * 参数二:起始索引 0 * 参数三:个数 3 * */ FileOutputStream fos = new FileOutputStream("src\\IOStream\\a.txt"); byte[] b = {97,98,99,100,101}; fos.write(b,1,2); } }换行和续写
public class FileOutputStreamDemo3 { public static void main(String[] args) throws IOException { /* * 换行符: * windows:\r\n * Linux:\n * Mac:\r * 续写: * 打开续写开关即可,即创建对象的第二个参数 * 默认是false表示关闭,创建对象会清空文件 * 传递true,表示打开续写,此时创建对象不会清空文件 * */ FileOutputStream fos = new FileOutputStream("src\\IOStream\\a.txt",true); String str1 = "kcbdkdvkdkjxkjdkj"; String str2 = "\r\n"; String str3 = "666"; byte[] b1 = str1.getBytes(); byte[] b2 = str2.getBytes(); byte[] b3 = str3.getBytes(); fos.write(b1); fos.write(b2); fos.write(b3); } } 4.字节输入流的基本用法FileInputStream:操作本地文件的字节输入流,可以把本地文件中的数据读取到程序中来
书写步骤:
①创建字节输入流对象
细节1:如果文件不存在,就直接报错
②读数据
细节1:一次读一个字节,读出来的数据在ASCII上对应的数字
细节2:读到文件末尾了,read方法返回-1
③释放资源
细节:每次使用完流之后都要释放资源
public class FileInputStreamDemo1 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("src\\IOStream\\a.txt"); int ans = fis.read(); while(ans!=-1){ System.out.print((char)ans); ans = fis.read(); } fis.close();//释放资源 } }文件拷贝的基本代码
思想:边读边写
public class FileInputStreamDemo2 { public static void main(String[] args) throws IOException { //1.创建对象 FileInputStream fis = new FileInputStream("C:\\Users\\zhangtenlan\\Desktop\\abc.txt"); FileOutputStream fos = new FileOutputStream("src\\IOStream\\ans.txt"); //2.拷贝 //核心思想:边读边写 int b; while((b=fis.read())!=-1) { fos.write(b); } //3.释放资源 //规则:先开的最后关闭 fos.close(); fis.close(); } }以上方法在拷贝大文件的时候速度非常的慢,原因就在于,每次只能读取一个字节的数据,我们可以用read方法的一个重载来一次性读取多个数据。
public int read(byte[] buffer) //一次读一个字节数组数据 //注意:一次读一个字节数组的数据,每次读取会尽可能把数组装满 //长度尽量设置为1024的整数倍,不要太大,像1024*1024*5例子:
public class FileInputStreamDemo3 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("src\\IOStream\\ans.txt"); byte[] b = new byte[2]; int len; while((len=fis.read(b))!=-1){ //当读到的长度为-1时代表读完了 System.out.print(new String(b,0,len)); } fis.close(); } }拷贝大文件的写法
public class FileInputStreamDemocratic { public static void main(String[] args) throws IOException { long start = System.currentTimeMillis();//记录开始时间 //1.创建对象 FileInputStream fis = new FileInputStream("大文件"); FileOutputStream fos = new FileOutputStream("目标位置"); //2.拷贝 int len; byte[] bytes = new byte[1024*1024*5]; while((len=fis.read(bytes))!=-1){ fos.write(bytes,0,len); } //3.释放资源 fos.close(); fis.close(); long end = System.currentTimeMillis();//记录结束时间 System.out.println(end-start);//看看程序运行花了多少时间 } } 5.异常处理(了解即可) try{ }catch(IOException e){ }finally{ //finally中代码一定执行,除非JVM退出 //所以释放资源的代码写到这里再合适不过 } 6.字符集1.在计算机中,任意数据都是以二进制的形式来存储的
2.计算机中最小的存储单元是一个字节
3.ASCII字符集中,一个英文占一个字节
4.简体中文版Windows,默认使用GBK字符集
5.GBK字符集完全兼容ASCII字符集
注意:一个英文占一个字节,二进制第一位是0
一个中文占两个字节,二进制高位字节的第一位是1
6.Unicode字符集的UTF-8编码格式
注意:一个英文占一个字节,二进制第一位是0,转成十进制是正数
一个中文占三个字节,二进制第一位是1,第一个字节转成十进制是负数
为什么会有乱码?
原因1:读取数据时未读完整个汉字
原因2:编码和解码时的方式不统一
Java中的解码和编码:
public class FileInputStreamDemo5 { public static void main(String[] args) throws UnsupportedEncodingException { /* * java中的编码方法 * public byte[] getBytes() 使用默认方式进行编码UTF-8 * public byte[] getBytes(String charsetName) 使用指定方式进行编码 * * java中的解码方法 * String(byte[] bytes) 使用默认方式进行解码UTF-8 * String(byte[] bytes,String charsetName) 使用指定方式进行解码 * */ String str = "你好"; byte[] res1 = str.getBytes(); System.out.println(Arrays.toString(res1)); System.out.println("========================================================================="); byte[] res2 = str.getBytes("GBK"); System.out.println(Arrays.toString(res2)); System.out.println("========================================================================="); String ans1 = new String(res1); System.out.println(ans1); System.out.println("========================================================================="); String ans2 = new String(res2,"GBK"); System.out.println(ans2); } } 7.字符输入流字符流
字符流 = 字节流+字符集
特点
输入流:一次读一个字节,遇到中文时,一次读多个字节(所以适合处理中文)
输出流:底层会把数据按照指定的编码方式进行编码,变成字节再写到文件中
使用场景
对于纯文本文件进行读写操作
书写步骤
①创建字符输入流对象
②读取数据
注意:细节1:按字节进行读取,遇到中文,一次读多个字节,读取后解码,返回一个整数
细节2:读到文件末尾了,read方法返回-1
③释放资源
7.1FileReader public class IOTestOne { public static void main(String[] args) throws IOException { /* * 第一步:创建对象 * public FileReader(File file) 创建字符输入流关联本地文件 * public FileReader(String pathname) * * 第二步:读取数据 * public int read() 读取数据,读到末尾返回-1 * public int read(char[] buffer) 读取多个数据,读到末尾返回-1 * * 第三步:释放资源 * public void close 释放资源 * */ FileReader fr = new FileReader("src\\IOTest\\a.txt"); int ch; while((ch=fr.read())!=-1){ System.out.print((char)ch); } fr.close(); } } /* 有参的read FileReader fr = new FileReader("src\\IOTest\\a.txt"); char[] chars = new char[2]; int len; while((len=fr.read(chars))!=-1){//有参read返回值是 System.out.print(new String(chars,0,len)); } r.close(); */ 7.2FileWriter①创建字符输出流对象
细节1:参数是字符串表示的路径或者是File对象都可以
细节2:如果文件不存在会创建一个新的文件,但是要保证父级路径是存在的
细节3:如果文件已经存在,则会清空文件,如果不想清空可以打开续写开关
②写数据
细节:如果write方法的参数是整数,但是实际上写到本地文件中的是整数在字符集上对应的字符
③释放资源
细节:每次使用完流之后都要释放资源
public class IOTestTwo { public static void main(String[] args) { /* * 第一步:创建对象 * public FileWriter(File file) * public FileWriter(String pathname) * public FileWriter(File file,boolean append) 续写开关 * public FileWriter(String pathname,boolean append) * * 第二步:读取数据 * void write(int c) * void write(String str) * void write(String str,int off,int len) * void write(char[] cbuf) * void write(char[] cbuf,int off,int len) * * 第三步:释放资源 * public void close() * */ } } 8.IO流综合练习字节流和字符流的使用场景
字节流:拷贝任意类型的文件
字符流:读取纯文本文件中的数据,往纯文本文件中写出数据
8.1练习一拷贝文件夹,考虑子文件夹
public class Test1 { public static void main(String[] args) throws IOException { File src = new File("C:\\Users\\zhangtenlan\\Desktop\\src");//数据源 File dest = new File("C:\\Users\\zhangtenlan\\Desktop\\dest");//目的地 copydir(src,dest); } private static void copydir(File src, File dest) throws IOException { dest.mkdirs(); //1.进入数据源 File[] arr = src.listFiles(); //2.遍历数据源 for(File file:arr){ //3.如果是文件,则拷贝 if(file.isFile()) { FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(new File(dest,file.getName())); byte[] bytes = new byte[1024]; int len; //边读边写 while((len= fis.read(bytes))!=-1){ fos.write(bytes,0,len); } fos.close(); fis.close(); } //4.如果是文件夹,则递归 else { copydir(file,new File(dest,file.getName())); } } } } 8.2练习二文件加密
为了保证文件的安全性,就需要对原始文件进行加密存储,再使用的时候再对其进行解密处理。
加密原理:
对原始文件中的每一个字节数据进行更改,然后将更改以后的数据存储到新文件中
解密原理:
读取加密之后的文件,按照加密规则反向操作,变成原始文件
//完成这个功能,我们可以利用异或的原理 //加密,我们可以令每个字节都异或上一个数字 public class Test2 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("src\\yuan.png"); FileOutputStream fos = new FileOutputStream("src\\jiami"); int b; while((b=fis.read())!=-1){ fos.write(b^5); } fos.close(); fis.close(); } } //解密,我们取原来加密过的图片,再次异或上这个数字 public class Test2 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("src\\jiami"); FileOutputStream fos = new FileOutputStream("src\\jiemi.png"); int b; while((b=fis.read())!=-1){ fos.write(b^5); } fos.close(); fis.close(); } } 8.3练习三文本文件中有以下数据:2-1-9-4-7-8
将文本中的数据进行排序,变成以下数据:1-2-4-7-8-9
public class Test3 { public static void main(String[] args) throws IOException { //1.读取数据 //纯文本文件,可以使用字符流来操作 FileReader fr = new FileReader("src\\a.txt"); StringBuilder sb = new StringBuilder(); int b; while((b=fr.read())!=-1){ sb.append((char)b); } fr.close(); //2.排序 ArrayList<Integer> list = new ArrayList<>(); String[] arr = sb.toString().split("-");//分离- for (String s : arr) {//将数据转为整数类型,为排序做准备 list.add(Integer.parseInt(s)); } Arrays.sort(arr);//排序 //3.写回 FileWriter fw = new FileWriter("src\\b.txt"); for (int i = 0; i < arr.length; i++) { if(i==arr.length-1) { fw.write(arr[i]+""); } else { fw.write(arr[i]+"-"); } } fw.close(); } }菜鸟之路Day15一一IO流(一)由讯客互联IT业界栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“菜鸟之路Day15一一IO流(一)”