try{}
catch(IOException e)
{
]
e.getMessage()方法可用於找出對象的詳細信息
e.getClass().getName()能夠得到異常對象的實際類型
FileWriter out=new FileWriter("output.txt");
等價於 OutputStreamWriter out=new OutputStringWriter(new FIleoutputStream("output.txt"));
PrintWriter out=new PrintWriter(new FileWriter("employee.txt"));
等價於PrintWriter out=new PrinWriter(new FileOutputStream("employee.txt"));
String name="Harry Hacker";
double salary=7500;
out.print(name);
out.print(' ');
out.print(salary);
上面的代碼會把 Harry Hacker 7500寫到out中,這些字符接着被轉化爲字節最終顯示到employee.txt中。
BufferedReader in=new BufferedReader(new FileReader("employee.txt"));
String line;
while((line=in.readline())!=null)
{
do something with the line
}
BufferedReader in2=new BufferedReader(new InputStreamReader(url.openStream()));
存儲可變類型對象
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("employee.txt"));
Employee harry=new Employee("Harry",5000,1989,10,1);
out.writeObject(harry);
ObjectInputStream in =new ObjectInputStream(new FileInputStream(employee.txt));
Employee e1=(Employee)in.readObject();
File f=new File("test.txt");
會在當前目錄下的test.txt文件創建一個File對象,如果提供的文件名沒有對應的文件,那麼該調用不會創建一個文件。實際上,從一個File對象創建的的一個文件可以通過幾個流類的構造器完成,或者使用FILE類的createNewFile方法。
利用File可以創建目錄
流操作主要需要掌握,字節,字符,字串之間的轉化關係。