一天一个题 关于解析文件 100分

断点 2011-06-22 03:05:07
要求:
统计文件中各个数字和英文字母各自的数量,区分大小写
并将Map中的结果写入目标文件中
方法名:filePath 源文件路径 destPath 目标文件路径
public boolean parse(String filePath, String destPath);

实现代码越少就给分
...全文
87 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
TKD03072010 2011-06-23
  • 打赏
  • 举报
回复
测试成功!!!

import java.io.File;
import java.io.FileInputStream;
import java.io.RandomAccessFile;

public class ParseFile {
public static void main(String[] args) {
String filePath = "filePath.txt";
String destPath = "filePath2.txt";
parse(filePath, destPath);
}

public static boolean parse(String filePath, String destPath) {
try {
File file = new File(filePath);
byte[] bs = new byte[(int)file.length()];
new FileInputStream(file).read(bs);
String s = new String(bs);
RandomAccessFile o = new RandomAccessFile(destPath,"rw");
for (int i='0'; i<='9'; i++) o.writeBytes((char)i + " " + (s.length()- s.replaceAll((char)i+"", "").length()) + "\r\n");
for (int i='A'; i<='Z'; i++) {
o.writeBytes((char)i + " " + (s.length()-s.replaceAll((char)i+"", "").length()) + "\r\n");
o.writeBytes((char)(i+32) + " " + (s.length()-s.replaceAll((char)(i+32)+"", "").length()) + "\r\n");
}
o.close();
} catch (Exception e) {return false;}
return true;
}
}

liangtu 2011-06-22
  • 打赏
  • 举报
回复
3楼是实力派,我很坚信;
qybao 2011-06-22
  • 打赏
  • 举报
回复
借用LS的代码

public static boolean parse(String filePath, String destPath) {
try {
File file = new File(filePath);
byte[] bs = new byte[(int)file.length()];
new FileInputStream(file).read(bs);
String s = new String(bs);
RandomAccessFile o = new RandomAccessFile(destPath,"rw");
for (int i='0'; i<='9'; i++) o.writeBytes((char)i + " " + s.length()-s.replaceAll((char)i+"", "").length() + "\r\n");
for (int i='A'; i<='Z'; i++) {
o.writeBytes((char)i + " " + s.length()-s.replaceAll((char)i+"", "") + "\r\n");
o.writeBytes((char)(i+32) + " " + s.length()-s.replaceAll((char)(i+32)+"", "") + "\r\n");
}
o.close();
} catch (Exception e) {return false;}
return true;
}
飞跃颠峰 2011-06-22
  • 打赏
  • 举报
回复

public static boolean parse(String filePath, String destPath) {
try {
File file = new File(filePath);
byte[] bs = new byte[(int)file.length()];
new FileInputStream(file).read(bs);
int[] is = new int[128];
for (int i=0; i<bs.length; i++)
if (bs[i]>=0) is[(int)bs[i]]++;
RandomAccessFile o = new RandomAccessFile(destPath,"rw");
for (int i='0'; i<='9'; i++) o.writeBytes((char)i + " " + is[i] + "\r\n");
for (int i='A'; i<='Z'; i++) o.writeBytes((char)i + " " + is[i] + "\r\n");
for (int i='a'; i<='z'; i++) o.writeBytes((char)i + " " + is[i] + "\r\n");
o.close();
} catch (Exception e) {return false;}
return true;
}
TKD03072010 2011-06-22
  • 打赏
  • 举报
回复
楼上分析的有道理
...
恐怕又见作业帖...
望舒 2011-06-22
  • 打赏
  • 举报
回复
你技术分太低了,害怕是作业帖.....

50,528

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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