练习1: 拷贝文件
四种方式拷贝文件,统计各自用时
- 字节流的基本流:一次读写一个字节
- 字节流的基本流:一次读写一个字节数组
- 字节缓冲流:一次读写一个字节
- 字节缓冲流:一次读写一个字节数组
public class Test1 {
public static void main(String[] args) throws IOException {
//231,164字节
long start = System.currentTimeMillis();
//980毫秒
//method1();
//2毫秒
method2();
//22毫秒
//method3();
//1毫秒
method4();
long end = System.currentTimeMillis();
//如果文件大,可以 /1000.0+"秒"
System.out.println((end - start) + "毫秒");
}
private static void method1() throws IOException {
FileInputStream fis = new FileInputStream("/Users/jessy/Desktop/offer50/movie.mp4");
FileOutputStream fos = new FileOutputStream("/Users/jessy/Desktop/offer50/copy1.mp4");
int b;
while ((b = fis.read()) != -1) {
fos.write(b);
}
fos.close();
fis.close();
}
private static void method2() throws IOException {
FileInputStream fis = new FileInputStream("/Users/jessy/Desktop/offer50/movie.mp4");
FileOutputStream fos = new FileOutputStream("/Users/jessy/Desktop/offer50/copy2.mp4");
byte[] bytes = new byte[1024 * 4];
int len;
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fos.close();
fis.close();
}
private static void method3() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("/Users/jessy/Desktop/offer50/movie.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/Users/jessy/Desktop/offer50/copy3.mp4"));
int b;
while ((b = bis.read()) != -1) {
bos.write(b);
}
bos.close();
bis.close();
}
private static void method4() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("/Users/jessy/Desktop/offer50/movie.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/Users/jessy/Desktop/offer50/copy4.mp4"));
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
bos.close();
bis.close();
}
}
练习2: 修改文本顺序
需求:把《出师表》的文章顺序进行恢复到一个新的文件中
csb.txt
3.侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下。愚以为宫中之事,事无大小,悉以咨之,然后施行,必得裨补阙漏,有所广益。
8.愿陛下托臣以讨贼兴复之效,不效,则治臣之罪,以告先帝之灵。若无兴德之言,则责攸之、祎、允等之慢,以彰其咎;陛下亦宜自谋,以咨诹善道,察纳雅言,深追先帝遗诏,臣不胜受恩感激。
4.将军向宠,性行淑均,晓畅军事,试用之于昔日,先帝称之曰能,是以众议举宠为督。愚以为营中之事,悉以咨之,必能使行阵和睦,优劣得所。
2.宫中府中,俱为一体,陟罚臧否,不宜异同。若有作奸犯科及为忠善者,宜付有司论其刑赏,以昭陛下平明之理,不宜偏私,使内外异法也。
1.先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。然侍卫之臣不懈于内,忠志之士忘身于外者,盖追先帝之殊遇,欲报之于陛下也。诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以塞忠谏之路也。
9.今当远离,临表涕零,不知所言。
6.臣本布衣,躬耕于南阳,苟全性命于乱世,不求闻达于诸侯。先帝不以臣卑鄙,猥自枉屈,三顾臣于草庐之中,咨臣以当世之事,由是感激,遂许先帝以驱驰。后值倾覆,受任于败军之际,奉命于危难之间,尔来二十有一年矣。
7.先帝知臣谨慎,故临崩寄臣以大事也。受命以来,夙夜忧叹,恐付托不效,以伤先帝之明,故五月渡泸,深入不毛。今南方已定,兵甲已足,当奖率三军,北定中原,庶竭驽钝,攘除奸凶,兴复汉室,还于旧都。此臣所以报先帝而忠陛下之职分也。至于斟酌损益,进尽忠言,则攸之、祎、允之任也。
5.亲贤臣,远小人,此先汉所以兴隆也;亲小人,远贤臣,此后汉所以倾颓也。先帝在时,每与臣论此事,未尝不叹息痛恨于桓、灵也。侍中、尚书、长史、参军,此悉贞良死节之臣,愿陛下亲之信之,则汉室之隆,可计日而待也。
- 方法1
public class Test2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("/Users/jessy/Desktop/offer50/src/com/itheima/mybufferedstream1/csb.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) {
//获取o1和o2的序号
int i1 = Integer.parseInt(o1.split("\\.")[0]);
int i2 = Integer.parseInt(o2.split("\\.")[0]);
return i1 - i2;
}
});
BufferedWriter bw = new BufferedWriter(new FileWriter("/Users/jessy/Desktop/offer50/src/com/itheima/mybufferedstream1/result.txt"));
for (String str : list) {
bw.write(str);
bw.newLine();
}
bw.close();
}
}
- 方法2
TreeMap
public class Test3 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("/Users/jessy/Desktop/offer50/src/com/itheima/mybufferedstream1/csb.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);
tm.put(Integer.parseInt(arr[0]), arr[1]);
}
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter("/Users/jessy/Desktop/offer50/src/com/itheima/mybufferedstream1/result2.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();
}
}
练习3: 软件运行的次数
实现一个验证程序运行次数的小程序,要求如下:
- 当程序运行超过3次时会给出提示:本软件只能免费使用3次,欢迎您注册会员后继续使用~
- 程序运行演示如下:
第一次运行控制台输出:欢迎使用本软件,第1次使用免费~
第二次运行控制台输出:欢迎使用本软件,第2次使用免费~
第三次运行控制台输出:欢迎使用本软件,第3次使用免费~
第四次及之后运行控制台输出:本软件只能免费使用3次,欢迎您注册会员后继续使用~
public class Test4 {
public static void main(String[] args) throws IOException {
//不能把计数器保存到文件在,内存每次运行后都会消失 --> 保存在本地文件中
//int count = 0;
//1。把文件中的数子读取到内存中
BufferedReader br = new BufferedReader(new FileReader("/Users/jessy/Desktop/offer50/src/com/itheima/mybufferedstream1/count.txt"));
String line = br.readLine();
int count = Integer.parseInt(line);
count++;
//2。判断
if (count <= 3) {
//<=3 正常运行
System.out.println("欢迎使用本软件,第" + count + "次使用免费~");
} else {
//>3 不能运行
System.out.println("本软件只能免费使用3次,欢迎您注册会员后继续使用~");
}
BufferedWriter bw = new BufferedWriter(new FileWriter("/Users/jessy/Desktop/offer50/src/com/itheima/mybufferedstream1/count.txt"));
//变成字符串
bw.write(count + "");
bw.close();
}
}
创建IO流的心得
- 随用随创建
- 什么时候不用什么时候关闭