当前位置: 首页 > news >正文

Day24-C:\Users\Lenovo\Desktop\note\code\JavaSE\Basic\src\com\InOut

字符流

适合读写文本文件的内容

FileReader

把文件(磁盘或网络)中的数据以字符的形式读取到内存中

image-20251025192439555

package Basic.src.com.InOut.FileReaderWriter;import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;public class FileReaderTest1 {public static void main(String[] args) throws Exception {try (//创建一个文件字符输入流管道与源文件接通Reader fr = new FileReader("Basic\\src\\com\\InOut\\FileReaderWriter\\itheima01.txt");){
//            //2、读取文本文件的内容
//            int c;
//            while ((c = fr.read()) != -1) {//read返回的是int
//                System.out.print((char)c);
//            }//3、每次读取多个字符char[] buffer = new char[3];int len;while((len = fr.read(buffer))!= -1) {String str = new String(buffer, 0, len);System.out.print(str);}//性能是比较不错的!} catch (Exception e) {e.printStackTrace();;}}
}

FileWriter

字符输出流写出数据之后,必须刷新流或者关闭流,写出去的数据才能生效

image-20251027144838330

package Basic.src.com.InOut.FileReaderWriter;import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;public class FileWriterTest2 {public static void main(String[] args) throws IOException {try (//0、创建一个文字字符输出流管道与目标文件接通//Writer fw = new FileWriter("Basic/src/itheima02out.txt");//是覆盖管道Writer fw = new FileWriter("Basic/src/itheima02out.txt",true);//是追加管道){//1、写一个字符出去fw.write('a');fw.write(97);fw.write('磊');//写一个字符出去fw.write("\r\n");//换行//2、写一个字符串出去fw.write("我爱你中国abc");fw.write("\r\n");//换行//3、写字符串的一部分出去fw.write("我爱你中国abc",0,5);fw.write("\r\n");//换行//4、写一个字符数组出去char[] buffer = {'黑','马','a','b','c'};fw.write(buffer);fw.write("\r\n");//换行//5、写字符数组的一部分出去fw.write(buffer,0,2);} catch (IOException e) {throw new RuntimeException(e);}}
}
package Basic.src.com.InOut.FileReaderWriter;import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;public class FileWriterTest3 {public static void main(String[] args) throws IOException {Writer fw = new FileWriter("Basic/src/itheima03out.txt" );//字符输出流写出数据之后,必须刷新流或者关闭流,写出去的数据才能生效//字符输出流运行时会在内存里面新建一个缓冲区,直到数据拷贝结束后再将缓冲区内数据一次性写到文件中去// 但是仍然会创造这个文件出来//写字符数据出去fw.write('a');fw.write('b');fw.write('c');fw.write('d');fw.write("\r\n");fw.write("我爱你中国");fw.write("\r\n");fw.write("我爱你中国");//        fw.flush();//刷新流,刷新完会将缓冲区的数据同步到文件中
//        fw.write("张三");
//        fw.flush();fw.close();//关闭流包含刷新操作,但后续无法继续使用}
}

缓冲流

对原始流进行包装,以提高原始流读写数据的性能

image-20251027152113433

在内存区创建一个缓存区(8KB),直接从内存中读取

image-20251027152619201

package Basic.src.com.InOut.BufferStream;import java.io.*;public class BufferedInputStreamTest1 {public static void main(String[] args) throws IOException {try (InputStream is = new FileInputStream("Basic/src/itheima01.txt");//1.定义一个字节缓冲输入流InputStream bis = new BufferedInputStream(is);OutputStream os = new FileOutputStream("Basic/src/itheima01_bak.txt");//2.定义一个字节缓冲输出流包装原始的字节流OutputStream bos = new BufferedOutputStream(os,8192*2);){byte[] buffer = new byte[1024];int len;while ((len = bis.read(buffer)) != -1) {bos.write(buffer, 0, len);}System.out.println("复制完成");} catch (FileNotFoundException e) {throw new RuntimeException(e);}}
}

image-20251027161016236

package Basic.src.com.InOut.BufferStream;import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;public class BufferedReaderTest2 {public static void main(String[] args) throws IOException {try (Reader fr = new FileReader("Basic/src/itheima04.txt");//创建一个字符缓存输入流包装原本的输入流BufferedReader bfr  = new BufferedReader(fr);){
//            char[] buffer = new char[3];
//            int len;
//            while ((len = bfr.read(buffer)) != -1) {
//                System.out.print(new String(buffer,0,len));
//            }
//            System.out.println(bfr.readLine());
//            System.out.println(bfr.readLine());
//            System.out.println(bfr.readLine());
//            System.out.println(bfr.readLine());//nullString line;while ((line = bfr.readLine()) != null) {System.out.println(line);}} catch (IOException e) {e.printStackTrace();}}
}

image-20251027174104600

字节数组理论上越大越好,但是一定程度之后就不明显,且有可能占用太多内存,一般32KB是巅峰

如果代码编码和被读取的文件编码不一致,使用字符流读取文件时就会出现乱码

字符输入转换流

image-20251027182233278

image-20251027182420935

package Basic.src.com.InOut.BufferStream;import java.io.*;public class Test2 {public static void main(String[] args) throws IOException{try (//1.得到文件的原始字节流(GBK形式)InputStream is = new FileInputStream("Basic\\src\\itheima06.txt");//2.把原始的字节输入流按照指定的字符集编码转换成字符输入流Reader isr = new InputStreamReader(is,"GBK");//3.把字符输入流包装成缓冲字符输入流BufferedReader br = new BufferedReader(isr)){String line;while ((line = br.readLine()) != null){System.out.println(line);}} catch (FileNotFoundException e) {throw new RuntimeException(e);}}
}

image-20251027183555362

package Basic.src.com.InOut.BufferStream;import java.io.*;public class OutputStreamReaderTest3 {/*掌握字符输出转换流*/public static void main(String[] args) throws IOException {try (//指定写出去的字符编码//1.创建一个文件字节输出流OutputStream os = new FileOutputStream("Basic/src/itheima07Out.txt");//2.把原始的字节输出流按照指定的字符集编码输出转换流Writer osw = new OutputStreamWriter(os,"GBK");//把字符输出流再包装成缓冲字符流BufferedWriter bw = new BufferedWriter(osw);){bw.write("我是中国人abc");bw.write("我爱你中国123");} catch (FileNotFoundException e) {e.printStackTrace();}}
}

打印流

image-20251027184618095

打印流实现更方便更高效的方法,能实现打印啥出去就是啥出去

image-20251027184908160

package Basic.src.com.InOut.PrintStream;import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;public class PrintTest1 {public static void main(String[] args) throws UnsupportedEncodingException {try (//1.创建一个打印流管道PrintStream ps = new PrintStream("Basic/src/itheima08.txt","GBK");){ps.print(97);//打印后自动换行ps.print('a');ps.print("我爱你中国abc");ps.print(true);ps.print(99.5);ps.write(97);//a} catch (FileNotFoundException e) {throw new RuntimeException(e);}}
}

image-20251027190433070

重定向

package Basic.src.com.InOut.PrintStream;import java.io.FileNotFoundException;
import java.io.PrintStream;public class PrintTest2 {public static void main(String[] args) {try(PrintStream ps = new PrintStream("Basic/src/itheima09.txt");) {System.setOut(ps);//重定向System.out.println("老骥伏枥");System.out.println("志在千里");} catch (FileNotFoundException e) {e.printStackTrace();}}
}

数据流

image-20251027191047266

package Basic.src.com.InOut.DataStream;import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;public class DataOutputStreamTest1 {public static void main(String[] args) throws IOException {try (//1.创建一个数据输出流包装低级的字节输出流DataOutputStream dos = new DataOutputStream(new FileOutputStream("Basic/src/itheima10out.txt"));//输出的还包含了编码类型,没办法直接读取,可以通过DataInputStream读取){dos.writeInt(97);dos.writeDouble(99.5);dos.writeBoolean(true);dos.writeUTF("黑马程序员666!");} catch (FileNotFoundException e) {throw new RuntimeException(e);}}
}

image-20251027191938033

package Basic.src.com.InOut.DataStream;import java.io.*;/*
使用数据输入流读取特定类型的数据*/
public class DataInputStreamTest2 {public static void main(String[] args) throws IOException {try (DataInputStream dis = new DataInputStream(new FileInputStream("Basic/src/itheima10out.txt"));){int i = dis.readInt();System.out.println(i);double d = dis.readDouble();System.out.println(d);boolean b = dis.readBoolean();System.out.println(b);String rs = dis.readUTF();System.out.println(rs);} catch (FileNotFoundException e) {e.printStackTrace();}}
}

序列化流

对象序列化:把Java对象写入到文件当中去

对象反序列化:把文件的Java对象读出来

image-20251027193028817

package Basic.src.com.InOut.ObjectStream;import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;/*
目标:掌握对象字节输出流的使用:序列化对象*/
public class Test1ObjectOutputStream {public static void main(String[] args) {try(//2.创建一个对象字节输出流包装原始的字节输出流ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Basic/src/itheima11out.txt"));) {//1.创建一个java对象User u= new User("admin","张三",32,"6156321");//3.序列化对象到文件在去//对象如果需要序列化,必须实现序列化接口implements Serializable是序列化的前提//存入之后无法直接读取oos.writeObject(u);System.out.println("序列化对象成功!");} catch (IOException e) {throw new RuntimeException(e);}}
}

image-20251027194423879

package Basic.src.com.InOut.ObjectStream;import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;public class Test2ObjectInputStream {public static void main(String[] args) throws ClassNotFoundException {try(//1.创建一个对象字节输入流管道,包装低级的字节输入流与源文件接通ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Basic/src/itheima11out.txt"));) {User u = (User) ois.readObject();System.out.println(u);//private transient String password;//加上transient之后,这个变量将不参与序列化//User{loginName='admin', userName='张三', age=32, password='null'}} catch (IOException e) {throw new RuntimeException(e);}}
}

可以将多个对象存储到ArrayList中去,然后直接对集合进行序列化即可

ArrayList已经实现了序列化接口

框架

框架的形式:一般是把类、接口等编译为class形式,再压缩成一个jar结尾的文件发行出去。

image-20251027200159305

package Basic.src.com.libTest;import org.apache.commons.io.FileUtils;import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;public class CommonIOTest1 {public static void main(String[] args) throws IOException {FileUtils.copyFile(new File("Basic/src/itheima01.txt"),new File("Basic/src/a.txt"));FileUtils.copyDirectory(new File("F:\\硕士阶段\\Java\\测试文件夹"),new File("F:\\硕士阶段\\Java\\测试文件夹(存储用)"));FileUtils.deleteDirectory(new File("F:\\硕士阶段\\Java\\测试文件夹()存储用"));//可以删除非空文件夹//Files.copy(Path.of("Basic\\src\\itheima01.txt"),Path.of("Basic\\src\\b.txt"))在java11中出现//System.out.println(Files.readString(Path.of("C:\\Users\\Lenovo\\Desktop\\note\\code\\JavaSE\\Basic\\src\\itheima01.txt")));//也是java11}
}
http://www.hskmm.com/?act=detail&tid=40142

相关文章:

  • noi2.0下vscode快速配置指北 - Gon
  • 【通讯协议】IIC
  • Robot Queries
  • TCP/IP协议概述
  • 102302136 林伟杰 数据采集与融合作业1
  • 爆零记
  • DataGrip2022导入和导出sql文件
  • 【CI130x 离在线】如何运行 curl 脚本
  • 日总结 18
  • 一场比赛
  • 新东方第三节课名言作文
  • 【性能优化必看】CPU耗时飙高?GC频繁停顿?一文教你快速定位!​
  • 十月阅读_3
  • 学校协同云盘怎么选?2025年10大热门教育网盘推荐与对比
  • GPU集群之间的交互
  • Java并发编程基础:从线程管理到高并发应用实践
  • 102302115方朴第一次作业
  • CF1267G Game Relics
  • 中考_体育
  • python爬取京东评论 -
  • C++ STL
  • 20232422 2025-2026-1 《网络与系统攻防技术》实验三实验报告
  • 10.18 CSP-S 模拟赛
  • 20232404 2025-2026-1 《网络与系统攻防技术》实验三实验报告
  • 「WC2014-紫荆花之恋」题解
  • P14309 【MX-S8-T2】配对题解
  • 魔改sunpinyin
  • 20232308 2025-2026-1 《网络与系统攻防技术》实验三实验报告
  • [xp] GVim v9.0.494 (or thereabouts) is the last version known to support Windows XP.
  • 「CTSC2017-游戏」题解