加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

用Java实现对英文版《飘》的文件读取与写入操作

发布时间:2020-12-15 07:35:05 所属栏目:Java 来源:网络整理
导读:1 package File; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 8 public class File_Test { 9 public static void main(String[] args) { 10 int []a
 1 package File;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 
 8 public class File_Test {
 9     public static void main(String[] args) {
10         int []arr=new int [100];//数组存入
11         
12         FileInputStream fis=null;
13         FileOutputStream fos=null;
14     
15         try {
16             fis=new FileInputStream("D:WiththeWind.txt");//《飘》文件位置
17         } catch (FileNotFoundException e) {
18             e.printStackTrace();
19         }    
20         int temp;
21         try {
22             while((temp=fis.read())!=-1) {
23                 if(((char)temp>=‘A‘&&(char)temp<=‘Z‘)||((char)temp>=‘a‘&&(char)temp<=‘z‘))
24                 arr[temp-65]++;//存入数组
25             }
26         } catch (IOException e) {
27             e.printStackTrace();
28         }finally {
29             try {
30                 fis.close();
31             } catch (IOException e) {
32                 e.printStackTrace();
33             }
34         }
35         
36         for(int i=0;i<100;i++) {
37             if(arr[i]!=0)
38                 System.out.println((char)(i+65)+":"+arr[i]);
39         }
40         
41         try {
42             fos=new FileOutputStream("1024.txt");//在当前目录下写入文件
43         } catch (FileNotFoundException e) {
44             e.printStackTrace();
45         }
46         
47         for(int i=0;i<100;i++) {
48             if(arr[i]!=0)
49             {
50                 try {
51                     fos.write((char)(i+65));//写入字母
52                 } catch (IOException e) {
53                     e.printStackTrace();
54                 }
55                 try {
56                     fos.write(":".getBytes());//写入冒号:
57                 } catch (IOException e) {
58                     e.printStackTrace();
59                 }
60                 try {
61                     fos.write(String.valueOf(arr[i]).getBytes());//写入数组的值,即字母的个数
62                 } catch (IOException e) {
63                     e.printStackTrace();
64                 }
65                 try {
66                     fos.write("n".getBytes());//换行
67                 } catch (IOException e) {
68                     e.printStackTrace();
69                 }
70                 try {
71                     fos.flush();//刷新
72                 } catch (IOException e) {
73                     e.printStackTrace();
74                 }
75             }
76         }
77     }
78 
79 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读