io流和序列化
阿新 • • 發佈:2017-06-17
close 空格 ont delet 內容 stringbu class 成功 緩沖區
2.使用FileInputStream讀取文件內容(字節流)
3.使用FileOutputStream寫入文件內容(字節流)
4.使用FileReader讀取文件內容(字符流)
1.使用File操作文件
public class IoTest { public static void main(String[] args) throws IOException { /* 01.刪除或者創建文件 * File file=new File("e:/io.txt"); addOrDel(file); */ File file=new File("e:/java/hello"); //file.mkdir(); 只能創建一層目錄 file.mkdirs(); //同時創建多層目錄 } /** * 刪除或者創建文件 */ public static void addOrDel(File file) throws IOException { if (!file.exists()) { //判斷文件是否存在 if (file.createNewFile()) {//創建成功 System.out.println("創建成功!"); System.out.println("是否是文件:"+file.isFile()); System.out.println("文件名稱:"+file.getName()); System.out.println("文件大小:"+file.length()); System.out.println("文件的絕對路徑:"+file.getAbsolutePath()); }else { System.out.println("創建失敗"); } }else { System.out.println("文件已經存在!"); if (file.delete()) {//刪除文件 System.out.println("刪除成功!"); } } } }
public class FileInputStreamTest01 { public static void main(String[] args) throws Exception { /* * 創建輸入流對象 從電腦中的源文件 獲取 數據到 內存中 * 如果沒有指定的文件 會報錯 FileNotFoundExecption */ FileInputStream stream=new FileInputStream("e:/hello.txt"); //輸出字節數 System.out.println("可讀取的字節數:"+stream.available()); /* * 讀取內容 * 循環讀取數據,會從輸入流中讀取一個8位的字節!把它轉換成了0-255之間的整數返回了! * 我們要想拿到具體的值!必須把整數轉成字符 char (0-65535) * read()當讀取文件的結尾處 返回-1 * UTF-8 :中的 中文字符以及中文 占據 3個 字節, 數字和字母占1個字節 * GBK,gb2312: 編碼中 中文和符號都占2個字節! */ int num; while((num=stream.read())!=-1){ System.out.print(num); } //關閉輸入流 stream.close(); } }
public class FileOutputStreamTest02 { public static void main(String[] args) throws Exception { /* * 01.java項目的編碼格式 要和輸出文件的編碼一致 都是UTF-8 * 02.如果系統中沒有指定的文件,會默認創建 * 03.如果重復輸出,則上次的內容會被覆蓋 * 如果不想覆蓋!使用重載!在第二個參數的位置輸入true */ FileOutputStream stream=new FileOutputStream("e:/hello.txt",true); String str="真的好棒!"; //參數傳遞需要一個Byte類型的數組 stream.write(str.getBytes()); //關閉輸出流 stream.close(); } }
public class FileReaderTest03 { public static void main(String[] args) throws Exception { //獲取當前的編碼格式 System.out.println("使用的編碼為:"+System.getProperty("file.encoding")); //創建輸入流 Reader Reader reader=new FileReader("e:/hello.txt"); //因為讀取的字符 創建數據的中轉站 會有多余的空格產生 char [] words=new char[1024]; int num; //需要字符串的 拼接 StringBuilder sb=new StringBuilder(); while((num=reader.read(words))!=-1){ sb.append(words); } System.out.println(sb.toString()); //關閉輸入流 reader.close(); } }5.使用BufferedReader讀取文件內容(字符流)
public class BufferedReaderTest04 { public static void main(String[] args) { /* * 創建輸入流 Reader * BufferedReader和FileReader聯合使用 * 效率高 底層有默認的緩沖區 還可以逐行讀取 */ //創建BufferedReader對象 傳遞Reader的對象 BufferedReader br=null; Reader reader=null; try { reader = new FileReader("e:/hello.txt"); br=new BufferedReader(reader); //一行一行的讀取 String line=null; while((line=br.readLine())!=null){ System.out.println(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ //關閉流 先開的後關 try { br.close(); reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
6.使用FileWriter讀取文件內容(字符流)
public class FileWriterTest05 { public static void main(String[] args) { try { /* * 輸出流 默認的會給我們創建新的文件 * 不使用第二個參數! 那麽默認會覆蓋之前的內容 * ctrl +shift +t 選中需要查詢的類或者接口 */ Writer writer=new FileWriter("e:/hello.txt",true); writer.write("我愛北京天安門!"); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }
7.使用BufferedWriter寫入文件(字符流)
public class BufferedWriterTest06 { public static void main(String[] args) { try { //創建輸出流對象 Writer writer=new FileWriter("e:/hello.txt",true); //創建BufferedWriter對象 BufferedWriter bw=new BufferedWriter(writer); //換行 bw.newLine(); bw.write("北京也愛你!"); bw.newLine(); bw.write("北京也愛你!"); bw.close(); writer.close(); //獲取輸入流 Reader reader=new FileReader("e:/hello.txt"); BufferedReader br=new BufferedReader(reader); String line=null; while((line=br.readLine())!=null){ System.out.println(line); } br.close(); reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
8.使用InputStreamReader解決中文亂碼問題
public class InputStreamReaderTest07 { public static void main(String[] args) { BufferedReader br=null; InputStreamReader isr=null; InputStream stream=null; try { //創建輸入流對象 stream=new FileInputStream("e:/hello.txt"); System.out.println("文件的大小:"+stream.available()); //使用InputStreamReader來解決亂碼 isr=new InputStreamReader(stream, "utf-8"); //讀取 br=new BufferedReader(isr); String line=null; while((line=br.readLine())!=null){ System.out.println(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { br.close(); isr.close(); stream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
9.讀取2進制文件
public class DataInputStreamTest08 { public static void main(String[] args) throws Exception { //創建輸入流 InputStream fis=new FileInputStream("e:/mm.mp3"); //讀取2進制文件 DataInputStream dis=new DataInputStream(fis); //復制文件到另一個目錄 OutputStream fos=new FileOutputStream("e:/U1/慢慢.mp3"); //以2進制的方式輸出到指定的目錄 DataOutputStream dos=new DataOutputStream(fos); //開始讀取 int data; while((data=dis.read())!=-1){ //寫入 dos.write(data); } dos.close(); fos.close(); dis.close(); fis.close(); }10.序列化和反序列化
/** * 序列化:將內存中對象的狀態或者信息 轉換成 持久化的過程! * 反序列化:把持久化的對象 變成 內存中的一個對象的過程! * 目的: * 01.使自定義的對象 持久化!對象本身是在內存中的!我們想把它持久化! * 02.把對象從一個地方傳遞到另一個地方! * 03.使程序具有維護性! * * 怎麽才能實現對象的序列化?? * 1.讓對象所屬的類 實現Serializable 接口 之後, 這個類 就可以序列化了! * Serializable:只是一個能否被序列化的標記! */ public class Student implements Serializable{ //實體類 private Integer id; private Integer age; private String name; @Override public String toString() { return "Student [id=" + id + ", age=" + age + ", name=" + name + "]"; } public Student() { super(); } public Student(Integer id, Integer age, String name) { super(); this.id = id; this.age = age; this.name = name; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }用於序列化的Student類
public class SerializableTest { public static void main(String[] args) throws Exception { //序列化操作 //首先實例化一個對象 Student student=new Student(1, 500, "小白2"); //想序列化?從內存中 放入 持久化的介質中 輸出流 FileOutputStream fos=new FileOutputStream("e:/student.txt"); ObjectOutputStream oos=new ObjectOutputStream(fos); //開始持久化 oos.writeObject(student); oos.close(); fos.close(); } }序列化
/** * 反序列化 * 需要和 序列化時候的包名 一致 不然 沒法反序列化 */ public class StudentTest { public static void main(String[] args) throws Exception { // 從文件中把對象 拿到 內存中 輸入流 FileInputStream fis = new FileInputStream("e:/student.txt"); ObjectInputStream ois = new ObjectInputStream(fis); // 讀取文件中的對象 Student student = (Student) ois.readObject(); System.out.println(student.getId()); System.out.println(student.getAge()); System.out.println(student.getName()); ois.close(); fis.close(); } }
io流和序列化