zoukankan      html  css  js  c++  java
  • 用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 []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("
    ".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 }

    要求二:

    实现单词的统计。

     1 package File;
     2 
     3 import java.io.*;
     4 
     5 public class File_test_01 {
     6     public static void main(String[] args) {
     7         try {
     8             FileWriter fw=new FileWriter("d:\result.txt");
     9             FileReader fr=new FileReader("d:\WiththeWind.txt");
    10             StringBuffer buffer=new StringBuffer();
    11             int ch,count=0;//ch记录当前字符,count记录数组位置
    12             boolean flag; 
    13             String temp;
    14             
    15             //两个数组记录时下标同步
    16             String [] arr=new String [200000];//记录单词
    17             int [] x=new int[200000];//记录个数
    18             
    19             //循环按照字符依次读入
    20             while((ch=fr.read())!=-1) {
    21 //                System.out.println((char)ch);//成功读入字符
    22                 
    23                 //如果是字母,则存储当前单词
    24                 if(((char)ch>='a'&&(char)ch<='z')||((char)ch>='A'&&(char)ch<='Z')) {
    25                     if((char)ch>='A'&&(char)ch<='Z')
    26                         ch=ch+32;
    27                     buffer.append((char)ch);
    28 //                    System.out.println("buffer1="+buffer);
    29                 }
    30                 else
    31                 {
    32                     //如果buffer没有字母,即不是单词
    33                     if(buffer.length()>0)
    34                     {
    35                         flag=true;
    36                         for(int i=0;arr[i]!=null;i++)  //查重
    37                             if(arr[i].equals(buffer.toString())) {
    38                                 x[i]++;
    39                                 flag=false;
    40                                 break;
    41                             }
    42 //                        System.out.println("buffer2="+buffer);
    43                         if(flag) //如果不重复
    44                         {
    45                             arr[count++]=buffer.toString();
    46                             x[count]++;
    47 //                            System.out.println("arr="+arr[count-1]);
    48                         }
    49                         buffer.delete(0, buffer.length());//清空buffer
    50                     }
    51                 }
    52             }
    53             for(int i=0;arr[i]!=null;i++) {
    54                 temp=String.valueOf(x[i]);
    55                 fw.write(arr[i]+":"+temp+"	");
    56                 if(i%5==0)
    57                     fw.write("
    ");
    58             }
    59         }
    60         catch (Exception e) {
    61             System.out.println(e.toString());
    62         }
    63     }
    64 }

    结果:

  • 相关阅读:
    BZOJ2140: 稳定婚姻(tarjan解决稳定婚姻问题)
    BZOJ2124: 等差子序列(树状数组&hash -> bitset 求是否存在长度为3的等差数列)
    HDU 1217 Arbitrage(Bellman-Ford判断负环+Floyd)
    HDU 2112 Today(Dijkstra+map)
    HDU 2066 一个人的旅行(dijkstra水题+判重边)
    POJ 1511 Invitation Cards(Dijkstra(优先队列)+SPFA(邻接表优化))
    HDU 2544 最短路(floyd+bellman-ford+spfa+dijkstra队列优化)
    POJ 2431 Expedition (贪心 + 优先队列)
    POJ 3253 Fence Repair(哈夫曼编码)
    优先队列的使用(转)
  • 原文地址:https://www.cnblogs.com/dongao/p/11586538.html
Copyright © 2011-2022 走看看