从文件读入《飘》的英文版,并将结果输出到文件中

要求一:

实现对英文版《飘》的字母出现次数统计

 package File;

 import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; public class File_Test {
public static void main(String[] args) {
int []arr=new int [100];//数组存入 FileInputStream fis=null;
FileOutputStream fos=null; try {
fis=new FileInputStream("D:\\WiththeWind.txt");//《飘》文件位置
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int temp;
try {
while((temp=fis.read())!=-1) {
if(((char)temp>='A'&&(char)temp<='Z')||((char)temp>='a'&&(char)temp<='z'))
arr[temp-65]++;//存入数组
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
} for(int i=0;i<100;i++) {
if(arr[i]!=0)
System.out.println((char)(i+65)+":"+arr[i]);
} try {
fos=new FileOutputStream("1024.txt");//在当前目录下写入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} for(int i=0;i<100;i++) {
if(arr[i]!=0)
{
try {
fos.write((char)(i+65));//写入字母
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.write(":".getBytes());//写入冒号:
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.write(String.valueOf(arr[i]).getBytes());//写入数组的值,即字母的个数
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.write("\n".getBytes());//换行
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.flush();//刷新
} catch (IOException e) {
e.printStackTrace();
}
}
}
} }

要求二:

实现单词的统计。

 package File;

 import java.io.*;

 public class File_test_01 {
public static void main(String[] args) {
try {
FileWriter fw=new FileWriter("d:\\result.txt");
FileReader fr=new FileReader("d:\\WiththeWind.txt");
StringBuffer buffer=new StringBuffer();
int ch,count=0;//ch记录当前字符,count记录数组位置
boolean flag;
String temp; //两个数组记录时下标同步
String [] arr=new String [200000];//记录单词
int [] x=new int[200000];//记录个数 //循环按照字符依次读入
while((ch=fr.read())!=-1) {
// System.out.println((char)ch);//成功读入字符 //如果是字母,则存储当前单词
if(((char)ch>='a'&&(char)ch<='z')||((char)ch>='A'&&(char)ch<='Z')) {
if((char)ch>='A'&&(char)ch<='Z')
ch=ch+32;
buffer.append((char)ch);
// System.out.println("buffer1="+buffer);
}
else
{
//如果buffer没有字母,即不是单词
if(buffer.length()>0)
{
flag=true;
for(int i=0;arr[i]!=null;i++) //查重
if(arr[i].equals(buffer.toString())) {
x[i]++;
flag=false;
break;
}
// System.out.println("buffer2="+buffer);
if(flag) //如果不重复
{
arr[count++]=buffer.toString();
x[count]++;
// System.out.println("arr="+arr[count-1]);
}
buffer.delete(0, buffer.length());//清空buffer
}
}
}
for(int i=0;arr[i]!=null;i++) {
temp=String.valueOf(x[i]);
fw.write(arr[i]+":"+temp+"\t");
if(i%5==0)
fw.write("\n");
}
}
catch (Exception e) {
System.out.println(e.toString());
}
}
}

结果:

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

  1. java===java基础学习(5)---文件读取,写入操作

    文件的写入读取有很多方法,今天学到的是Scanner和PrintWriter 文件读取 Scanner in = new Scanner(Paths.get("file.txt") ...

  2. testbench的设计 文件读取和写入操作 源代码

    十大基本功之 testbench 1. 激励的产生 对于 testbench 而言,端口应当和被测试的 module 一一对应.端口分为 input,output 和 inout 类型产生激励信号的时 ...

  3. 记-Golang日志文件读取及写入操作

    Golang语言的 os 包中OpenFile 函数,如下所示: func OpenFile(name string, flag int, perm FileMode) (*File, error) ...

  4. Java底层代码实现单文件读取和写入(解决中文乱码问题)

    需求: 将"E:/data/车站一次/阿坝藏族羌族自治州.csv"文件中的内容读取,写入到"E:/data//车站一次.csv". 代码: public cla ...

  5. Apache commons-io实现单文件读取和写入

    Apache commons-io提供了很多类,这里只介绍FileUtils类. FileUtils类提供了一些操作文件对象的实用方法,包括文件的读取.写入.复制和比较等. 比如逐句读取和写入一个文件 ...

  6. python文件读取和写入案例

    python文件读取和写入案例  直接上代码吧 都是说明 百度上找了很多,最终得出思路 没有直接可以读取修改的扩展,只能先读取,然后复制一份,然后在复制出来的文件里面追加保存 然后删除读的那个,但是缺 ...

  7. HDFS数据流-剖析文件读取及写入

    HDFS数据流-剖析文件读取及写入 文件读取 1. 客户端通过调用FileSystem对象的open方法来打开希望读取的文件,对于HDFS来说,这个对象是分布式文件系统的一个实例.2. Distrib ...

  8. 使用java 程序创建格式为utf-8文件的方法(写入和读取json文件)

    使用java 程序创建格式为utf-8文件的方法:  try{            File file=new   File("C:/11.jsp");              ...

  9. properties文件读取与写入

    将peoperties文件的读取和写入封装成了一个工具类: import java.io.BufferedInputStream; import java.io.FileInputStream; im ...

随机推荐

  1. flask框架(六): 实现支持正则的路由

    一:默认路由 @app.route('/user/<username>') @app.route('/post/<int:post_id>') @app.route('/pos ...

  2. HDU 5793 A Boring Question ——(找规律,快速幂 + 求逆元)

    参考博客:http://www.cnblogs.com/Sunshine-tcf/p/5737627.html. 说实话,官方博客的推导公式看不懂...只能按照别人一样打表找规律了...但是打表以后其 ...

  3. [BZOJ4033]:[HAOI2015]树上染色(树上DP)

    题目传送门 题目描述 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并将其他的N-K个点染成白色.将所有点染色后,你会获得黑点两两之间的距离加 ...

  4. 「CF442C」 Artem and Array

    题目链接 戳我 \(Solution\) 观察发现如果一个数两边都比他大,删掉他可以保证最优,这个应该是显然的.这个东西用单调栈维护一下,最后剩下的就是个单调递减或单调递增的数列,从小到大排个序取前面 ...

  5. springMVC中的ModelAndView说明

    ModelAndView 类别就如其名称所示,是代表了Spring Web MVC程式中呈现画面时所使用Model资料物件与View资料物件,由于Java程式中一次只能返回一个物件,所以ModelAn ...

  6. (一)C语言的四大数据类型

  7. CodeForces 137A

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Polyca ...

  8. java判断一个字符串是否为数字型

    摘自:https://blog.csdn.net/qq_42133100/article/details/92158507 方法一:用JAVA自带的函数(只能判断正整数 ) 2 public stat ...

  9. -webkit-scrollbar 的使用,滚动条的隐藏

    滚动条的隐藏 -webkit-scrollbar 是一个伪类选择器 设置滚动条的样式 例如滚动条的隐藏 元素::-webkit-scrollbar{ width:0; }

  10. 2.HDFS和HA

    1.HDFS简介 DataNode NameNode SecondaryNameNode HDFS文件权限 2.HDFS小结 3.HDFS交互操作 4.HDFS编程访问接口