用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 ...
随机推荐
- 灰度图像--图像增强 直方图匹配(规定化)Histogram Specification
学习DIP第39天 转载请标明本文出处:http://blog.csdn.net/tonyshengtan,欢迎大家转载,发现博客被某些论坛转载后,图像无法正常显示,无法正常表达本人观点,对此表示很不 ...
- less命令:查看文件内容
less 命令的作用和 more 十分类似,都用来浏览文本文件中的内容,不同之处在于,使用 more 命令浏览文件内容时,只能不断向后翻看,而使用 less 命令浏览,既可以向后翻看,也可以向前翻看. ...
- jQuery事件之自定义事件
其实事件的bind和unbind,都是为了自定义事件做准备. 语法: $(selector).trigger(type, data); 作用:在每一个匹配的元素上触发某类事件,它触发的是由bind() ...
- 五、在事务中使用Mybatis缓存的那些坑
场景: 1.同一个事务中 2.使用mybatis执行同一个sql @Transactional(rollbackFor = { Exception.class }) public void getIn ...
- JSP通过URL给Servlet传值
jsp传数据: <a id="a1" href="" ></a> <script> $("#a1").a ...
- 批量插入数据@Insert
// 批量插入数据 @Insert("<script>" + "insert into index_kline (currency_id, currency, ...
- 识别C++编译器编译标准
cout << __cplusplus<<endl; C++03:__cplusplus = 199711L C++11:__cplusplus = 201103L
- Okhttp源码分析--基本使用流程分析
Okhttp源码分析--基本使用流程分析 一. 使用 同步请求 OkHttpClient okHttpClient=new OkHttpClient(); Request request=new Re ...
- IDEA找回Run Dashboard
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- 邻居子系统输出 之 neigh_output、neigh_hh_output
概述 ip层在构造好ip头,检查完分片之后,会调用邻居子系统的输出函数neigh_output进行输出,输出分为有二层头缓存和没有两种情况,有缓存时调用neigh_hh_output进行快速输出,没有 ...