文件操作

markdown ## I/0 Stream ```text I/O: 设备之间/内部数据传输 ``` ### 字符流 ```text 文本文件用字符流 ``` ```text #读入文件 方式一 File file = new File("filepath"); if(file.exists()){ try { FileReader fileReader = new FileReader(file); int read = fileReader.read(); while (read != -1) { System.out.print((char) read); read = fileReader.read(); } fileReader.close(); } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } #读入文件 方式二 File file = new File("filepath"); if(file.exists()){ try { FileReader fileReader = new FileReader(file); char[] chars = new char[5]; int read; while ((read = fileReader.read(chars)) != -1){ for (int i = 0; i < read; i++) { System.out.print(chars[i]); } } fileReader.close(); } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } ``` ```text #写出文件 File file = new File("filepath"); if(file.exists()){ try { FileWriter fileWriter = new FileWriter(file,true); fileWriter.write("hello world\n"); fileWriter.close(); } catch (IOException e) { throw new RuntimeException(e); } } ``` ### 字节流 ```text 非文本文件用字节流 ``` #### FileInputStream ```text File file = new File("filepath"); if(file.exists()){ try { FileInputStream fileInputStream = new FileInputStream(file); byte[] bytes = new byte[10]; int read; while ((read = fileInputStream.read(bytes)) != -1){ for (int i = 0; i < read; i++) { System.out.print((char) bytes[i]); } } } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } ``` ### 缓冲流 ```text 提升读写速度 ``` ```text File file = new File("filepath"); try { FileInputStream fileInputStream = new FileInputStream(file); BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); byte[] bytes = new byte[10]; int read; while ((read = bufferedInputStream.read(bytes)) != -1){ for (int i = 0; i < read; i++) { System.out.print((char) bytes[i]); } } } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } ``` ### 转换流 ```text Java API 提供了两个转换流(属于字符流) public class InputStreamReader extends Reader public class OutputStreamWriter extends Writer #字节流与字符流的转换 ``` ```text File file = new File("/home/harlod/OneDrive/Project/java_basis/src/filestream/a.txt"); try { FileInputStream fileInputStream = new FileInputStream(file); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf8"); char[] chars = new char[10]; int read; while ((read = inputStreamReader.read(chars)) != -1) { for (int i = 0; i < read; i++) { System.out.print(chars[i]); } } } catch (FileNotFoundException e) { throw new RuntimeException(e); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } ``` ### 对象流 ```text #又称为对象的序列化和反序列化 ObjectInputStream ObjectOutputStream 序列化是 RMI 过程的参数与返回值都必须实现的机制 1. 对象需要实现接口 Serializable 2. 定义序列版本号 public static final long serialVersionUID 3. 类的属性都应该可序列化 ``` ```text try { Person person = new Person(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("a.txt"))); objectOutputStream.writeObject(person); objectOutputStream.flush(); objectOutputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } ``` ```text try { ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("a.txt"))); Object o = objectInputStream.readObject(); System.out.println(o.toString()); objectInputStream.close(); } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); } ``` *

*[返回教程主页](https://www.monody.net/p/blog-page_3.html)*

*

评论