IO 练习

 1 package Aug14.IO;
 2 
 3 import java.io.*;
 4 
 5 public class TestIO2 {
 6 
 7     public static void main(String[] args) {
 8         File file = new File("D://Text.txt");
 9         File f1=new File("D://Test1.txt");
10         File f2=new File("D://Test2.txt");
11         FileReader fr = null;
12         int count = 0;
13         try {
14             fr = new FileReader(file);
15             BufferedReader br = new BufferedReader(fr);
16             
17             FileWriter fw1 = new FileWriter(f1);
18             BufferedWriter bw1 = new BufferedWriter(fw1);
19             
20             FileWriter fw2 = new FileWriter(f2);
21             BufferedWriter bw2 = new BufferedWriter(fw2);
22             
23             String s = null;
24             while ((s = br.readLine()) != null) {
25 
26                 if (count % 2 != 0) {
27                     bw1.write(s);
28                     bw1.newLine();
29                     //System.out.println(s);
30 
31                 } else {
32                     bw2.write(s);
33                     bw2.newLine();
34                     //System.out.println(s);
35                 }
36 
37                 count++;
38             }
39             
40             bw1.close();
41             bw2.close();
42             br.close();
43             fr.close();
44             fw1.close();
45             fw2.close();
46 
47         } catch (FileNotFoundException e) {
48 
49             e.printStackTrace();
50         } catch (IOException e) {
51 
52             e.printStackTrace();
53         }
54 
55     }
56 
57 }
 1 package Aug14.IO;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.File;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileReader;
 7 
 8 public class TestIO3 {
 9 
10     public static void main(String[] args) {
11         File file = new File("D://Text1.txt");
12         FileReader fr = null;
13         String s = null;
14         int count = 0;
15         int length=0;
16         try {
17             fr = new FileReader(file);
18             BufferedReader br = new BufferedReader(fr);
19             while ((s = br.readLine()) != null) {
20                 int n = s.split(" ").length;
21                 count =count+n;
22                 length+=s.length()-n;
23             }
24             System.out.println("the number of words : " + count);
25             System.out.println("the length of all word: " + length);
26             System.out.printf("the average word length: %.2f " ,(double) length/count);
27         } catch (Exception e) {
28 
29             e.printStackTrace();
30         }
31 
32     }
33 
34 }
 1 package Aug14.IO;
 2 
 3 
 4 import java.io.*;
 5 
 6 public class Test1 {
 7 
 8     public static void main(String[] args) {
 9         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10         File file=new File("D://Test12.txt");
11         
12         try {
13             System.out.print("Enter a File with Path: ");
14             String  filename=br.readLine();
15             
16             File file1=new File(filename);
17             if(file1.exists()&&file1.isFile()){
18                 System.out.print("Enter a word to searth: ");
19                 String  word=br.readLine();
20                 InputStream in=new FileInputStream(file1);
21                 byte data[]=new byte [1024];
22                 int len=in.read(data);
23                 String s=new String (data,0,len);
24                 if(s.indexOf(word)!=-1)
25                 {
26                     int n=s.split(word).length-1; //Word 出现个数
27                     
28                     System.out.println("The word is exits! count= "+n);
29                     System.out.println(s);
30                 
31                 }
32                 else
33                     System.out.println("The word is not exits!");
34             }
35             else
36                 System.out.println("The file  is not exits!");
37             
38         } catch (IOException e) {
39 
40 
41         }
42     }
43 
44 }

 

posted on 2014-08-15 19:48  @冰糖  阅读(190)  评论(0编辑  收藏  举报

导航