首页 > 编程语言 > 详细

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

时间:2019-09-25 20:31:31      阅读:91      评论:0      收藏:0      [点我收藏+]
 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 }

 

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

原文:https://www.cnblogs.com/dongao/p/11586538.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!