50,528
社区成员
发帖
与我相关
我的任务
分享
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;
}
}
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;
}
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;
}