Java的IO體系主要有5類1接口:InputStream、OutputStream、Reader、Writer、File 和Serializable接口。
InputStream、OutputStream、Reader、Writer是抽象類,InputStream和OutputStream是字節流的父類,Reader、Writer是字符流的父類。
找個圖看一下IO體系結構:
字節流是一個字節一個字節的讀取,這個針對單字節的文件還是可以的,但是文件中有中文,如果讀取的位置不對,是會出現亂碼的。而字符流就可以解決這一問題,它每次讀取的是2個字節的。File類是針對文件系統操作,對文件進行操作。
字節流不能使用緩衝區,而字符流是可以使用緩衝區的。緩衝區的作用是將讀取或寫入的字符存放到一塊內存區域,等到一定時候,則一併處理,這樣可以加快處理的速度,字節流則是每次都和要讀取的或者是要寫入的文件進行交互,這樣會增大系統的時間開銷,所以速度會慢。
有很多的字節流類和字符流類繼承的InputStream、OutputStream、Writer、Reader。
IO的輸入輸出源和目的地:將數據讀入程序中,則使用read方法,程序將數據寫入到文件中則使用write方法,這兩個方法有很多的參數,根據需要使用。
Java的序列化是爲了將對象流化,可以對其進行讀寫操作,對象都是在JVM中存在,如果JVM關閉則對象就消失,但是使用序列化之後對象就可以被寫入到文件中,以後如果使用的話可以再進行讀取文件,那麼對象又可以重新使用,可以序列化的對象必須是實現了Serializable接口的。反序列化就是將序列化的對象重新取出來使用。
貼上幾段代碼:
這段代碼主要是File類的使用
package cn.swpu;
import java.io.File;
import java.io.FilenameFilter;
class FileAccept implements FilenameFilter //FilenameFilter是接口,只有一個accept方法
{
String str = null;
FileAccept(String s)
{
str = "."+s;
}
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.endsWith(str);
}
}
public class Example9_1 {
public static void main(String[] args) {
File dir = new File("D:/ch1");
FileAccept acceptCondition = new FileAccept("java");
File fileName[] = dir.listFiles(acceptCondition);//調用listFiles方法時纔會去調用accept方法。accept方法在創建對象時不會調用
for(int i = 0 ; i < fileName.length; i++)
{
System.out.printf("\n文件名稱:%s,長度:%d",
fileName[i].getName(),fileName[i].length());
}
boolean boo = false;
if(fileName.length > 0)
{
boo = fileName[0].delete();
if(boo)
System.out.printf("\n文件:%s被刪除:",fileName[0].getName());
}
}
}
這段代碼是使用Java字節流,如果有中文,讀寫時可能會出現亂碼
package cn.swpu;
import java.io.*;
public class Example9_3 {
public static void main(String[] args) {
File file = new File("D:/hello.txt");
byte[] b = "歡迎welcome".getBytes();
try
{
FileOutputStream out = new FileOutputStream(file);
out.write(b);
out.close();
FileInputStream in = new FileInputStream(file);
//in.read(b, 1, 3);
int n = 0;
String str;
while((n = in.read(b,0,6)) != -1)
{
str = new String(b,0,n);
System.out.println(str);
}
in.close();
str = null;
}catch(IOException e)
{
System.out.println(e);
}
}
}
這段是使用字符流:
package cn.swpu;
import java.io.*;
public class Example9_4 {
public static void main(String[] args) {
File file = new File("D:/hello.txt");
char b[] = "歡迎welcome".toCharArray();
try{
FileWriter out = new FileWriter(file,true);
out.write(b);
out.write("來到北京");
out.flush();
out.close();
FileReader in = new FileReader(file);
int n = 0;
String str;
while((n = in.read(b, 0, 5)) != -1)
{
str = new String(b,0,n);
System.out.println(str);
}
in.close();
str = null;
}catch(IOException ex){System.out.println(ex);}
}
}
這段是使用了緩衝流,只能用在字符流中:
package cn.swpu;
import java.io.*;
public class Example9_5 {
public static void main(String[] args) {
File readFile = new File("D:/hello.txt");
File writeFile = new File("D:/Hello1.txt");
try
{
FileReader inOne = new FileReader(readFile);
BufferedReader in = new BufferedReader(inOne);
FileWriter outTwo = new FileWriter(writeFile);
BufferedWriter out = new BufferedWriter(outTwo);
String s = null;
int i = 0;
while((s = in.readLine() ) != null)
{
i++;
out.write(i+":"+s);
out.newLine();
}
out.flush();
out.close();
outTwo.close();
while((s = in.readLine()) != null)
{
System.out.println( s );
}
inOne.close();
in.close();
}catch(IOException ex)
{
System.out.println(ex);
}
}
}
這段是使用對象流:
package cn.swpu;
import java.io.*;
@SuppressWarnings("serial")
class Goods implements Serializable
{
String name ;
double unitPrice;
public Goods(String name,double unitPrice)
{
this.name = name;
this.unitPrice = unitPrice;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setUnitPrice(double unitPrice)
{
this.unitPrice = unitPrice;
}
public double getUnitPrice()
{
return this.unitPrice;
}
}
public class Example9_10 {
public static void main(String[] args) {
Goods TV1 = new Goods("HaierTV",3468);
try{
FileOutputStream fileOut = new FileOutputStream("D:/a.txt");
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(TV1);
FileInputStream fileIn = new FileInputStream("D:/a.txt");
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
Goods TV2 = (Goods)objectIn.readObject();
// TV2.setUnitPrice(888);
// TV2.setName("GREATWALL");
System.out.printf("\nTV1:%s,%f",TV1.getName(),TV1.getUnitPrice());
System.out.printf("\nTV2:%s,%f",TV2.getName(),TV2.getUnitPrice());
}catch(IOException ex) {}
catch(ClassNotFoundException ex){}
}
}