缓冲流
import java.io.*;
import java.util.HashMap;
/**
* Create by xiye on 2019/12/5 11:40
*/
public class Demo1_缓冲流 {
/*
* BufferedInputStream与FileInputStream用法差不多,区别在于效率
* BufferedInputStream有缓存(8192),效率高;
* FileInputStream每次都要进行IO操作,效率低。(小文件感觉无差别,大文件就明显)
*
* BufferedReader特有方法readLine方法读取一行字符数据
* BufferedWriter特有方法newLine方法写入一行空行
*
* */
public static void main(String[] args) throws IOException {
//byteMethod();
charMethod();
}
private static void charMethod() throws IOException {
HashMap<String, String> map = new HashMap<>();
BufferedReader br = new BufferedReader(new FileReader("D:/file_test/csb.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:/file_test/copy/csb_copy.txt"));
String line;
while ((line = br.readLine()) != null) { // 特有方法,读取一行
String[] str = line.split("\\.", 2);
map.put(str[0], str[1]);
}
for (String k : map.keySet()) {
//System.out.println(k + "." + map.get(k));
bw.write(k + "." + map.get(k));
bw.newLine(); // 特有方法,写入一行空行
}
bw.close();
br.close();
}
private static void byteMethod() throws IOException {
long start = System.currentTimeMillis();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:/file_test/jdk8.exe"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:/file_test/copy/jdk8_copy.exe"));
/*int b;
while ((b = bis.read()) != -1 ) {
bos.write(b);
}*/
byte[] bytes = new byte[4 * 1024]; // 当大小达到一定值时,继续增加不会提高效率。
int len;
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
bos.close();
bis.close();
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - start));
}
}
字符缓冲流示例数据
3.侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下。愚以为宫中之事,事无大小,悉以咨之,然后施行,必得裨补阙漏,有所广益。
8.愿陛.下托臣以讨贼兴复之效,不效,则治臣之罪,以告先帝之灵。若无兴德之言,则责攸之、祎、允等之慢,以彰其咎;陛下亦宜自谋,以咨诹善道,察纳雅言,深追先帝遗诏,臣不胜受恩感激。
4.将军向宠,性行淑均,晓畅军事,试用之于昔日,先帝称之曰能,是以众议举宠为督。愚以为营中之事,悉以咨之,必能使行阵和睦,优劣得所。
2.宫中府中,俱为一体,陟罚臧否,不宜异同。若有作奸犯科及为忠善者,宜付有司论其刑赏,以昭陛下平明之理,不宜偏私,使内外异法也。
1.先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。然侍卫之臣不懈于内,忠志之士忘身于外者,盖追先帝之殊遇,欲报之于陛下也。诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以塞忠谏之路也。
9.今当远离,临表涕零,不知所言。
6.臣本布衣,躬耕于南阳,苟全性命于乱世,不求闻达于诸侯。先帝不以臣卑鄙,猥自枉屈,三顾臣于草庐之中,咨臣以当世之事,由是感激,遂许先帝以驱驰。后值倾覆,受任于败军之际,奉命于危难之间,尔来二十有一年矣。
7.先帝知臣谨慎,故临崩寄臣以大事也。受命以来,夙夜忧叹,恐付托不效,以伤先帝之明,故五月渡泸,深入不毛。今南方已定,兵甲已足,当奖率三军,北定中原,庶竭驽钝,攘除奸凶,兴复汉室,还于旧都。此臣所以报先帝而忠陛下之职分也。至于斟酌损益,进尽忠言,则攸之、祎、允之任也。
5.亲贤臣,远小人,此先汉所以兴隆也;亲小人,远贤臣,此后汉所以倾颓也。先帝在时,每与臣论此事,未尝不叹息痛恨于桓、灵也。侍中、尚书、长史、参军,此悉贞良死节之臣,愿陛下亲之信之,则汉室之隆,可计日而待也。
转换流
以utf-8编码读取gbk编码文件
以gbk编码获取utf编码文件
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
* Create by xiye on 2019/12/5 15:42
*/
public class Demo2_转换流 {
/*转换流(实质还是字符流)
* InputStreamReader、OutputStreamWriter
*
*
* ISO-8859-1是单字节字符集,即无论中英文还是其他,都用一个字节表示
* GBK中文编码,占用两个字节
* */
/*
* 文件编码是GBK/ANSI,读取时编码为UTF-8,中文字符会出现“符号类”乱码(如?�)
*
* 文件编码是UTF-8,读取时编码为GBK,中文字符会出现“中文类”乱码(如)
*
* 以上并非绝对出现情况,具体而定
* */
public static void main(String[] args) throws Exception {
// 默认是UTF-8
// 读取文件时,文件编码和指定(默认UTF-8)编码不同时,“中文字符”可能导致乱码出现
InputStreamReader ir = new InputStreamReader(new FileInputStream("D:/file_test/charset/UTF.txt"), "GBK");
OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream("D:/file_test/charset/UTF1.txt"));
OutputStreamWriter ow1 = new OutputStreamWriter(new FileOutputStream("D:/file_test/charset/UTF2.txt"), "GBK");
int b;
while ((b = ir.read()) != -1) {
ow.write(b);
ow1.write(b);
}
ow1.close();
ow.close();
ir.close();
}
}
序列流
bean对象实现序列化接口
package com.xiye.bean;
import java.io.Serializable;
/**
* Create by xiye on 2019/12/5 17:38
*/
public class Student implements Serializable {
private static final long serialVersionUID = 6579123894181504511L;
private String name;
private Integer age;
private transient Float score;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Float getScore() {
return score;
}
public void setScore(Float score) {
this.score = score;
}
}
测试序列化与反序列化
import com.xiye.bean.Student;
import java.io.*;
/**
* Create by xiye on 2019/12/5 16:50
*/
public class Demo3_序列流 {
/*
* 被transient(瞬态)修饰的不会被序列化
*
* */
/*
* bean对象没有设置UID时,序列化之后再反序列化并且修改原来bean对象的属性或方法时,会出现版本号不一致的异常
*
* 设置快捷键生成UID:
* 1、File --> Settings --> Editor --> inspections,
* 2、搜索UID,选中“Serializable classes without serialVersionUID”
* 3、鼠标焦点位于bean对象名称,按alt + enter,选择生成UID
* */
public static void main(String[] args) throws IOException, ClassNotFoundException {
serializationMethod();
deserializationMethod();
}
private static void deserializationMethod() throws IOException, ClassNotFoundException {
// 反序列化对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("stu.info"));
Student stu = (Student) ois.readObject();
System.out.println(stu);
}
private static void serializationMethod() throws IOException {
Student stu = new Student();
stu.setName("xiye");
stu.setAge(21);
stu.setScore(99F); // 被transient(瞬态)修饰序列化时不会保存
// 序列化对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("stu.info"));
oos.writeObject(stu);
oos.close();
}
}
序列化结果
�� sr com.xiye.bean.Student[M�R��%� L aget Ljava/lang/Integer;L namet Ljava/lang/String;xpsr java.lang.Integer⠤���8 I valuexr java.lang.Number������ xp t xiye
反序列化结果
Student{name=‘xiye’, age=21, score=null}
打印流
import java.io.FileNotFoundException;
import java.io.PrintStream;
/**
* Create by xiye on 2019/12/5 19:33
*/
public class Demo4_打印流 {
public static void main(String[] args) {
PrintStream ps = null;
try {
ps = new PrintStream("ps.txt");
System.setOut(ps);
System.out.println("打印流就是");
System.out.println("原本在控制台输出的内容");
System.out.println("输出到指定流的文件中");
System.out.println("此时控制台不再输出");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ps.close();
}
}
注:似乎不常用。。。
Path接口和Files工具
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
/**
* Create by xiye on 2019/1/5 19:40
*/
public class Demo5_PathAndFiles {
public static void main(String[] args) {
Path source = Paths.get("ps.txt");
Path target = Paths.get("path.txt");
//if (Files.notExists(target)) {
try {
// StandardCopyOption.REPLACE_EXISTING进行覆盖
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
//}
}
}