问题描述
程序1/**Tochangethislicenseheader,chooseLicenseHeadersinProjectProperties.*Tochangethistemplatefile,chooseTools|Templates*andopenthetemplateintheeditor.*//****@authorminer*/importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.FileReader;importjava.io.FileWriter;importjava.util.HashMap;/***测试文件读取*@authorAdministrator**/publicclassFileTest{publicstaticvoidmain(String[]args){try{//Stringteststr=test();FileWriterfw=newFileWriter("E:\1.txt",true);BufferedWriterbw=newBufferedWriter(fw);bw.write("我爱大海,我爱蓝天;我爱大海,我爱蓝天/r/n");bw.write("我爱大海,我爱蓝天;我爱大海,我爱蓝天/r/n");bw.flush();fw.close();bw.close();//逐行读取文件FileReaderfr=newFileReader("E:\1.txt");BufferedReaderbr=newBufferedReader(fr);Stringstrline=br.readLine();System.out.println(strline);fr.close();br.close();//整个读取文件FileInputStreamfis=newFileInputStream("c:/test.txt");byte[]bt=newbyte[1024];fis.read(bt);fis.close();Stringfilestr=newString(bt);filestr=filestr.trim();HashMapHZ=newHashMap();for(inti=0;i<filestr.length();i++){Stringtemp=filestr.substring(i,i+1);if(HZ.containsKey(temp)){intcs=Integer.valueOf(HZ.get(temp).toString());HZ.put(temp,cs+1);}else{HZ.put(temp,1);}}HZ.toString();System.out.println(HZ.toString());}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}程序2importjava.io.BufferedReader;importjava.io.FileReader;importjava.io.File;importjava.io.FileWriter;publicclassMyFileReader{publicstaticvoidmain(String[]args)throwsException{FileWriterfileWriter=newFileWriter("E:\1.txt",true);int[]a=newint[]{11112,222,333,444,555,666};for(inti=0;i<a.length;i++){fileWriter.write(String.valueOf(a[i])+"");}fileWriter.flush();fileWriter.close();}}