1,編寫一個程序,讀取文件test.txt的內容並在控制台輸出。如果源文件不存在,則顯示相應的錯誤信息。
1 package src; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileReader; 6 import java.io.IOException; 7 8 public class Test { 9 10 public static void main(String[] args) { 11 File f = new File("src\\test1.txt");//文件在src名為test1.txt中 12 try { 13 FileReader fr = new FileReader(f);//將文件讀取到內容中 14 int m; 15 while((m=fr.read())!=-1){ 16 System.out.print((char)(m)); 17 } 18 } catch (FileNotFoundException e) { 19 // TODO Auto-generated catch block 20 e.printStackTrace(); 21 } catch (IOException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } 25 } 26 }
2,編寫一個程序實現如下功能,從當前目錄下的文件fin.txt中讀取80個字節(實際讀到的字節數可能比80少)並將讀來的字節寫入當前目錄下的文件fout.txt中
1 package src; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 9 public class Test { 10 11 public static void main(String[] args) { 12 File f1 = new File("src\\fin.txt");//src下fin.txt文件 13 File f2 = new File("src\\fout.txt");//src下fout.txt文件 14 15 try { 16 FileInputStream fis = new FileInputStream(f1); 17 FileOutputStream fos = new FileOutputStream(f2); 18 19 byte[] temp = new byte[80];//定義一個字節數組 20 fis.read(temp);//讀到內存中 21 fos.write(temp);//寫到文件 22 23 fis.close(); 24 fos.close(); 25 } catch (FileNotFoundException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } catch (IOException e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } 32 33 } 34 }
3,使用java的輸入/輸出流技術將一個文本文件的內容按行讀出,每讀出一行就順序添加行號,並寫入到另一個文件中。
1 package src; 2 3 import java.io.BufferedReader; 4 import java.io.BufferedWriter; 5 import java.io.File; 6 import java.io.FileNotFoundException; 7 import java.io.FileReader; 8 import java.io.FileWriter; 9 import java.io.IOException; 10 11 public class Test { 12 13 public static void main(String[] args) { 14 File f1 = new File("src\\fin.txt");//src下fin.txt文件 15 File f2 = new File("src\\fout.txt");//src下fout.txt文件 16 17 try { 18 FileReader fr = new FileReader(f1); 19 FileWriter fw = new FileWriter(f2); 20 21 BufferedReader br = new BufferedReader(fr); 22 BufferedWriter bw = new BufferedWriter(fw); 23 24 String temp; 25 int i=1; 26 while((temp=br.readLine())!=null){ 27 bw.write(i+","+temp); 28 bw.newLine();//換行 29 i++; 30 } 31 bw.flush();//把緩沖區內容寫到文件 32 br.close(); 33 bw.close(); 34 br.close(); 35 bw.close(); 36 } catch (FileNotFoundException e) { 37 // TODO Auto-generated catch block 38 e.printStackTrace(); 39 } catch (IOException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 } 44 }
4,編寫一個程序,接收從鍵盤輸入的數據,並把從鍵盤輸入的內容寫到input.txt文件中,如果輸入"quit",則程序結束。
1 package src; 2 3 import java.io.File; 4 import java.io.FileWriter; 5 import java.io.IOException; 6 import java.util.Scanner; 7 8 public class Test { 9 10 public static void main(String[] args) { 11 File f = new File("src\\input.txt"); 12 try { 13 FileWriter fw = new FileWriter(f); 14 Scanner scanner = new Scanner(System.in); 15 String temp; 16 while(!((temp=scanner.nextLine()).equals("quit"))){ 17 fw.write(temp); 18 } 19 fw.close(); 20 } catch (IOException e) { 21 // TODO Auto-generated catch block 22 e.printStackTrace(); 23 } 24 } 25 }
5,編寫一個程序實現如下功能,文件fin.txt是無行結構(無換行符)的漢語文件,從fin中讀取字符,寫入文件fou.txt中,每40個字符一行(最后一行可能少於40個字)
1 package src; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.io.FileReader; 6 import java.io.FileWriter; 7 import java.io.IOException; 8 public class Test { 9 10 public static void main(String[] args) { 11 File f1=new File("src\\fin.txt"); 12 File f2=new File("src\\fout.txt"); 13 try { 14 FileReader fr=new FileReader(f1); 15 FileWriter fw=new FileWriter(f2); 16 17 char temp[]=new char[40]; 18 int len; 19 while((len=fr.read(temp))!=-1) 20 { 21 if(len==40) 22 fw.write(new String(temp)+"\n"); 23 else 24 fw.write(temp, 0, len); 25 } 26 fr.close(); 27 fw.close(); 28 29 } catch (FileNotFoundException e) { 30 // TODO 自動生成的 catch 塊 31 e.printStackTrace(); 32 } catch (IOException e) { 33 // TODO 自動生成的 catch 塊 34 e.printStackTrace(); 35 } 36 } 37 }