用Java实现对英文版《飘》的文件读取与写入操作
从文件读入《飘》的英文版,并将结果输出到文件中
要求一:
实现对英文版《飘》的字母出现次数统计
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实现对英文版《飘》的文件读取与写入操作的更多相关文章
- java===java基础学习(5)---文件读取,写入操作
文件的写入读取有很多方法,今天学到的是Scanner和PrintWriter 文件读取 Scanner in = new Scanner(Paths.get("file.txt") ...
- testbench的设计 文件读取和写入操作 源代码
十大基本功之 testbench 1. 激励的产生 对于 testbench 而言,端口应当和被测试的 module 一一对应.端口分为 input,output 和 inout 类型产生激励信号的时 ...
- 记-Golang日志文件读取及写入操作
Golang语言的 os 包中OpenFile 函数,如下所示: func OpenFile(name string, flag int, perm FileMode) (*File, error) ...
- Java底层代码实现单文件读取和写入(解决中文乱码问题)
需求: 将"E:/data/车站一次/阿坝藏族羌族自治州.csv"文件中的内容读取,写入到"E:/data//车站一次.csv". 代码: public cla ...
- Apache commons-io实现单文件读取和写入
Apache commons-io提供了很多类,这里只介绍FileUtils类. FileUtils类提供了一些操作文件对象的实用方法,包括文件的读取.写入.复制和比较等. 比如逐句读取和写入一个文件 ...
- python文件读取和写入案例
python文件读取和写入案例 直接上代码吧 都是说明 百度上找了很多,最终得出思路 没有直接可以读取修改的扩展,只能先读取,然后复制一份,然后在复制出来的文件里面追加保存 然后删除读的那个,但是缺 ...
- HDFS数据流-剖析文件读取及写入
HDFS数据流-剖析文件读取及写入 文件读取 1. 客户端通过调用FileSystem对象的open方法来打开希望读取的文件,对于HDFS来说,这个对象是分布式文件系统的一个实例.2. Distrib ...
- 使用java 程序创建格式为utf-8文件的方法(写入和读取json文件)
使用java 程序创建格式为utf-8文件的方法: try{ File file=new File("C:/11.jsp"); ...
- properties文件读取与写入
将peoperties文件的读取和写入封装成了一个工具类: import java.io.BufferedInputStream; import java.io.FileInputStream; im ...
随机推荐
- SpringMVC——MVC执行流程底层剖析
SpringMVC流程图如上面所示,根据上图,串联一下底层源码: 1.在DispatcherServlet中找到doDisPatch 2.观察方法体,然后找到getHandler方法 3.点进方法,发 ...
- Ansible常用模块之命令类模块
Command模块 在远程节点上执行命令 [root@tiandong ~]# ansible all -m command -a "ls" 在远程主机上执行ls命令. [root ...
- 面试准备一个访问一个URL的过程简版
客户端获取URL - > DNS解析 - > TCP连接 - >发送HTTP请求 - >服务器处理请求 - >返回报文 - >浏览器解析渲染页面 - > TC ...
- js 原型链、构造函数、原型与实例之间的关系
面向对象编程都会涉及到继承这个概念,JS中实现继承的方式主要是通过原型链的方法. 一.构造函数.原型与实例之间的关系 每创建一个函数,该函数就会自动带有一个 prototype 属性.该属性是个指针, ...
- MySQL 主从复制 (CentOS 7)
1.主从复制原理 (1) 当master 服务器上的数据发生改变时,则将其改变写入二进制日志文件中: (2) slave服务器会在一定时间间隔内对 master 服务器上的二进制日志进行探测,探测其是 ...
- window7上爬虫框架Scrapy的安装 --错误分析lxml
本文讲解的是python已经安装成功的情况下,且pip可以使用 版本:python2.7.10 #安装 pip install Scrapy 在安装Scrapy框架时总会报lxml安装不了的各种错误, ...
- Ubuntu——关于以root权限操作和启用root账户的讨论
概括性的说,在Ubuntu下面,推荐用户加入到sudo之后,使用命令: sudo -i 来以管理员权限进行操作. 而不推荐用户直接登录root用户. 具体内参考:https://help.ubuntu ...
- oracle表结构表数据导入导出
--------------------------------------imp/exp------------------------------------------------------- ...
- 理解MQ
1.定义 2.消息队列比较 参考文章:https://www.jianshu.com/p/068b8d1610ee
- vue-template-compiler作用
vue-template-compiler的作用是什么: 看起来 template-compiler是给parse函数使用的, 那么parse函数是干什么的呢 先看一下parse的结果: 结论:使用v ...