求救大神们这个编程怎么做

zjponline 2020-06-29 03:18:52
编一程序,统计硬盘上某一文本文件(例如d:\test.txt)中所包含的字母、数字、和其他字符的个数,并把这些个数信息写到一新的文本文件中。
...全文
237 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
CS_草祭先生 2020-06-29
  • 打赏
  • 举报
回复
搜百度吧,使用文件输入输出流操作文件
傻蛋丫 2020-06-29
  • 打赏
  • 举报
回复
供楼主参考

import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

public class Count {
	public static void main(String[] args) {
		Count count = new Count();
		String str = count.readFile("D://read.txt");//读
		count.writeFile("D://write.txt",str);//写
	}
	
	private void writeFile(String filePath,String str) {
		FileWriter fw;
		try {
			fw = new FileWriter(filePath);
			fw.write(str);
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}
	
	public String readFile(String fileName){
		int zm = 0;
		int sz = 0;
		int other = 0;
	try {
		@SuppressWarnings("resource")
		FileInputStream fis = new FileInputStream(fileName);
		byte[] buff = new byte[1024];
		while(fis.available()>0){
			int len = fis.read(buff);
			String s = new String(buff,0,len);
			for(int i=0;i<s.length();i++){
				char c = s.charAt(i);
				if(Character.isUpperCase(c) || Character.isLowerCase(c)){
					zm++;
				}else if(Character.isDigit(c)){
					sz++;
				} else {
					other++;
				}
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
		return "字母:"+zm+" "+"\n数字:"+sz+" "+"\n其他字符:"+other;
	}
}

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