IO-4

墨蓝 2022-07-29 10:53 212阅读 0赞

IO-4

01_IO流(序列流)

* 1.什么是序列流

  1. \*序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推.

* 2.使用方式

* 整合两个:SequenceInputStream(InputStream, InputStream)

  1. FileInputStreamfis1 = new FileInputStream("a.txt"); //创建输入流对象,关联a.txt
  2. FileInputStreamfis2 = new FileInputStream("b.txt"); //创建输入流对象,关联b.txt
  3. SequenceInputStreamsis = new SequenceInputStream(fis1, fis2); //将两个流整合成一个流!!!
  4. FileOutputStreamfos = new FileOutputStream("c.txt"); //创建输出流对象,关联c.txt
  5. intb;
  6. while((b= sis.read()) != -1) \{ //用整合后的读
  7. fos.write(b); //写到指定文件上
  8. \}
  9. sis.close(); //sis在关闭的时候,会将构造方法中传入的流对象也都关闭
  10. fos.close();

02_IO流(序列流整合多个)

* 整合多个:SequenceInputStream(Enumeration)

*

  1. FileInputStreamfis1 = new FileInputStream("a.txt"); //创建字节输入流对象,关联a.txt
  2. FileInputStreamfis2 = new FileInputStream("b.txt"); //创建字节输入流对象,关联b.txt
  3. FileInputStreamfis3 = new FileInputStream("c.txt"); //创建字节输入流对象,关联c.txt
  4. Vector<InputStream>v = new Vector<>(); //创建vector集合对象,FileInputStream也行
  5. v.add(fis1); /将流对象添加
  6. v.add(fis2);
  7. v.add(fis3);
  8. Enumeration<InputStream>en = v.elements(); //获取枚举引用
  9. SequenceInputStreamsis = new SequenceInputStream(en); //传递给SequenceInputStream构造,将枚举中的输入流整合成一个
  10. FileOutputStreamfos = new FileOutputStream("d.txt");
  11. intb;
  12. while((b= sis.read()) != -1) \{
  13. fos.write(b);
  14. \}
  15. sis.close();
  16. fos.close();

03_IO流(内存输出流*****)

* 1.什么是内存输出流

  1. \*该输出流可以向内存中写数据, 把内存当作一个缓冲区, 写出之后可以一次性获取出所有数据

*FileInputStream读取中文的时候出现了乱码解决方案:

  1. \*1,字符流读取
  2. \*2,ByteArrayOutputStream

* 2.使用方式

  1. \*创建对象: new ByteArrayOutputStream()
  2. \*写出数据: write(int), write(byte\[\])
  3. \*获取数据: toByteArray()
  4. \*
  5. FileInputStreamfis = new FileInputStream("a.txt");
  6. ByteArrayOutputStreambaos = new ByteArrayOutputStream(); //在内存中创建了可以增长的内存数组
  7. intb;
  8. while((b= fis.read()) != -1) \{
  9. baos.write(b);
  10. \}
  11. //byte\[\]newArr = baos.toByteArray(); //将内存缓冲区中所有的字节存储在newArr中
  12. //System.out.println(newString(newArr));
  13. System.out.println(baos);
  14. fis.close(); //baos不用关因为它是在内存中创建的,用完就释放了,而流之所以要关,是因为流相当于在内存和硬盘之间建立了一条管道!!!!!!!!

04_IO流

* 定义一个文件输入流,调用read(byte[]b)方法,将a.txt文件中的内容打印出来(byte数组大小限制为5)

* 分析:

  1. \* 1,reda(byte\[\] b)是字节输入流的方法,所以创建FileInputStream,关联a.txt
  2. \* 2,创建内存输出流,将读到的数据写到内存输出流中
  3. \* 3,创建字节数组,长度为5
  4. \* 4,将内存输出流的数据全部转换为字符串打印
  5. \* 5,关闭输入流
  6. publicstatic void main(String\[\] args) throws IOException \{
  7. //1,reda(byte\[\]b)是字节输入流的方法,创建FileInputStream,关联a.txt
  8. FileInputStreamfis = new FileInputStream("a.txt");
  9. //2,创建内存输出流,将读到的数据写到内存输出流中
  10. ByteArrayOutputStreambaos = new ByteArrayOutputStream();
  11. //3,创建字节数组,长度为5
  12. byte\[\]arr = new byte\[5\];
  13. intlen;
  14. while((len= fis.read(arr)) != -1) \{
  15. baos.write(arr,0, len);
  16. //System.out.println(newString(arr,0,len)); //会乱码!!!!!!
  17. \}
  18. //4,将内存输出流的数据全部转换为字符串打印
  19. System.out.println(baos); //即使没有调用,底层也会默认帮我们调用toString()方法
  20. //5,关闭输入流
  21. fis.close();
  22. \}

