Java字节流文件流的练习

P1.将hohai.txt复制到workspace的工程目录,编写以下程序:
a)查看该文件包含多少字节。
b)查看该文件包含多少字符。
c)估算该文件包含多少句子。(无需100%准确)
d)估算该文件包含多少单词。(无需100%准确)

package test;
import java.util.*;
import java.io.*;
//P1.将hohai.txt复制到workspace的工程目录,编写以下程序:
//a)查看该文件包含多少字节。
//b)查看该文件包含多少字符。
//c)估算该文件包含多少句子。(无需100%准确)
//d)估算该文件包含多少单词。(无需100%准确)
public class Filedemo01
{
	public static void main(String[] args) throws IOException
	{
		//文件字节输入流
		FileInputStream is = new FileInputStream("hohai.txt");
		//逐个字节读
		System.out.println("输出的查找结果为:");
		int count1 = 0;
		int c;
		int countOfPharase=0;
		int countOfSentence=0;
		while((c=is.read()) != -1)
		{
			count1++;
			if(c == ' ')
				countOfPharase++;
			else if(c=='.' || c=='!' || c=='?'||c==',')
				countOfSentence++;
		}
		//3-关闭字节输入流
		is.close();
		System.out.println("a. 该文件包含的字节数为:" + count1);
		
		//文件字符输入流
		FileReader reader = new FileReader("hohai.txt");
		//逐个字符读
		int count2=0;
	    while(reader.read() != -1)
	    	count2++;
	    //关闭字符输入流
		reader.close();
		System.out.println("b. 该文件包含的字符数为::"+count2);
		System.out.println("c. 该文件包含的句子数为::"+countOfSentence);
		System.out.println("d. 该文件包含的单词数为::"+countOfPharase);
		
	}
}

e)比较FileInputStream和BufferedInputStream的工作效率(速度)。

package test;

import java.io.*;
//e)比较FileInputStream和BufferedInputStream的工作效率(速度)
public class Filedemo03 {
	public static void main(String args[]){
		try {
			long start = System.currentTimeMillis();
			System.out.println("程序开始执行的时间为:"+start);
			File fi = new File("E:/javaproject/hohai.txt");
			FileInputStream fis = new FileInputStream(fi);
			int c=fis.read();
			while(c!=-1){
				
				System.out.print((char)c);
 
				c=fis.read();
			}
			
			long end = System.currentTimeMillis();
			System.out.println("程序运行结束的时间为: "+end);
			System.out.println("程序执行时间为:" + (end - start) + "毫秒");
			
			
			
			start = System.currentTimeMillis();
			System.out.println("程序开始执行的时间为:"+start);
			File afi = new File("E:/javaproject/text.txt");
			FileInputStream afis = new FileInputStream(afi);
			BufferedInputStream bis = new BufferedInputStream(afis);
			c=bis.read();
			while(c!=-1){
				
				System.out.print((char)c);
				c=bis.read();
			}
			end = System.currentTimeMillis();
			afis.close();
			fis.close();
			bis.close();
			System.out.println("程序运行结束的时间为: "+end);
			System.out.println("程序执行时间为:" + (end - start) + "毫秒");
		} catch (Exception el) {
			el.printStackTrace();
		}
	}
}

f)查看字符串“hohai”出现过多少次。(可选题)

package test;
import static java.lang.System.out;
import java.io.*;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//f)查看字符串“hohai”出现过多少次。(可选题)
public class Filedemo02 {
		static int count=0;
		public static void find_word(String filename,String word) throws Exception {
			FileReader infile=new FileReader(filename);
			Pattern pat=Pattern.compile(word);
			BufferedReader reader=new BufferedReader(infile);
			String aLine=reader.readLine();
			while(aLine !=null) {
				Matcher mat=pat.matcher(aLine);
				if(mat.find()) {
					count++;	
			  aLine=reader.readLine();
				}
			}
		//System.out.println(count);
			reader.close();			
		}
		public static void main(String[] args) throws Exception {	
			  Scanner sc=new Scanner(System.in);
		      out.print("请输入文件名:");
		      String filename=sc.next();
		      out.print("请输入要查找的单词:");
		      String word=sc.next();
		      System.out.println("正常输入");
		      find_word(filename,word);
		    System.out.println("执行成功");
		      System.out.println(word+"出现的行的次数是"+count);
		}
	}

g)显示第1000个字节后的第一个完整单词。(可选题)

