1. 程式人生 > 實用技巧 >Java基礎學習01--I/O位元組流基本使用

Java基礎學習01--I/O位元組流基本使用

Java的I/O流可以分為:位元組流和字元流。本文重點介紹位元組流的基本使用。

1.FileInputStream與FileOutputStream

 1 package file.demo;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.IOException;
 5 
 6 //演示FileInputStream的使用
 7 public class filedemo01 {
 8 
 9     public static void main(String[] args) throws IOException {
10 
11         //
0.載入檔案路徑 12 String url = filedemo01.class.getClassLoader().getResource("aaa.txt").getPath(); 13 System.out.println("檔案位置是:" + url); 14 //1.建立FileInputStream 15 FileInputStream fileInputStream = new FileInputStream(url); 16 17 int available = fileInputStream.available();
18 System.out.println("可用位元組數為:" + available); 19 20 // //單個位元組讀取 21 // int data = 0; 22 // while ((data = fileInputStream.read()) != -1) { 23 // System.out.print((char) data); 24 // } 25 26 System.out.println("--------------------------"); 27 28 //一次讀取多個位元組
29 byte[] bytes = new byte[2]; 30 int count = 0; 31 while ((count = fileInputStream.read(bytes)) != -1) { 32 System.out.print(new String(bytes, 0, count)); 33 } 34 fileInputStream.close(); 35 } 36 }
 1 package file.demo;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 
 6 //演示FileOutputStream的使用
 7 public class filedemo02 {
 8     public static void main(String[] args) throws IOException {
 9 
10         //0.
11         String url = filedemo01.class.getClassLoader().getResource("bbb.txt").getPath();
12         System.out.println("檔案位置是:" + url);
13         //1.建立FileOutputStream物件
14         FileOutputStream fileOutputStream = new FileOutputStream(url);
15 
16         fileOutputStream.write(97);
17         fileOutputStream.write('b');
18 
19         fileOutputStream.close();
20     }
21 }
 1 package file.demo;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 
 7 public class filedemo03 {
 8 
 9 
10     public static void main(String[] args) throws IOException {
11         FileInputStream fileInputStream = new FileInputStream("/Users/yangasen/Downloads/aaa.txt");
12 
13         FileOutputStream fileOutputStream = new FileOutputStream("/Users/yangasen/Downloads/ccc.txt");
14 
15         int count = 0;
16         byte[] bytes = new byte[1024];
17         while ((count = fileInputStream.read(bytes)) != -1) {
18             fileOutputStream.write(bytes, 0, count);
19         }
20         fileInputStream.close();
21         fileOutputStream.close();
22 
23     }
24 
25 
26 }

2.BufferedInputStream與BufferedOutputStream

 1 package file.demo;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.FileInputStream;
 5 import java.io.IOException;
 6 
 7 //BufferedInputStream
 8 public class buffereddemo01 {
 9     public static void main(String[] args) throws IOException {
10         FileInputStream fileInputStream = new FileInputStream("/Users/yangasen/Downloads/aaa.txt");
11         BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
12 //        int data = 0;
13 //        while ((data = bufferedInputStream.read()) != -1) {
14 //            System.out.print((char) data);
15 //        }
16         System.out.println("-----------------");
17 
18         byte[] bytes = new byte[1024];
19         int count = 0;
20         while ((count = bufferedInputStream.read(bytes)) != -1) {
21             System.out.println(new String(bytes, 0, count));
22         }
23         bufferedInputStream.close();
24     }
25 }
 1 package file.demo;
 2 
 3 import java.io.BufferedOutputStream;
 4 import java.io.FileOutputStream;
 5 
 6 public class buffereddemo02 {
 7     public static void main(String[] args) throws Exception {
 8         FileOutputStream fileOutputStream = new FileOutputStream("/Users/yangasen/Downloads/ddd.txt");
 9         BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
10 
11         for (int i = 0; i < 10; i++) {
12             bufferedOutputStream.write('z');
13             bufferedOutputStream.flush();
14         }
15         bufferedOutputStream.close();
16     }
17 }

3.ObjectOutputStream與ObjectInputStream

 1 package file.demo;
 2 
 3 import java.io.Serializable;
 4 
 5 /*序列化與反序列化注意事項:
 6 1.序列化類必須實現Serializable介面
 7 2.序列化類中的物件屬性也要實現Serializable介面
 8 3.序列化版本號ID必須統一
 9 4.使用transient的屬性不會被序列化
10 5.靜態屬性不會被序列化
11 6.序列化多個物件,可以通過ArrayList等集合類
12  */
13 public class Student implements Serializable {
14 
15     private static final long serialVersionUID = 100L;
16 
17     private String Name;
18     private Integer Age;
19 
20     public String getName() {
21         return Name;
22     }
23 
24     public Integer getAge() {
25         return Age;
26     }
27 
28     public void setName(String name) {
29         Name = name;
30     }
31 
32     public void setAge(Integer age) {
33         Age = age;
34     }
35 
36     public Student(String name, Integer age) {
37         Name = name;
38         Age = age;
39     }
40 
41     @Override
42     public String toString() {
43         return "Student{" +
44                 "Name='" + Name + '\'' +
45                 ", Age=" + Age +
46                 '}';
47     }
48 }
 1 package file.demo;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.ObjectOutputStream;
 5 
 6 public class objectdemo01 {
 7     public static void main(String[] args) throws Exception {
 8         FileOutputStream fileOutputStream = new FileOutputStream("/Users/yangasen/Downloads/student.bin");
 9         ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
10 
11         objectOutputStream.writeObject(new Student("zhangsan", 18));
12 
13         objectOutputStream.close();
14     }
15 }
 1 package file.demo;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.ObjectInputStream;
 5 
 6 public class objectdemo02 {
 7     public static void main(String[] args) throws Exception {
 8         FileInputStream fileInputStream = new FileInputStream("/Users/yangasen/Downloads/student.bin");
 9         ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
10 
11         Student student = (Student) objectInputStream.readObject();
12         System.out.println(student);
13 
14         objectInputStream.close();
15 
16     }
17 }