Java学习-IO流-综合练习
练习1:拷贝文件并统计用时(4.27GB)
利用基本流:一次读写一个字节
long start = System.currentTimeMillis();
FileInputStream fis = FileInputStream("..\\xx.iso");
FileOutputStream fos = FileOutputStream("copy.iso");
int b;
while((b=fis.read())!=-1){fos.write(b);}
fos.close();
fis.close();
long end = System.currentTimeMullis();
sout((end-start)/1000);//时间过长
利用基本流:一次读写一个字节数组
long start = System.currentTimeMillis();
FileInputStream fis = FileInputStream("..\\xx.iso");
FileOutputStream fos = FileOutputStream("copy.iso");
byte[] bytes = new byte[8192];
int len;
while((len=fis.read(bytes))!=-1){fos.write(bytes,0,len);}
fos.close();
fis.close();
long end = System.currentTimeMullis();
sout((end-start)/1000.0);
//→ 21.333
//→ 19.726
//→ 16.253
利用缓冲流:一次读写一个字节
long start = System.currentTimeMillis();
BufferedInputStream bis = BufferedInputStream(new FileInputStream("..\\xx.iso"));
BufferedOutputStream bos = new BufferedOutputStream(FileOutputStream("copy.iso"));
int b;
while((b=fis.read())!=-1){fos.write(b);}
fos.close();
fis.close();
long end = System.currentTimeMullis();
sout((end-start)/1000.0);
//→ 137.391
//→ 137.9
//→ 95.466
利用缓冲流:一次读写一个字节数组
long start = System.currentTimeMillis();
BufferedInputStream bis = BufferedInputStream(new FileInputStream("..\\xx.iso"));
BufferedOutputStream bos = new BufferedOutputStream(FileOutputStream("copy.iso"));
byte[] bytes = new byte[8192];
int len;
while((len=fis.read(bytes))!=-1){fos.write(bytes,0,len);}
fos.close();
fis.close();
long end = System.currentTimeMullis();
sout((end-start)/1000.0);
//→ 18.488
//→ 18.842
//→ 17.686
练习2:把《出师表》的按照文章顺序恢复,写到一个新文件中
intput.txt:
3.侍中、侍郎郭攸之、费祎、董允等…
8.愿陛下托臣以讨贼兴复之效…
4.将军向宠,性行淑均,晓畅军事…
2.宫中府中,俱为一体,陟罚臧否,不宜异同…
1.先帝创业未半而中道崩殂…
9.今当远离,临表涕零…
6.臣本布衣,躬耕于南阳…
7.先帝知臣谨慎,故临崩寄臣以大事也…
5.亲贤臣,远小人,此先汉所以兴隆也…
法1:
BufferedReader br = new BufferedReader(new FileReader("..\\input.txt"));
String line;
ArrayList<String> list = new ArrayList<>();
while((line=br.readLine())!=null){
list.add(line);
}
br.close();
//排序规则:按照每一行前边的序号排序
Collections.sort(list,new Comparator<String>(){
@Override
public int compare(String o1,String o2){
int n1 = Integer.parseInt(o1.split("\\.")[0]);
int n2 = Integer.parseInt(o2.split("\\.")[0]);
return n1-n2;
}
});
//写出
BufferedWriter bw = new BufferedWriter(new FileWriter("result.txt"));
for(String str:list){
bw.write(str);
bw.write(newLine);
}
bw.close();
法2:
BufferedReader br = new BufferedReader(new FileReader("..\\input.txt"));
String line;
TreeMap<Integer,String> tm = new TreeMap<>();
while((line=br.readLine())!=null){
String[] arr = line.split("\\.");
tm.put(Integer.parseInt(arr[0]),line);
}
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter("result.txt"));
Set<Map.Entry<Integer,String>> entries = tm.entrySet();
for(Map.Entry<Integer,String> entry:entries){
String value = entry.getValue();
bw.write(value);
bw.newLine();
}
bw.close();
result.txt:
1.先帝创业未半而中道崩殂…
2.宫中府中,俱为一体…
3.侍中、侍郎郭攸之、费祎、董允等…
4.将军向宠,性行淑均,晓畅军事…
5.亲贤臣,远小人,此先汉所以兴隆也…
6.臣本布衣,躬耕于南阳…
7.先帝知臣谨慎,故临崩寄臣以大事也…
8.愿陛下托臣以讨贼兴复之效…
9.今当远离,临表涕零…
练习3:实现一个验证程序运行次数的小程序,要求如下:
1.第一次运行:欢迎使用本软件,第1次使用免费
2.第二次运行:欢迎使用本软件,第2次使用免费
3.第三次运行:欢迎使用本软件,第3次使用免费
4.当程序运行超过三次时给出提示:本软件只能免费使用3次,欢迎您注册会员后继续试用
反面教材:
BufferedReader br = new BufferedReader(new FileReader("count.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("count.txt"));
String line = br.readLine();
br.close();
int count = Integer.parseInt(line);//→ Cannot parse null string
count++;
if(count<=3){
sout("欢迎使用本软件,第1"+count+"次使用免费");
}else{
sout("本软件只能免费使用3次,欢迎您注册会员后继续试用");
}
bw.write(count+"");
bw.close();
分析原因:创建输入流会把文件清空,readLine() 读到的是 null,IO流应该随用随创建,不用就关闭
BufferedReader br = new BufferedReader(new FileReader("count.txt"));
String line = br.readLine();
br.close();
int count = Integer.parseInt(line);//→ Cannot parse null string
count++;
if(count<=3){
sout("欢迎使用本软件,第1"+count+"次使用免费");
}else{
sout("本软件只能免费使用3次,欢迎您注册会员后继续试用");
}
BufferedWriter bw = new BufferedWriter(new FileWriter("count.txt"));
bw.write(count+"");
bw.close();