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(); } } }
新聞熱點
疑難解答