用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 ...
随机推荐
- B. Tell Your World(几何数学 + 思维)
B. Tell Your World time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Linux之防火墙iptables
一.检查iptables服务状态 1.首先检查iptables服务的状态 [root@bogon ~]# service iptables status iptables: Firewall is n ...
- JavaWeb-SpringSecurity自定义登陆页面
系列博文 项目已上传至guthub 传送门 JavaWeb-SpringSecurity初认识 传送门 JavaWeb-SpringSecurity在数据库中查询登陆用户 传送门 JavaWeb-Sp ...
- 0.2 IDEA配置
一.IDEA配置maven 在启动配置设置清理方式:clean jetty:run maven版本以及本地setting和repository JRE版本以及编码格式:-Dfile.encoding= ...
- IDEA找回Run Dashboard
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- Nginx-rtmp之监听端口的管理
1. 概述 监听端口属于 server 虚拟主机,它是由 server{} 块下的 listen 配置项决定的. 每监听一个 TCP 端口,都将使用一个独立的 ngx_rtmp_conf_port_t ...
- react 的定义组件(了解)
react 中定义组件的方法 1. 定义组件 React.createClass() (被淘汰了) 定义组件中的函数 methods 的中的 this 统统指向 组件 2. 函数定义组件 定义的组件时 ...
- Mac os文件名大小写不敏感
Mac os文件名大小写不敏感,但是linux是大小写敏感的. 让我们代入一个场景, 创建一个新文件,你习惯以小写字母开头,接着在其他module中import,看起来一切都正常,emmm,确实没有任 ...
- centos7编译安装Python 3.6.8 后用pip3出现SSL未配置问题(import ssl失败)解决方法
下载源码编译安装openssl https://www.openssl.org/source/openssl-1.0.2j.tar.gz ./config --prefix=/usr/local/op ...
- MonkeyRunner基本操作
1. #导入模块; from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage 2. #连接当前设备,并返 ...