法国网站域名,江苏网站建设联系方式,药品包装设计,网络优化初学者难吗一、理解
1. 简单而言#xff1a;流就是内存与存储设备之间传输数据的通道、管道。
2. 分类#xff1a; (1) 按方向(以JVM虚拟机为参照物)【重点】 输入流#xff1a;将中的内容读入到中。 输出流#xff1a;将中的内容写入到中。 (2) 按单位#xff1a; 字节流#xf…一、理解
1. 简单而言流就是内存与存储设备之间传输数据的通道、管道。
2. 分类 (1) 按方向(以JVM虚拟机为参照物)【重点】 输入流将中的内容读入到中。 输出流将中的内容写入到中。 (2) 按单位 字节流以字节为单位可以操作所有类型的文件。 字符流以字符为单位只能操作文本类型的文件。 (3) 按功能 节点流具有基本的读写功能。 过滤流在节点流的基础上增加新的功能。 二、字节流
1. 父类
字节流的父类(抽象类) (1) InputStream字节输入流 对应的操作为读操作 功能方法read方法 (2) OutputStream:字节输出流 对应的操作为写操作 功能方法write方法 2. 字节节点流 (1) FileOutputStream字节节点输出流 、文件字节输出流 构造方法 FileOutputStream fos new FileOutputStream(D:\\test56/a.txt); 参数代表操作文件的路径如果指定的文件夹不存在则运行报错错误信息为 java.io.FileNotFoundException: D:\test5\a.txt (系统找不到指定的路径。) 如果指定的 文件不存在系统自动创建 绝对路径盘符\\文件夹\\ 文件 相对路径文件夹/文件默认在当前的项目中查找对应的文件夹内容 功能方法: write(int n)将单个字节写入文件中 close()关闭流 (2) FileInputStream文件字节输入流 构造方法 FileInputStream fis new FileInputStream(file/c.txt); 参数说明参数代表操作路径如果指定的文件不存在则运行报错错误信息为 java.io.FileNotFoundException: file\c.txt (系统找不到指定的文件。) 功能方法 int read()一次性读取一个字节内容将读取的内容作为返回值返回达到文件尾部时回-1 close()关闭流释放资源 3.字节流 (1) 过滤流BufferedOutputStream/BufferedInputStream 缓冲流,提高IO效率减少访问磁盘的次数 数据存储在缓冲区中flush是将缓存区的内容写入文件中也可以直接close。 public class TestFileCopyBuffered { public static void main(String[] args) throws IOException { // 1. 创建文件字节输入流输出流对象 // (1) 创建文件节点流对象 FileInputStream fis new FileInputStream(D:\\test\\ph.mp4); // (2) 创建过滤流 BufferedInputStream bis new BufferedInputStream(fis); // 写文件 FileOutputStream fos new FileOutputStream(file/ph_copy.mp4); BufferedOutputStream bos new BufferedOutputStream(fos); // 2. 边读边写 while(true){ int nbis.read(); if(n-1) break; bos.write(n); } // 3. 关闭流。释放资源 bis.close(); bos.close(); } } (1) 过滤流ObjectOutputStream/ObjectInputStream 增强了缓冲区功能 增强了读写8种基本数据类型和字符串功能 增强了读写对象的功能: readObject() 从流中读取一个对象 writeObject()写入对象 对象在流上进行传输的过程称为对象序列化。 对象序列化的要求[重点] 参与对象序列化的对象对应的类必须实现java.io.Serializable接口 transient修饰的属性不参与对象序列化 对象序列化达到文件尾部的标识 如果运行时抛出 java.io.EOFException代表读取的文件达到尾部 对象序列化的细节 如果对象的属性是自定义类型的对象时则该对象也必须是可序列化的 如果对集合进行对象序列化必须保证该集合中的所有元素是可序列化的 例 import java.io.*;public class TestObjectOutputStream {public static void main(String[] args) throws IOException, ClassNotFoundException
{// 将对象写入文件中Student s new Student(红包,23,100.0);// 1. 创建文件字节输出流对象 -》基础流FileOutputStream fos new FileOutputStream(file/stu.txt);// 2. 包装过滤流ObjectOutputStream oos new ObjectOutputStream(fos);// 3. 写对象oos.writeObject(s);// 4. 关闭流释放资源oos.close();// 读对象FileInputStream fis new FileInputStream(file/stu.txt);ObjectInputStream ois new ObjectInputStream(fis);Object oois.readObject();System.out.println(o);ois.close();}} import java.io.Serializable;class Address implements Serializable{}public class Student implements Serializable {private String name;private transient Integer age;private Double score;private Address a new Address();public Student() {}public Student(String name, Integer age, Double score) {this.name name;this.age age;this.score score;}public String getName() {return name;
}public void setName(String name) {this.name name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}public Double getScore() {return score;}public void setScore(Double score) {this.score score;}Overridepublic String toString() {return Student{ name name \ , age age , score score };}} 三、字符流
1. 字符流的父类(抽象类) Reader字符输入流 对应的操作为读操作 功能方法read方法 Writer:字符输出流 对应的操作为写操作 功能方法write方法 2. 文件字符流 (1) FileWriter文件字符输出流继承Writer中的方法 public void write(int n):将单个字符写入到文件中 (2) FileReader文件字符输入流继承Reader中的方法 public int read():一次读取一个字符的内容 3. 字符过滤流 (1) BufferedReader 功能方法readLine()一次性读取一行内容返回内容为String读取达到尾部返回-1 (2) PrintWriter println(参数); 4. 桥转换流 InputStreamReader/OutputStreamWriter:桥转换流设置 编解码格式 import java.io.*;// 桥转换流: ctrA - ctrx - 设置格式 - ctrv -ctrspublic class TestInputStreamReader {public static void main(String[] args) throws IOException {// 1. 创建文件字节输入流对象
FileInputStream fis new FileInputStream(file/k.txt);// 2. 创建桥转换流对象设置编解码格式
InputStreamReader isr new InputStreamReader(fis,GBK);// 3. 创建过滤流
BufferedReader br new BufferedReader(isr);// 4. 读操作
while(true){String n br.readLine();if(nnull) break;System.out.println(n);}// 5. 关闭流
br.close();}} import java.io.*;// 桥转换流: ctrA - ctrx - 设置格式 - ctrv -ctrspublic class TestOutputStreamWriter {public static void main(String[] args) {PrintWriter pw null;try {FileOutputStream fos new FileOutputStream(file/my.txt);OutputStreamWriter osw new OutputStreamWriter(fos, GBK);pw new PrintWriter(osw);pw.println(嘻嘻);pw.println(哈哈);pw.print(呵呵);}catch (IOException e){e.printStackTrace();}finally {if(pw !null) {pw.close();}}}} 四、 File类
1.IO和File
IO流对文件中的内容进行操作。 File类对文件自身进行操作例如删除文件文件重新命名等
2.操作 public class TestFile { public static void main(String[] args) throws IOException { File file new File(file/hh.txt); /*System.out.println(file.exists()); file.createNewFile();*/ if(file.exists()){ System.out.println(文件存在则直接使用...); FileInputStream fis new FileInputStream(file); }else{ System.out.println(文件不存在创建新的文件....); file.createNewFile(); } } }