1,編寫一個程序,讀取文件test.txt的內(nèi)容并在控制臺輸出。如果源文件不存在,則顯示相應(yīng)的錯誤信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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); //將文件讀取到內(nèi)容中 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,編寫一個程序?qū)崿F(xiàn)如下功能,從當(dāng)前目錄下的文件fin.txt中讀取80個字節(jié)(實際讀到的字節(jié)數(shù)可能比80少)并將讀來的字節(jié)寫入當(dāng)前目錄下的文件fout.txt中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
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 []; //定義一個字節(jié)數(shù)組 fis.read(temp); //讀到內(nèi)存中 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的輸入/輸出流技術(shù)將一個文本文件的內(nèi)容按行讀出,每讀出一行就順序添加行號,并寫入到另一個文件中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
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(); //把緩沖區(qū)內(nèi)容寫到文件 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,編寫一個程序,接收從鍵盤輸入的數(shù)據(jù),并把從鍵盤輸入的內(nèi)容寫到input.txt文件中,如果輸入"quit",則程序結(jié)束。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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,編寫一個程序?qū)崿F(xiàn)如下功能,文件fin.txt是無行結(jié)構(gòu)(無換行符)的漢語文件,從fin中讀取字符,寫入文件fou.txt中,每40個字符一行(最后一行可能少于40個字)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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(); } } } |