public class MyDemo {
10年積累的成都網(wǎng)站建設(shè)、成都做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)制作后付款的網(wǎng)站建設(shè)流程,更有濮陽縣免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
static int count = 0;
// 每次調(diào)用任意構(gòu)造方法實(shí)例化都會(huì)執(zhí)行
{
count++;
}
// 提供方法返回實(shí)例化過的次數(shù)
public static int getCount() {
return count;
}
}
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* 統(tǒng)計(jì)一部小說中文字?jǐn)?shù)量
*
*/
public class Demo10 {
static int count = 0;
public static void main(String[] args)throws IOException{
String file = "src/javase2/day03/demo.txt";
MapCharacter, Integer map = count(file);
ListEntryCharacter, Integer entrys =
new ArrayListEntryCharacter,Integer(map.entrySet());
Collections.sort(entrys,
new ComparatorEntryCharacter, Integer() {
public int compare(EntryCharacter, Integer o1,
EntryCharacter, Integer o2) {
return -(o1.getValue() - o2.getValue());
}
});
int all = map.size();
System.out.println("不重復(fù)字符數(shù)量:"+ all);
System.out.println("總字符數(shù)量:"+ count);
for(int i=0; i10; i++){
EntryCharacter, Integer entry = entrys.get(i);
char c = entry.getKey();
int val = entry.getValue();
System.out.println(c+":"+val+","+(val*100.0/count)+"%");
}
}
public static MapCharacter, Integer count(String file)
throws IOException {
InputStreamReader in =
new InputStreamReader(
new BufferedInputStream(
new FileInputStream(file)),"GBK");
MapCharacter, Integer map =
new HashMapCharacter, Integer();
SetCharacter ignoreChars = new HashSetCharacter();
//ignoreChars 忽略的字符
ignoreChars.add('\n');
ignoreChars.add('\r');
ignoreChars.add(',');
ignoreChars.add('。');
ignoreChars.add(' ');
ignoreChars.add(' ');//...
int c;
while((c = in.read())!=-1){
char ch = (char)c;
if(ignoreChars.contains(ch)){
continue;
}
count++;
Integer val = map.get(ch);
map.put(ch, val == null ? 1 : val+1);
}
in.close();
return map;
}
}
翻了一下以前的案例,給你找到了 ?你可以試試
下面的這個(gè)是一個(gè)IO 的工具類
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import javax.imageio.stream.FileImageInputStream;
/**
* IO 工具類
*/
public class IOUtils {
public static Object deepCopy(Object obj) {
try {
ByteArrayOutputStream buf =
new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(buf);
oos.writeObject(obj);// 將對(duì)象序列化到byte數(shù)組流中
oos.close();
byte[] ary = buf.toByteArray();
ObjectInputStream ois = new ObjectInputStream(
new ByteArrayInputStream(ary));
Object o = ois.readObject();// 從byte數(shù)組流中反序列化對(duì)象
ois.close();
return o;
} catch (Exception e) {// 異常捕獲再拋出, 將異常轉(zhuǎn)換為運(yùn)行時(shí)異常
throw new RuntimeException(e);
}
}
/**
* 復(fù)制文件功能: 將文件src復(fù)制為dst
* 只能復(fù)制文件! 不支持文件夾的復(fù)制!
* @param src 源文件
* @param dst 目標(biāo)文件
*/
public static void cp(String src, String dst){
try {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
byte[] buf = new byte[10*1024];
int count;
while((count=in.read(buf))!=-1){
//System.out.println(count);
out.write(buf, 0, count);
}
//System.out.println("結(jié)束了:"+count);
in.close();
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void cp2(String src, String dst){
try {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
int b;
while((b=in.read())!=-1){
out.write(b);
}
in.close();
out.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 實(shí)現(xiàn)將文件讀取到一個(gè)byte數(shù)組
*/
public static byte[] read2(String filename){
try{
InputStream in = new FileInputStream(filename);
byte[] buf = new byte[in.available()];
in.read(buf);
in.close();
return buf;
}catch(IOException e){
throw new RuntimeException(e);
}
}
public static void print(File file){
try{
RandomAccessFile raf = new RandomAccessFile(file, "r");
int b; int i=1;
while((b=raf.read())!=-1){
if(b=0xf)
System.out.print("0");
System.out.print(Integer.toHexString(b)+" ");
if(i++%16 == 0){
System.out.println();
}
}
System.out.println();
raf.close();
}catch(IOException e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void print(byte[] buf){
int i=1;
for(int b : buf){
b = b 0xff;
if(b=0xf)
System.out.print("0");
System.out.print(Integer.toHexString(b)+" ");
if(i++%16 == 0){
System.out.println();
}
}
System.out.println();
}
/**
* 將小文件 一次性 讀取到 byte數(shù)組中返回
* @param file 文件名
* @return 全部文件內(nèi)容
*/
public static byte[] read(String file){
try {
RandomAccessFile raf = new RandomAccessFile(file, "r");
byte[] buf = new byte[(int)raf.length()];
raf.read(buf);
raf.close();
return buf;
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static byte[] read(File file){
try {
RandomAccessFile raf = new RandomAccessFile(file, "r");
byte[] buf = new byte[(int)raf.length()];
raf.read(buf);
raf.close();
return buf;
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 在控制臺(tái)上按照16進(jìn)制格式輸出文件的內(nèi)容
* 每16個(gè)byte為一行
* @param file 文件名
* @throws RuntimeException 如果文件讀取失敗,跑出異常
*/
public static void print(String file){//IOUtils.java
try{
RandomAccessFile raf=
new RandomAccessFile(file, "r");
int b; int i=1;
while((b=raf.read())!=-1){
if(b=0xf){ System.out.print('0'); }
System.out.print(Integer.toHexString(b)+" ");
if(i++%16==0){ System.out.println(); }
}
System.out.println();
raf.close();?????
}catch(IOException e){
throw new RuntimeException(e);
}
}
}
使用代碼版本管理工具啊,例如svn、moven等等。 版本管理工具會(huì)記錄你的代碼更新情況,改動(dòng)、新增、刪除等操作都會(huì)詳細(xì)記錄的。 你安裝一個(gè)版本工具服務(wù)端和客戶端就行。