1,編寫一個程式,讀取檔案test.txt的內容並在控制台輸出。如果源檔案不存在,則顯示相應的錯誤資訊。
package src; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Test { public static void main(String[] args) { File f = new File("src\\test.txt");//檔案在src名為test.txt中 try { FileReader fr = new FileReader(f);//將檔案讀取到內容中 int m; while((m=fr.read())!=-){ System.out.print((char)(m)); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
2,編寫一個程式實現如下功能,從目前的目錄下的檔案fin.txt中讀取80個位元組(實際讀到的位元組數可能比80少)並將讀來的位元組寫入目前的目錄下的檔案fout.txt中
package src; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Test { public static void main(String[] args) { File f = new File("src\\fin.txt");//src下fin.txt檔案 File f = new File("src\\fout.txt");//src下fout.txt檔案 try { FileInputStream fis = new FileInputStream(f); FileOutputStream fos = new FileOutputStream(f); byte[] temp = new byte[];//定義一個位元組數組 fis.read(temp);//讀到記憶體中 fos.write(temp);//寫到檔案 fis.close(); fos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
3,使用java的輸入/輸出流技術將一個文字檔的內容按行讀出,每讀出一行就順序添加行號,並寫入到另一個檔案中。
package src; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Test { public static void main(String[] args) { File f = new File("src\\fin.txt");//src下fin.txt檔案 File f = new File("src\\fout.txt");//src下fout.txt檔案 try { FileReader fr = new FileReader(f); FileWriter fw = new FileWriter(f); BufferedReader br = new BufferedReader(fr); BufferedWriter bw = new BufferedWriter(fw); String temp; int i=; while((temp=br.readLine())!=null){ bw.write(i+","+temp); bw.newLine();//換行 i++; } bw.flush();//把緩衝區內容寫到檔案 br.close(); bw.close(); br.close(); bw.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
4,編寫一個程式,接收從鍵盤輸入的資料,並把從鍵盤輸入的內容寫到input.txt檔案中,如果輸入"quit",則程式結束。
package src; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class Test { public static void main(String[] args) { File f = new File("src\\input.txt"); try { FileWriter fw = new FileWriter(f); Scanner scanner = new Scanner(System.in); String temp; while(!((temp=scanner.nextLine()).equals("quit"))){ fw.write(temp); } fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
5,編寫一個程式實現如下功能,檔案fin.txt是無行結構(無分行符號)的漢語檔案,從fin中讀取字元,寫入檔案fou.txt中,每40個字元一行(最後一行可能少於40個字)
package src; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Test { public static void main(String[] args) { File f=new File("src\\fin.txt"); File f=new File("src\\fout.txt"); try { FileReader fr=new FileReader(f); FileWriter fw=new FileWriter(f); char temp[]=new char[]; int len; while((len=fr.read(temp))!=-) { if(len==) fw.write(new String(temp)+"\n"); else fw.write(temp, , len); } fr.close(); fw.close(); } catch (FileNotFoundException e) { // TODO 自動產生的 catch 塊 e.printStackTrace(); } catch (IOException e) { // TODO 自動產生的 catch 塊 e.printStackTrace(); } } }