### File類
~~~
public class FileDemo1 {
public static void main(String[] args) throws IOException {
// File類--不能訪問文件
File file = new File("E:/abc.doc");
// 創建文件夾mkdirs()
// if(!file.exists()) {
// file.mkdirs();
// }
// 創建文件createNewFile()
// if(!file.exists()) {
// file.createNewFile();
// }
// 判斷是文件還是文件夾的方法
// if(file.isFile()) {
// System.out.println("是文件");
// }
//
// if(file.isDirectory()) {
// System.out.println("是文件夾");
// }
// 文件路徑
// System.out.println(file.getPath());
// 文件名稱
// System.out.println(file.getName());
// 刪除文件
// if(file.exists()) {
// file.delete();
// }
// System.out.println(file.getAbsolutePath());
}
}
~~~
***
### 流(Stream)
文件傳輸(讀取和書寫)
**按照流向分類**:輸入輸出的**參照物是Java程序**
①輸入流:讀文件
②輸出流:寫文件
**按照傳輸單位分類**:
①字節流:以字節為單位傳輸數據,一般用于二進制文件的傳輸
②字符流:以字符為單位傳輸數據,一般用于純文本文件的傳輸
**按照功能分類**:
①節點流
②處理流
***



***
~~~
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 純文本的拷貝
* @author Administrator
*
*/
public class Practice1 {
public static void main(String[] args) {
// 將D:/text1.txt拷貝到E:test2.txt
FileReader fr = null;
BufferedReader in = null;
FileWriter fw = null;
PrintWriter out = null;
try {
fr = new FileReader("D:/test1.docx");
in = new BufferedReader(fr);
fw = new FileWriter("E:/test2.docx");
out = new PrintWriter(fw,true);
String msg = in.readLine();
while(msg != null) {
out.println(msg);
msg = in.readLine();
}
System.out.println("文件copy成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
out.close();
}
}
}
~~~
~~~
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 二進制文件的拷貝
* @author Administrator
*
*/
public class BinaryFileCopyDemo1 {
public static void main(String[] args) {
// 搭建管道
FileInputStream fis = null;
BufferedInputStream in = null;
FileOutputStream fos = null;
BufferedOutputStream out = null;
try {
fis = new FileInputStream("C:\\Users\\Administrator\\Desktop\\2345截圖20180511165805.png");
in = new BufferedInputStream(fis);
File file = new File("E:/xyz");
if(!file.exists()) {
file.mkdir();
}
fos = new FileOutputStream("E:/test.png");
out = new BufferedOutputStream(fos);
byte buffer[] = new byte[1024];
// len是真正讀取到的字節數,沒有返回-1
int len = in.read(buffer);
while(len != -1) {
// buffer緩沖區
// off 偏移量--0
// len 長度
out.write(buffer, 0, len);
len = in.read(buffer);
}
System.out.println("finish");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
if(fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
~~~
***
### 對象序列化
對象(Object)序列化是指將對象轉換為字節序列的過程
反序列化則是根據字節序列恢復對象的過程
通過使用ObjectInputStream和ObjectOutputStream類保存和讀取對
象的機制叫做序列化機制
序列化對象前提:
這個類必須實現一個接口**Serializable**(或者**Externalizable**)
~~~
public interface Serializable {
}
~~~
接口Serializable是一個空的接口,Java中存在很多向這樣沒有方法,只有定義的接口:標識接口,標識一個類是否具有某種能力
實現Serializable的類對象就能實現序列化
~~~
import java.io.Serializable;
public class Person implements Serializable{
// serialVersionUID確保唯一
private static final long serialVersionUID = 8373631186930852694L;
// transient能夠修飾屬性--不能被序列化(反序列化只能得到默認值)
transient int age;
String name;
public Person() {
}
public Person(int age, String name) {
super();
this.age = age;
this.name = name;
}
}
~~~
~~~
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerializationDemo {
public static void main(String[] args) {
// 對象序列化
// Person p = new Person(13, "李四");
// FileOutputStream fos = null;
// ObjectOutputStream out = null;
// try {
// fos = new FileOutputStream("E:/person.bin");
// out = new ObjectOutputStream(fos);
// out.writeObject(p);
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// try {
// fos.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// try {
// out.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// 反序列化
FileInputStream fis = null;
ObjectInputStream in = null;
try {
fis = new FileInputStream("E:/person.bin");
in = new ObjectInputStream(fis);
Person p1 = (Person) in.readObject();
System.out.println(p1.age + " " + p1.name);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
~~~