package test;
import java.io.*;
//g)显示第1000个字节后的第一个完整单词。(可选题)
public class Filedemo04 {
	public static void main(String[] args) throws IOException{
			FileInputStream fis = new FileInputStream("hohai.txt");
			byte[] bytes = new byte[35];  // 两个单词长度不超过35
			byte b;
			int i = 0, j = 0, count = 0;
			// 跳过前999个字节
			fis.skip(999);
			/*
			int len = (int)fis.skip(999);
			System.out.println(len);  // 实际跳过字节数
			*/
			// 将第1000个字节及之后34个字节记录
			fis.read(bytes);
			for(int k=0; k<35; k++){
				b = bytes[k];
				// 从第1000个字节开始,找出首次满足的两个非字母的字节位置
				// 即第1000个字节后,首个完整字母的起始位置-1与结束位置+1
				if(!((b>='A' && b<='Z') || (b>='a' && b<='z'))){
					count ++;
					if(count == 1)
						i = k+1;
					if(count==2){
						// 出现两相邻的非字母时
						if((k-i)==0){
							i = k;  // 更新i
							count --;  // 恢复count
							continue;
						}
						j = k-1;
						break;
					}
				}
			}
			// 以下是第1000--1034个字节结果输出,用于检验答案
			System.out.print("第1000--1034个字节结果输出,用于检验答案:   ");
			System.out.println(new String(bytes));
			// 以下是答案输出
			System.out.print("第1000个字节后的第一个完整单词是:");
			System.out.println(new String(bytes, i, j-i+1));
		}
	}

代码改进(在更新的地方把k=i改成k=i+1)

import java.io.*;

public class P1_g {
	public static void main(String[] args) throws IOException{
		FileInputStream fis = new FileInputStream("hohai.txt");
		byte[] bytes = new byte[35];  // 两个单词长度不超过35
		byte b;
		int i = 0, j = 0, count = 0;
		// 跳过前999个字节
		fis.skip(999);
		/*
		int len = (int)fis.skip(999);
		System.out.println(len);  // 实际跳过字节数
		*/
		// 将第1000个字节及之后34个字节记录
		fis.read(bytes);
		for(int k=0; k<35; k++){
			b = bytes[k];
			// 从第1000个字节开始,找出首次满足的两个非字母的字节位置
			// 即第1000个字节后,首个完整字母的起始位置-1与结束位置+1
			if(!((b>='A' && b<='Z') || (b>='a' && b<='z'))){
				count ++;
				if(count == 1)
					i = k+1;
				if(count==2){
					// 出现两相邻的非字母时
					if((k-i)==0){
						i = k+1;  // 更新i  在这里改进提高效率
						count --;  // 恢复count
						continue;
					}
					j = k-1;
					break;
				}
			}
		}
		// 以下是第1000--1034个字节结果输出,用于检验答案
		System.out.println(new String(bytes));
		// 以下是答案输出
		System.out.println(new String(bytes, i, j-i+1));
	}
}

P2. 编写如下程序。
a)用循环语句和DataOutputStream类,将99乘法表数据存入指定的文件。
(注:需整理格式)
b)用DataInputStream打开该文件,显示所有数据。
示例:
在这里插入图片描述

package test;
import java.io.*;
	//P2.	编写如下程序。
	//a)用循环语句和DataOutputStream类,将99乘法表数据存入指定的文件。
//		(注:需整理格式)
	//b)用DataInputStream打开该文件,显示所有数据
public class Iotestdemo01 {
		public static void main(String[] args){
			String str;
			File file=new File("E:\\javaproject\\test\\iotext.txt");
			try {
				FileOutputStream fos=new FileOutputStream(file);
				BufferedOutputStream bos=new BufferedOutputStream(fos);
				DataOutputStream dos=new DataOutputStream(bos);
				for(int i=1;i<=9;i++) {
					for(int j=1;j<=9;j++) {
						str=(i*j+"\t");
						dos.writeBytes(str);
					}
					dos.writeBytes("\n");
				}
				dos.close();
				bos.close();
				fos.close();
			}catch(IOException e) {
				System.out.println("发生错误");
			}
			try {
				FileInputStream fis=new FileInputStream(file);
				BufferedInputStream bis=new BufferedInputStream(fis);
				DataInputStream dis=new DataInputStream(bis);
				while(dis.available()>0) {
					str=dis.readLine();
					System.out.print(str);
				}
				dis.close();
				bis.close();
				fis.close();
			}catch(IOException e) {
				System.out.println("发生错误");
			}
		
			
		}

		
	}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值