按照處理的位元組數
位元組流
字元流
按照方向分類
輸入流
輸出流
按照功能
節點流
處理流
相關代碼
public static void output() {
FileOutputStream fos = null;
File file = new File("demo0609.txt");
String str = "mark";
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
fos = new FileOutputStream(file);
fos.write(str.getBytes());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void input() {
FileInputStream fis = null;
File file = new File("demo0609.txt");
int result = 0;
try {
fis = new FileInputStream(file);
while((result=fis.read())!=-1) {
System.out.println((char)result);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void outputBuffered() {
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
File file = new File("test.txt");
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
fos = new FileOutputStream(file);
osw = new OutputStreamWriter(fos);
bw = new BufferedWriter(osw);
String[] a = {"sfds", "eeee"};
for (int i = 0; i < a.length; i++) {
bw.write(a[i]);
bw.newLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bw.close();
osw.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void inputBuffered() {
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
File file = new File("test.txt");
String result = "";
try {
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
while((result=br.readLine())!=null) {
System.out.println(result);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void serializable() {
OutputStream os = null;
ObjectOutputStream oos = null;
}
序列化和反序列化
類的對象會随着程式的終止而被垃圾收集器銷毀。如果要在不重新建立對象的情況下調用該類,該怎麼做?這就可以通過序列化将資料轉換為位元組流。 對象序列化是一個用于将對象狀态轉換為位元組流的過程,可以将其儲存到磁盤檔案中或通過網絡發送到任何其他程式;從位元組流建立對象的相反的過程稱為反序列化。而建立的位元組流是與平台無關的,在一個平台上序列化的對象可以在不同的平台上反序列化。 需要實作序列化的對象要實作Serializable接口
public static void serializable() {
Student stu = new Student("123", "mark", 22);
FileOutputStream fos = null;
ObjectOutputStream oos = null;
File file = new File("demo02.txt");
try {
fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
oos.writeObject(stu);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
oos.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void unserializable() {
FileInputStream fis = null;
ObjectInputStream ois = null;
File file = new File("demo02.txt");
if(!file.exists()) {
System.out.println("檔案不存在");
return;
}
try {
fis = new FileInputStream(file);
ois = new ObjectInputStream(fis);
Object o = ois.readObject();
if(o instanceof Student) {
Student stu = (Student)o;
System.out.println(stu.toString());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
ois.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
MarkText 編寫
