文章目錄
- 1 InputStream
- 2 OutputStream
- 3 檔案拷貝(位元組流)
- 4 FileReader、 FileWriter
- 5 檔案拷貝(字元流)
- 6 節點流和處理流
- 7 ObjectOutputSteam和ObjectInputStream---序列化和反序列化
- 8 轉換流
- 結語

📖
個人介紹
大家好我是:一顆松
認真分享技術,記錄學習點滴
如果分享對你有用請支援我哦🍺
點贊:👍 留言:✍收藏:⭐️
個人格言: 想法落實的最佳時機就是現在!🏄
1 InputStream
代碼示例:
public class FileInputStreamDemo {
public static void main(String[] args) throws Exception {
// 定義檔案路徑
String str = "E:\\Codes\\myProject\\fileInputStreamTest.txt";
// 建立FileInputStream對象
FileInputStream fileInputStream = new FileInputStream(str);
// 建立一個8個位元組的緩沖空間
byte buffer [] = new byte[8];
int length = 0;
while ((length = fileInputStream.read(buffer)) != -1){
System.out.println(new String(buffer,0,length));
}
}
}
2 OutputStream
代碼示例:
此種方式資料寫入檔案,是覆寫源檔案;
public class FileOutputSteamDemo {
public static void main(String[] args) throws IOException {
String str = "E:\\Codes\\myProject\\fileInputStreamTest.txt";
OutputStream outputStream = new FileOutputStream(str);
byte [] buffer = "gym.IverryverryLoveyou".getBytes();
try {
outputStream.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}finally {
outputStream.close();
}
System.out.println("完成寫入");
}
}
如果想實作資料寫入檔案,追加而不是覆寫則可以采用以下構造器:
- public FileOutputStream( filesrc, boolean append)
參數append為true時則,追加資料,不覆寫,
3 檔案拷貝(位元組流)
拷貝代碼示例
public class IOCopyDemo {
public static void main(String[] args) throws IOException {
String str = "E:\\Codes\\myProject\\copyTest.txt";
String str2 = "E:\\Codes\\myProject\\coptTest2.txt";
// 建立輸入流對象
FileInputStream fileInputStreamDemo = new FileInputStream(str);
// 建立輸出流對象
FileOutputStream fileOutputStream = new FileOutputStream(str2);
// 建立緩沖
byte buffer [] = new byte[8];
int leng = 0;
// 用循環實作拷貝
try {
while ((leng = fileInputStreamDemo.read(buffer))!=-1){
fileOutputStream.write(buffer,0,leng);
}
} finally {
if (fileOutputStream != null || fileInputStreamDemo != null){
fileInputStreamDemo.close();
fileOutputStream.close();
}
}
System.out.println("拷貝完成");
}
}
4 FileReader、 FileWriter
FileWriter同樣有兩種模式,覆寫模式,和追加模式;
覆寫模式;使用以下構造器
- public FileWriter( fileName)
使用下面構造器append 設定為true是是追加模式;
- public FileWriter( fileName, boolean append)
注意事項:FileWriter使用之後必須關閉(close)或者重新整理(flush),否則寫不到檔案中;
5 檔案拷貝(字元流)
public class CopyDemo2 {
public static void main(String[] args) throws FileNotFoundException {
String str = "E:\\Codes\\myProject\\fileWriter.txt";
String str2 = "E:\\Codes\\myProject\\fileCopy.txt";
// 建立字元輸入流
FileReader fileReader = new FileReader(str);
char [] array = new char[10];
int leng=0;
// 建立字元輸出流
try {
FileWriter fileWriter = new FileWriter(str2);
while ((leng = fileReader.read(array)) !=-1){
String s =new String(array,0,leng);
fileWriter.write(s);
// 重新整理字元輸出流
fileWriter.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
}
}
}
6 節點流和處理流
節點流是針對某個特定的資料源讀寫,如FileWriter、FileReader;處理流也叫包裝流,連接配接已存在的流,為程式提供更強大的功能如BufferWriter、BufferReader;
7 ObjectOutputSteam和ObjectInputStream—序列化和反序列化
此處理流(包裝流),提供了對基本類型或者對象類型的序列化和反序列化的方法;
ObjectOutputStream:提供了序列化的方法
ObjectInputStream:提供了反序列化的方法
序列化: 就是将資料與資料類型都儲存起來,叫序列化;
反序列化:指将序列化的資料恢複到原來;
注意:如果想讓某個對象支援序列化,則必須讓其類是可序列化的,也就是必須讓類實作兩個接口:
Sarializable:标記接口,沒有方法(推薦使用)
Externalizable:該接口有方法需要實作
序列化代碼示例:
public class SerializableDemo {
public static void main(String[] args) throws IOException {
// 建立被序列化對象
Person xiaoM = new Person("小明",18);
// 建立檔案路徑
String str = "E:\\Codes\\myProject\\ObjectOut.dat";
// 建立處理流對象
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(str));
outputStream.writeObject(xiaoM);
outputStream.close();
System.out.println("序列化完成");
}
}
class Person implements Serializable {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
/**
* 擷取
* @return name
*/
public String getName() {
return name;
}
/**
* 設定
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 擷取
* @return age
*/
public int getAge() {
return age;
}
/**
* 設定
* @param age
*/
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Person{name = " + name + ", age = " + age + "}";
}
}
反序列化代碼示例:
public class ReSerializableDmeo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 定義變量接收,反序列化資料
Person person = null;
// 建立檔案路徑
String str = "E:\\Codes\\myProject\\ObjectOut.dat";
// 建立ObjectInputStream對象
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(str));
person = (Person) objectInputStream.readObject();
objectInputStream.close();
System.out.println("反序列化完成!");
System.out.println("姓名:"+person.getName()+" 年齡:"+person.getAge());
}
}
序列化過程中需要注意的事項:
(1):讀寫順序要一緻
(2):要實作序列化的的對象,類必須實作Serializable接口
(3):序列化類中建議添加 SerialVersionUID,可以提高版本相容性
(4):序列化時會預設将裡面所有屬性都序列化了,但是被static和transient修飾的屬性不會被序列化;
(5):序列化時要求裡面屬性的類型也需要實作序列化接口
(6):序列化具有可繼承性
8 轉換流
InputStreamReader和OutputStreamWriter
代碼示例:
public class InputStreamReaderDemo {
public static void main(String[] args) throws IOException {
String str = "E:\\Codes\\myProject\\fileWriter.txt";
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(str),"gbk");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String s = null;
while ((s = bufferedReader.readLine()) != null){
System.out.println(s);
bufferedReader.lines();
}
inputStreamReader.close();
bufferedReader.close();
}
}
結語
大佬請留步![]()
IO流再回顧,深入了解序列化和反序列化