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

要求一:

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

 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. HZOJ 20190722 visit (组合数学+数论)

    考试T2,考试时打了个$O(n^3)$dp暴力,思路还是很好想的,但细节也不少,然后滚动数组没清空,而且题又看错了,只得了10pts,真是血的教训. 题解: 其实看数据范围,给出了模数是否为质数,其实 ...

  2. 【CUDA 基础】0.0 腾讯云CUDA环境搭建

    title: [CUDA 基础]0.0 腾讯云CUDA环境搭建 categories: CUDA Freshman tags: CUDA 环境搭建 toc: true date: 2018-02-13 ...

  3. 2019牛客暑期多校训练营(第三场)F 单调队列

    题意 给一个\(n\times n\)的矩阵,找一个最大的子矩阵使其中最大值与最小值的差小于等于\(m\). 分析 枚举子矩阵的上下边界,同时记录每一列的最大值和最小值. 然后枚举右边界,同时用两个单 ...

  4. sh_14_字符串定义和遍历

    sh_14_字符串定义和遍历 str1 = "hello python" str2 = '我的外号是"大西瓜"' print(str2) print(str1[ ...

  5. Java线程之Timer

    简述 java.util.Timer是一个定时器,用来调度线程在某个时间执行.在初始化Timer时,开启一个线程循环提取TaskQueue任务数组中的任务, 如果任务数组为空,线程等待直到添加任务: ...

  6. vue 项目安装

    安装stylus npm install stylus stylus-loader --save-dev

  7. axios 的坑

    必须安装axios 和qs 1.main.js中的配置 import axios from 'axios' import qs from 'qs'; axios.defaults.headers.po ...

  8. python-获取程序的路径

    python获取当前路径 import os,sys 使用sys.path[0].sys.argv[0].os.getcwd().os.path.abspath(file).os.path.realp ...

  9. iOS 应用"无法安装应用程序 因为证书无效"的解决方案

    原因是由于iOS7.1要安装企业应用,url必须是https的,不能是http,这就要求我们的服务器要支持https.因此,只要将原链接: itms-services://?action=downlo ...

  10. js前台传数组,java后台接收转list,前后台用正则校验

    前台,传参数时,将数组对象转换成json串,后台java收到后用 JSONArray.fromObject 转成集合. 前台js:var params = {"FileNameList&qu ...