今天系統的學習了 Java IO 這部分的知識,把一些以前沒有用過的,或者忘記的方法和類從新學了一下。怕本身睡一覺把今天學的忘完了QaQ。因此,不得已寫一下博客java
IO概述設計模式
package com.coder.com; import java.io.*; import java.nio.charset.StandardCharsets; public class Demo01 { /** * IO流概述 * 能夠將數據傳輸的操做當作一種數據的流動,按照流動的方向能夠分爲輸入Input和輸出Output * Java中的IO操做主要指的是 java.io包下的一些經常使用類的使用, 經過這些類能夠對數據進行讀取(輸入流)和寫出(輸出流) * <p> * IO流分類: * 按照方向: * - 輸入流 * - 輸出流 * 按照數據類型: * - 字節流 * -輸入 InputStream * -輸出 OutputStream * - 字符流 * -輸入 Reader * -輸出 Writer * 一切皆字節: * 計算機中的任何數據都是以二進制形式存儲的 * 傳輸時也是以二進制的形式傳輸的 *</p> * @param args */ public static void main(String[] args) throws IOException { //字節流 OutputStream os = new FileOutputStream("E:\\a.txt", true); os.write(65); byte[] bytes = {66, 67, 68}; os.write(bytes, 1, 2); os.write("你好,世界".getBytes(StandardCharsets.UTF_8)); os.close(); InputStream is = new FileInputStream("E:\\a.txt"); bytes = new byte[1024]; int len = is.read(bytes); System.out.println("new String(bytes, 0, len) = " + new String(bytes, 0, len)); is.close(); } }
字節輸出流app
方法:ide
經常使用子類:學習
字符流this
package com.coder.com; import java.io.*; public class Demo02 { public static void main(String[] args) throws IOException { Writer writer = new FileWriter("E:\\b.txt"); writer.write("你好,世界,"); writer.append("你好,Java").append("你好,Golang"); writer.close(); Reader reader = new FileReader("E:\\b.txt"); char[] chars = new char[1024]; int len = reader.read(chars); String s = new String(chars, 0, len); System.out.println("content = " + s); reader.close(); } }
轉換流編碼
package com.coder.com; import java.io.*; public class Demo03 { /** * 轉換流 * 字節流 裝飾爲 字符流 :使用裝飾着設計模式 * @param args */ public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("E:\\a.txt"); //param1 要轉換的字節流 //param2 指定編碼名稱 InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); char[] buffs = new char[1024]; int len = isr.read(buffs); for (int i = 0; i < len; i++) { System.out.print(buffs[i]); } isr.close(); fis.close(); } }
打印流設計
package com.coder.com; import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; public class Demo04 { /** * 打印流 * @param args */ public static void main(String[] args) throws IOException { PrintStream ps = new PrintStream("E:\\c.txt"); ps.println("你好,世界"); ps.println("你好,Java"); ps.println("你好, Golang"); ps.close(); PrintWriter pw = new PrintWriter("E:\\c.txt"); pw.println("你好Vue"); pw.println("你好SpringBoot"); pw.close(); } }
Propertiescode
package com.coder.com; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; public class Demo05 { public static void main(String[] args) throws IOException { // Properties properties = new Properties(); // properties.setProperty("name", "王令"); // properties.setProperty("age", "20"); // properties.setProperty("gender", "男"); // properties.store(new FileWriter("E:\\d.properties"), "我的信息"); Properties properties = new Properties(); properties.load(new FileReader("E:\\d.properties")); System.out.println("name = " + properties.getProperty("name")); System.out.println("age = " + properties.getProperty("age")); System.out.println("gender = " + properties.getProperty("gender")); } }
序列化 反 序列化get
package com.coder.com; import java.io.*; import java.util.ArrayList; import java.util.List; public class Demo06 { /** * 序列化 反 序列化 * * @param args */ public static void main(String[] args) throws IOException, ClassNotFoundException { // Person wl = new Person("王令", 21, "男"); // ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:\\person")); // oos.writeObject(wl); // oos.close(); // ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:\\person")); // Person wl = (Person) ois.readObject(); // System.out.println("wl = " + wl); // ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:\\list_person")); // List<Person> people = new ArrayList<>(); // people.add(new Person("張三", 20, "男")); // people.add(new Person("李四", 21, "女")); // people.add(new Person("王五", 22, "男")); // oos.writeObject(people); // oos.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:\\list_person")); List<Person> people = (List<Person>) ois.readObject(); for (Person person : people) { System.out.println(person); } } } class Person implements Serializable { private String name; private Integer age; private String gender; public Person() { } public Person(String name, Integer age, String gender) { this.name = name; this.age = age; this.gender = gender; } 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 String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + '}'; } }