Day24
IO流
一、带缓冲区的字节输出流
向文件写入数据
利用带缓冲区的字节输出流 向文件写入数据
public class Test {
public static void main(String[] args) throws IOException {
//1.创建流对象 -- 缓冲区大小:8192字节
//BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("cxk.txt"));
//1.创建流对象 -- 缓冲区大小:2048字节
//BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("cxk.txt"),2048);
//1.创建流对象 -- 缓冲区大小:2048字节
//注:在文件末尾追加
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("cxk.txt",true),2048);
//2.写入数据
bos.write("123".getBytes());
bos.write("abc".getBytes());
bos.write("def".getBytes());
bos.write("123".getBytes());
//3.关闭资源
bos.close();
}
}
读取文件里的数据
利用带缓冲区的字节输入流 读取文件里的数据
public class Test02 {
public static void main(String[] args) throws IOException {
//1.创建流对象 -- ,缓冲区大小:8192个字节
// BufferedInputStream bis = new BufferedInputStream(new FileInputStream("cxk.txt"));
//1.创建流对象 -- ,缓冲区大小:2048个字节
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("cxk.txt"),2048);
//2.读取数据
byte[] bs = new byte[1024];
int len;
while((len = bis.read(bs)) != -1){
System.out.println(new String(bs, 0, len));
}
//3.关闭资源
bis.close();
}
}
拷贝文件
public class Copy {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("源文件.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("目标文件.mp4"));
byte[] bs = new byte[1024];
int len;
while((len = bis.read(bs)) != -1){
bos.write(bs, 0, len);
}
bis.close();
bos.close();
}
}
二、字符输入转换流
向文件写入数据
利用 字符输出转换流 向文件写入数据
- 不处理异常的方式
- 文件存在的情况
- 文件不存在的情况
- 经验:
- 所有的输出流,当文件不存在时,都会创建文件,再写入
- 所有的输入流,当文件不存在时,就会报错 – FileNotFoundException文件未找到异常
public static void main(String[] args) throws IOException {
//1.创建流对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("wyf.txt"));
//2.写入数据
osw.write(97);//写入码值
char[] cs = {'1','2','3','a','鸡','哥'};
osw.write(cs);//写入char数组
osw.write(cs,4,2);//写入char数组,偏移量,写入长度
String str = "唱跳rap篮球,鸡哥救我";
//osw.write(str);//写入字符串
osw.write(str, 3, 3);//写入字符串,偏移量,写入长度
//3.关闭资源
osw.close();
}
- 处理异常的方式
public static void main(String[] args) { OutputStreamWriter osw = null; File file = new File("D://cxk.txt"); try { //1.创建流对象 + 系统默认编码格式 osw = new OutputStreamWriter(new FileOutputStream(file,true), "GBK"); //2.写入数据 String str = "唱跳rap篮球"; osw.write(str); System.out.println(file.getAbsolutePath()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (osw != null) { try { //3.关闭资源 osw.close(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println(file.delete()); }
读取 文件里的数据
利用字符输入转换流 读取 文件里的数据
- 不处理异常的方式
- 文件存在的情况
- 文件不存在的情况
- 经验:
- 所有的输出流,当文件不存在时,都会创建文件,再写入
- 所有的输入流,当文件不存在时,就会报错 – FileNotFoundException文件未找到异常
public static void main(String[] args) throws IOException { //1.创建流对象 + 系统默认编码格式 InputStreamReader isr = new InputStreamReader(new FileInputStream("cxk.txt")); //2.写入数据 -- 一个一个字符读取 int read = isr.read(); System.out.println((char)read); read = isr.read(); System.out.println((char)read); read = isr.read(); System.out.println((char)read); read = isr.read(); System.out.println((char)read); read = isr.read(); System.out.println((char)read); read = isr.read(); System.out.println(read); //3.关闭资源 isr.close(); }
public static void main(String[] args) throws IOException { //1.创建流对象 + 系统默认编码格式 InputStreamReader isr = new InputStreamReader(new FileInputStream("cxk.txt")); //2.写入数据 -- 一个一个字符循环读取 int read; while ((read = isr.read()) != -1) { System.out.println((char)read); } //3.关闭资源 isr.close(); }
public static void main(String[] args) { //1.创建流对象 + 系统默认编码格式 try (InputStreamReader isr = new InputStreamReader(new FileInputStream("cxk.txt"));) { //2.写入数据 -- 一个一个字符数组循环读取 char[] cs = new char[1024]; int len; while ((len = isr.read(cs)) != -1) { System.out.println(new String(cs, 0, len)); } } catch (IOException e) { e.printStackTrace(); } }
处理异常的方式
public static void main(String[] args) { //1.创建流对象 + 指定编码格式 try (InputStreamReader isr = new InputStreamReader(new FileInputStream("wyf.txt"), "GBK")) { //2.写入数据 -- 一个一个字符数组循环读取 char[] cs = new char[1024]; int len; while ((len = isr.read(cs)) != -1) { System.out.println(new String(cs, 0, len)); } } catch (IOException e) { e.printStackTrace(); } }
拷贝文件
public class Copy {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("小说.txt"), "GBK");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("copy.txt"), "GBK");
char[] cs = new char[1024];
int len;
while((len = isr.read(cs)) != -1){
osw.write(cs, 0, len);
}
isr.close();
osw.close();
}
}
三、文件字输出流
1.向文件写入数据
利用文件字符输出流 向文件写入数据
public static void main(String[] args) throws IOException {
// 1.创建流对象
FileWriter fw = new FileWriter("wyf.txt", true);
// 2.写入数据
fw.write("\n宇宙最强鸡中之王");
// 3.关闭资源
fw.close();
}
2.读取文件里的数据
public static void main(String[] args) throws IOException {
//1.创建流对象
FileReader fr = new FileReader("wyf.txt");
//2.读取数据
char[] cs = new char[1024];
int len;
while ((len = fr.read(cs)) != -1) {
System.out.println(new String(cs, 0, len));
}
//3.关闭资源
fr.close();
}
3.拷贝文件
public class copy {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("wyf.txt");
FileWriter fw = new FileWriter("坤哥秘籍.txt");
char[] cs = new char[1024];
int len;
while ((len = fr.read(cs)) != -1) {
fw.write(cs, 0, len);
}
fr.close();
fw.close();
}
}
四、带缓冲区的字符输入流
读取文件里的数据
利用带缓冲区的字符输入流 读取文件里的数据
public static void main(String[] args) throws IOException {
//1.创建流对象(在末尾追加):文件字节输出流 -> 字符输出转换流 -> 带缓冲区的字符输出流 // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new
// FileOutputStream("wyf.txt",true),"GBK"));
//1.创建流对象(在末尾追加):文件字符输出流 -> 带缓冲区的字符输出流
BufferedWriter bw = new BufferedWriter(new FileWriter("wyf.txt", true));
//2.写入数据
bw.write("\n独门绝技,铁山靠,坤拳");
//3.关闭资源
bw.close();
}
读取文件里的数据
利用带缓冲区的字符输入流 读取文件里的数据
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
// 1.创建流对象:文件字节输入流 -> 字符输入转换流 -> 带缓冲区的字符输入流
// BufferedReader br = new BufferedReader(new InputStreamReader(new
// FileInputStream("wyf.txt"),"GBk"));
// 1.创建流对象
BufferedReader br = new BufferedReader(new FileReader("wyf.txt"));
// 2.读取数据
char[] cs = new char[1024];
int len;
while ((len = br.read(cs)) != -1) {
System.out.println(new String(cs, 0, len));
}
// 3.关闭资源
br.close();
}
拷贝文件
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("坤哥秘籍.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("坤哥秘籍2.txt"));
char[] cs = new char[1024];
int len;
while ((len = br.read(cs)) != -1) {
bw.write(cs, 0, len);
}
br.close();
bw.close();
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("坤哥秘籍.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("坤哥秘籍2.txt"));
boolean flag = false;
String readLine;
while ((readLine = br.readLine()) != null) {
if (flag) {
bw.newLine();
}
bw.write(readLine);
flag = true;
}
br.close();
bw.close();
}
五、对象输出流
1.将数据写入到文件中
利用对象输出流 将数据写入到文件中
public static void main(String[] args) throws FileNotFoundException, IOException { //1.创建流对象 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("kk.txt")); //2.写入数据 oos.writeInt(250);//写入int值 oos.writeDouble(123123.123);//写入double值 oos.writeBoolean(true);//写入boolean值 oos.writeUTF("坤派掌门人");//写入字符串 oos.writeObject(new Date());//写入对象 //3.关闭资源 oos.close(); }
2.读取文件里的数据
利用对象输入流 读取文件里的数据
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("kk.txt")); int readInt = ois.readInt(); double readDouble = ois.readDouble(); boolean readBoolean = ois.readBoolean(); String readUTF = ois.readUTF(); Date readObject = (Date) ois.readObject(); System.out.println(readInt); System.out.println(readDouble); System.out.println(readBoolean); System.out.println(readUTF); System.out.println(readObject); ois.close(); }
3.将自定义对象写入到文件中
利用对象输出流 将自定义对象写入到文件中
public class Test { public static void main(String[] args) { //1.创建流对象 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(cxk.txt)); //2.写入数据 oos.writeObject(new User("1689278821", "132123", "成都吴彦祖", '男',"18")); oos.writeObject(new User("1389278821", "132123", "成都彭于晏", '男',"28")); oos.writeObject(new User("1989878821", "132123", "成都刘德华", '男',"22")); //3.关闭资源 oos.close(); } } class User implements Serializable{ private static final long serialVersionUID = -6899162031551365267L; private String username; private transient String password; private String nickName; private String name; private char sex; private int age; public User() { } public User(String username, String password, String nickName, String name, char sex, int age) { this.username = username; this.password = password; this.nickName = nickName; this.name = name; this.sex = sex; this.age = age; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [username=" + username + ", password=" + password + ", nickName=" + nickName + ", name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
4.读取文件里的自定义对象
利用对象输入流 读取文件里的自定义对象
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { //1.创建流对象 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("cxk.txt")); //2.读取数据 try { User user; while((user = (User) ois.readObject()) != null){ System.out.println(user); } } catch (EOFException e) { } //3.关闭资源 ois.close(); } class User implements Serializable{ private static final long serialVersionUID = -6899162031551365267L; private String username; private transient String password; private String nickName; private String name; private char sex; private int age; public User() { } public User(String username, String password, String nickName, String name, char sex, int age) { this.username = username; this.password = password; this.nickName = nickName; this.name = name; this.sex = sex; this.age = age; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } public String getName() { return name; } public void setName(String name) { this.name = name; } public char getSex() { return sex; } public void setSex(char sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User [username=" + username + ", password=" + password + ", nickName=" + nickName + ", name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
总结:
- 字节流
BufferedOutputStream、BufferedInputStream- 字符流
OutputStreamWriter、InputStreamReader
FileWriter、FileReader
BufferedWriter、BufferedReader- 对象流
ObjectOutputStream、ObjectInputStream
理解:序列化、反序列化、Serializable接口、transient