05_IO流(对象操作流ObjecOutputStream)

* 1.什么是对象操作流

  1. \*该流可以将一个对象写出, 或者读取一个对象到程序中. 也就是执行了序列化和反序列化的操作.
  2. \*序列化:将对象写到文件上,也就是我把这些数据按照某种规定写到文档上去,反序列化就是我把写过去的数据再翻译过来,翻译成对应的对象去使用了

*注:如果想执行序列化,对象类必须实现Serializable接口!!!!!!

* 2.使用方式

  1. \*写出: new ObjectOutputStream(OutputStream), writeObject()

public class Demo3_ObjectOutputStream {

  1. \* 将对象写出,序列化
  2. publicstatic void main(String\[\] args) throws IOException \{
  3. Personp1 = new Person("张三", 23);
  4. Personp2 = new Person("李四", 24);
  5. // FileOutputStream fos = newFileOutputStream("e.txt");
  6. // fos.write(p1);
  7. // FileWriter fw = newFileWriter("e.txt");
  8. // fw.write(p1);
  9. //无论是字节输出流,还是字符输出流都不能直接写出对象
  10. ObjectOutputStreamoos = new ObjectOutputStream(new FileOutputStream("e.txt"));//创建对象输出流,e,txt中的内容看不懂正常,只要系统能读出来就好!!!!!
  11. oos.writeObject(p1);
  12. oos.writeObject(p2);
  13. oos.close();
  14. \}

}

