標籤:style char cat pack exce finally span 使用 except
/*
字元流:
Reader Writer。
需求:
在硬碟上建立一個檔案並寫入一些資料。
*/
import java.io.*;
/*
//建立FileWriter對象,調用window資源,在指定位置建立好了資料存放的目的地。
//如果指定目錄下,已存在要建立的檔案,那麼會被覆蓋。
FileWriter fw = new FileWriter("demo.txt");
//通過流對象write方法將字串寫入到流中。
fw.write("abcde");
//關閉資源,清空它先。
fw.close();
需求:對原有檔案進行資料的續寫。
1,建立對象。
2,使用讀寫方法。
3,關閉資源。
*/
class FileWriterDemo
{
public static void main(String[] args) //throws IOException
{
FileWriter fw = null;
try
{
fw = new FileWriter("k:\\demo.txt",true);
fw.write("qqqqq\r\nmmmm");
}
catch (IOException e)
{
System.out.println(e.toString());
}
finally
{
if(fw!=null)
try
{
fw.close();
}
catch (IOException e)
{
System.out.println("close:"+e.toString());
}
}
}
}
package model;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class FileReadDemo {public static void main(String[] args){try {FileReader fr = new FileReader("E:\\demo.txt");char[] arr = new char[1024];int num = 0; try {while((num = fr.read(arr))!=-1) { System.out.println(new String(arr,0,num)); }} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
java讀寫 功能