06_IO流(对象操作流ObjectInputStream)(

* 读取: newObjectInputStream(InputStream), readObject()

  1. \*
  2. publicclass Demo3\_ObjectInputStream \{
  3. /\*\*
  4. \* @param args
  5. \* @throws IOException
  6. \* @throws ClassNotFoundException
  7. \* @throws FileNotFoundException
  8. \* 读取对象,反序列化
  9. \*/
  10. publicstatic void main(String\[\] args) throws IOException, ClassNotFoundException \{
  11. ObjectInputStreamois = new ObjectInputStream(new FileInputStream("e.txt"));
  12. Personp1 = (Person) ois.readObject(); //转换数据类型!!!!!
  13. Personp2 = (Person) ois.readObject();
  14. System.out.println(p1);
  15. System.out.println(p2);
  16. ois.close();
  17. \}
  18. \}

07_IO流(对象操作流优化)

* 将对象存储在集合中写出

  1. Personp1 = new Person("张三", 23);
  2. Personp2 = new Person("李四", 24);
  3. Personp3 = new Person("马哥", 18);
  4. Personp4 = new Person("辉哥", 20);
  5. ArrayList<Person>list = new ArrayList<>();
  6. list.add(p1);
  7. list.add(p2);
  8. list.add(p3);
  9. list.add(p4);
  10. ObjectOutputStreamoos = new ObjectOutputStream(new FileOutputStream("f.txt"));
  11. oos.writeObject(list); //把整个集合对象一次写出
  12. oos.close();

* 读取到的是一个集合对象

  1. ObjectInputStreamois = new ObjectInputStream(new FileInputStream("f.txt"));
  2. ArrayList<Person>list = (ArrayList<Person>)ois.readObject(); //将集合对象一次读取,泛型在运行期会被擦除,索引运行期相当于没有泛型
  3. //想去掉黄色可以加注解 @SuppressWarnings("unchecked")
  4. for(Person person : list) \{
  5. //System.out.println(person); //遍历集合!!
  6. System.out.println(person.getName()+ "..." +person.getAge());
  7. \}
  8. ois.close();

08_IO流(加上id号)

* 注意

  1. \*要写出的对象必须实现Serializable接口才能被序列化
  2. \*不用必须加id号(加上就是可以在报错的时候看的更清晰一些,不加报错的时候会随机生成一长串id号)

09_IO流(打印流的概述和特点)

* PrintStream和PrintWriter分别是打印的字节流和字符流

* 1.什么是打印流

  1. \*该流可以很方便的将对象的toString()结果输出, 并且自动加上换行, 而且可以使用自动刷出的模式
  2. \*System.out就是一个PrintStream, 其默认向控制台输出信息,PrintStream打印的是字节流!!!
  3. System.out.println("aaa");
  4. PrintStreamps = System.out; //获取标注输出流
  5. ps.println(97); //其实底层用的是Integer.toString(x),将x转换为数字字符串打印97
  6. ps.write(97); //查找码表,找到对应的a并打印
  7. ps.println("xxx");
  8. ps.println(newPerson("张三", 23));
  9. Personp = null;
  10. ps.println(p); //如果是null,就返回null,如果不是null,就打印对象的toString()

* 2.使用方式

  1. \*打印: print(), println()
  2. \*自动刷出: PrintWriter(OutputStream out, boolean autoFlush, String encoding) 打印字符流!!!
  3. \*打印流只操作数据目的
  4. PrintWriterpw = new PrintWriter(new FileOutputStream("g.txt"), true);//true代表可以自动刷出!!!
  5. pw.write(97);
  6. pw.print("大家好");
  7. pw.println("你好"); //自动刷出,只针对的是println方法,对其他方法不管用,但如果将其放在最后位置,则都能刷出!!!
  8. pw.close();

10_IO流(标准输入输出流概述和输出语句)

* 1.什么是标准输入输出流(掌握)

  1. \*System.inInputStream, 标准输入流, 默认可以从键盘输入读取字节数据
  2. \*System.outPrintStream, 标准输出流, 默认可以向Console中输出字符和字节数据

* 2.修改标准输入输出流(了解)

  1. \*修改输入流: System.setIn(InputStream)
  2. \*修改输出流: System.setOut(PrintStream)
  3. \*
  4. System.setIn(newFileInputStream("a.txt")); //修改标准输入流
  5. System.setOut(newPrintStream("b.txt")); //修改标准输出流
  6. InputStreamin = System.in; //获取标准输入流
  7. PrintStreamps = System.out; //获取标准输出流
  8. intb;
  9. while((b= in.read()) != -1) \{ //从a.txt上读取数据
  10. ps.write(b); //将数据写到b.txt上
  11. \}
  12. in.close();
  13. ps.close();

*注:System.out.println();也是一个输出流,不用关,因为没有和硬盘上的文件产生关联的管道,只有产生了相关联的管道才关!!!

11_IO流(修改标准输入输出流拷贝图片)(了解) 开发中不推荐使用!!!

  1. System.setIn(newFileInputStream("IO图片.png")); //改变标准输入流
  2. System.setOut(newPrintStream("copy.png")); //改变标准输出流
  3. InputStreamis = System.in; //获取标准输入流
  4. PrintStreamps = System.out; //获取标准输出流
  5. intlen;
  6. byte\[\]arr = new byte\[1024 \* 8\];
  7. while((len= is.read(arr)) != -1) \{
  8. ps.write(arr,0, len);
  9. \}
  10. is.close();
  11. ps.close();

11_IO流(两种方式实现键盘录入)

* A:BufferedReader的readLine方法。

//InputStreamReader是转换流,是是字节到字符转换的桥梁,将System.in读取的字节数据转换成字符数据!!!这就相当于设计模式..对System.in进行包装

  1. \*BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  2. Stringline = br.readLine();
  3. System.out.println(line);
  4. br.close();

* B:Scanner (这种更好,功能更强大!!!)

  1. Scannersc = new Scanner(System.in);
  2. Stringline = sc.nextLine();
  3. System.out.println(line);
  4. sc.close();

12_IO流(随机访问流概述和读写数据)

* A:随机访问流概述

  1. \*RandomAccessFile概述
  2. 随机访问流可以在指定位置去写,指定位置去读
  3. \*RandomAccessFile类不属于流,是Object类的子类。但它融合了InputStreamOutputStream的功能。
  4. \*支持对随机访问文件的读取和写入。

* B:read(),write(),seek()//设置指针的方法

  1. RandomAccessFileraf = new RandomAccessFile("g.txt", "rw");
  2. //raf.write(97); 写到文件中的是a
  3. //intx = raf.read();
  4. //System.out.println(x); 输出97
  5. raf.seek(10); //在指定位置设置指针
  6. raf.write(98); //在10的位置写入b
  7. raf.close();

13_IO流(数据输入输出流)

* 1.什么是数据输入输出流

  1. \*DataInputStream, DataOutputStream可以按照基本数据类型大小读写数据(读写各种基本数据类型!!! )
  2. \*例如按Long大小写出一个数字, 写出时该数据占8字节. 读取的时候也可以按照Long类型读取, 一次读取8个字节.
  3. FileOutputStreamfos = new FileOutputStream("h.txt");
  4. fos.write(997);
  5. fos.write(998);
  6. //h.txt中会乱码,且只有两个字节,因为997不在-128--127之间,并且调用write时会自动砍掉前24位!!!
  7. fos.close();

* 2.使用方式

  1. \*DataOutputStream(OutputStream), writeInt(), writeLong()
  2. DataOutputStreamdos = new DataOutputStream(new FileOutputStream("b.txt"));
  3. dos.writeInt(997);
  4. dos.writeInt(998);
  5. dos.writeInt(999);
  6. dos.close();
  7. \*DataInputStream(InputStream), readInt(), readLong()
  8. DataInputStreamdis = new DataInputStream(new FileInputStream("b.txt"));
  9. intx = dis.readInt();
  10. inty = dis.readInt();
  11. intz = dis.readInt();
  12. System.out.println(x);
  13. System.out.println(y);
  14. System.out.println(z);
  15. dis.close();

14_IO流(Properties的概述和作为Map集合的使用)

* A:Properties的概述

  1. \*Properties 类表示了一个持久的属性集。
  2. \*Properties 可保存在流中或从流中加载。
  3. \*属性列表中每个键及其对应值都是一个字符串。

* B:案例演示,Properties作为Map集合的使用

15_IO流(Properties的特殊功能使用)

* A:Properties的特殊功能 是双列集合,在开发中当作配置文件存储一些信息

  1. \*public Object setProperty(String key,String value)
  2. \*public String getProperty(String key)
  3. \*public Enumeration<String> stringPropertyNames()

* B:案例演示

  1. \*Properties的特殊功能
  2. Propertiesprop = new Properties();
  3. prop.setProperty("name","张三");
  4. prop.setProperty("tel","18912345678");
  5. //System.out.println(prop);
  6. Enumeration<String>en = (Enumeration<String>) prop.propertyNames();
  7. while(en.hasMoreElements())\{
  8. Stringkey = en.nextElement(); //获取Properties中的每一个键
  9. Stringvalue = prop.getProperty(key); //根据键获取值
  10. System.out.println(key+ "="+ value);
  11. \}

16_IO流(Properties的load()和store()功能)

* A:Properties的load()读取和store()写出功能

* B:案例演示

  1. \*Propertiesload()和store()功能
  2. Propertiesprop = new Properties();
  3. prop.load(newFileInputStream("config.properties")); //将文件上的键值对读取到集合中,文件中键和值用:或=去连接!!!
  4. prop.setProperty("tel","18912345678"); //只是在内存中改了,但并没有写到配置文件中,写到文件中用store方法!!!
  5. prop.store(newFileOutputStream("config.properties"), null);//第二个参数是对列表参数的描述,可以给值及一段字符串,也可以给null!!!
  6. System.out.println(prop);

*注:附config.properties,配置文件内容格式如下:

qq=12345

tel=18912345678

username=zhangsan

发表评论

表情:
评论列表 (有 0 条评论,212人围观)

还没有评论,来说两句吧...

相关阅读

    相关 4.文件操作和IO

    该文章主要告诉大家在Java中如何对文件进行操作,例如我们文件的创建、删除、移动、复制、剪切等等一系列操作是如何实现的,又该如何使用呢,那么在文章中我们会为大家一 一进行...

    相关 Java-基础-4.IO

    一:为什么有IO流?  在显示生产中,我们的数据,都是不停的往过输入和输出,我们将这种模式称作为流。并且在输入和输出的过程中,我们包装了一些其他类。 二:什么是IO流

    相关 IO-4

    IO-4 01\_IO流(序列流) \ 1.什么是序列流          \序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个