IO流
File类
构造方法:
File(String pathname):将一个路径下的文件/文件夹构造成一个file对象
方法:createNewFile()
当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
案例:
案例:
package cn.wzx.Demo;
import java.io.File;
import java.io.IOException;
public class Demo1 {
public static void main(String[] args) throws IOException {
File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
}
}
IO流概述
IO: input输入 / output输出
IO流:是连接内存和永久存储设备的一条管道
输出流
------------>
内存 永久存储设备(硬盘 U盘 光盘 磁带)
<-------------
输入流
IO流的分类
按照流的方向
输入流:将永久存储设备的数据读到程序中(内存)
输出流:将内存中程序的数据存储到永久存储设备(硬盘)
按照流的传输数据单位分
字节流:每次传输的数据最小单位是字节 字节流可以处理任何文件 音乐视频 等
字符流:一个字符等于两个字节 可以处理的文件是 记事本能打开且正确的文件 .txt .java
按照流的功能分
节点流:负责传输主要数据的流
过滤流:增强流功能的流
所有流的父类 | 输出流 | 输入流 |
---|
字节流 | OutputStream | InputStream |
字符流 | Writer | Reader |
字节流
字节输出流代表(文件输出流FileOutputStream)
方法:
write(int b)
write(byte[] b)
write(byte[] b,int off,int len)
close():关闭流
案例:
package cn.wzx;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo2 {
public static void main(String[] args) throws Exception {
File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);
byte[] b = "abcdefg".getBytes();
fileOutputStream.write(b, 1, 2);
fileOutputStream.close();
System.out.println("写完了");
}
}
案例:追加输出数据
当构造方法的第二参数为true时追加
FileOutputStream fileOutputStream = new FileOutputStream(file,true);
案例:输出换行
不同的系统有不同的写法
windows \r\n r return 回车 n newLine 新的一行
unix/linux \n
macOs \r
代码:
package cn.wzx;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo2 {
public static void main(String[] args) throws Exception {
File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);
byte[] b = "kashjfgkahjdfg".getBytes();
for (int i = 0; i < b.length; i++) {
fileOutputStream.write(b[i]);
fileOutputStream.write("\r\n".getBytes());
}
fileOutputStream.close();
System.out.println("写完了");
}
}
字节输入流(文件字节输入流FileInputStream)
构造方法:
FileInputStream(File file)
FileInputStream(String name)
方法:
read():从此输入流中读取一个数据字节。
read(byte[] b):每次读一个字节数组,将读到的数据存到数组中,返回值类型是int(读到的数据的长度,就是读到了几个数据)
read(byte[] b,int off,int len)
package cn.wzx;
import java.io.File;
import java.io.FileInputStream;
public class Demo3 {
public static void main(String[] args) throws Exception{
File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
FileInputStream fileInputStream = new FileInputStream(file);
int read1 = fileInputStream.read();
System.out.println((char)read1);
int read4 = fileInputStream.read();
System.out.println(read4);
int read5 = fileInputStream.read();
System.out.println(read5);
int read6 = fileInputStream.read();
System.out.println(read6);
fileInputStream.close();
}
}
案例:改造之后的代码
package cn.wzx;
import java.io.File;
import java.io.FileInputStream;
public class Demo3 {
public static void main(String[] args) throws Exception{
File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
FileInputStream fileInputStream = new FileInputStream(file);
int read;
while (true) {
read = fileInputStream.read();
if (read==-1) {
break;
}else {
System.out.print((char)read);
}
}
fileInputStream.close();
}
}
案例:有逼格的代码
int read;
while ((read=fileInputStream.read())!=-1) {
System.out.println((char)read);
}
案列:
package com.baizhi.entity;
import java.io.File;
import java.io.FileInputStream;
public class TestIOFile2 {
public static void main(String[] args) throws Exception{
File file = new File("E:\\ideaworkspace\\project01\\maven01\\src\\main\\resources\\File\\a.txt");
FileInputStream fis = new FileInputStream(file);
int r;
byte[] b = new byte[3];
while (true){
r = fis.read(b);
if(r==-1){
break;
}else{
System.out.println(new String(b,0,r));
}
}
fis.close();
}
}
文件的复制
1B = 8bit
1024B = 1KB
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB
1024TB = 1PB
1024PB = 1EB
1024EB = 1ZB
案例:
package cn.wzx;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Demo4 {
public static void main(String[] args) throws Exception{
File yuanFile = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/IO上.wmv");
File mubiaoFile = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file1/赵帅私密视频.wmv");
FileInputStream fInputStream = new FileInputStream(yuanFile);
FileOutputStream fileOutputStream = new FileOutputStream(mubiaoFile);
int len;
byte[] b = new byte[1024*1024];
while ((len = fInputStream.read(b))!=-1) {
fileOutputStream.write(b,0,len);
}
l
fileOutputStream.close();
fInputStream.close();
System.out.println("复制完毕");
}
}
过滤流
1.缓冲流BufferedOutputStream/BufferedInputStream
package cn.wzx;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
public class Demo5 {
public static void main(String[] args) throws Exception{
File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
bufferedOutputStream.write(65);
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
}
2.数据过滤流:可以传输基本的数据类型的数据 8中基本数据类型DataOutputStream DataInputStream
案例:
package cn.wzx;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Demo5 {
public static void main(String[] args) throws Exception{
File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/b.dat");
FileOutputStream fileOutputStream = new FileOutputStream(file);
DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);
dataOutputStream.writeInt(300);
dataOutputStream.flush();
dataOutputStream.close();
FileInputStream fileInputStream = new FileInputStream(file);
DataInputStream dataInputStream = new DataInputStream(fileInputStream);
int readInt = dataInputStream.readInt();
System.out.println(readInt);
}
}
3.ObjectOutputStream/ObjectInputStream 对象输出/输入流 需实现序列化Serializable接口
序列化:将一个对象写入一个持久存储设备(硬盘)的文件中
反序列化:将一个文件中的对象数据读入内存程序中
transient:修饰的属性不参与序列化,当序列化的时候,这个属性时默认值(即使初始化也是默认值)
案列:
package com.wzx;
import java.io.*;
public class TestIOFile3 {
public static void main(String[] args) throws Exception {
Student4 student4 = new Student4("王仨",18,new Address("北京","46131",1101011452L));
File file = new File("E:\\ideaworkspace\\project01\\maven01\\src\\main\\resources\\File\\a.txt");
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(student4);
oos.flush();
oos.close();
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
if(o instanceof Student4){
Student4 student = (Student4)o;
System.out.println(student);
}
ois.close();
}
}
class Student4 implements Serializable {
private String name;
private int age;
private Address address;
public Student4() {
}
public Student4(String name, int age, Address address) {
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Student4{" +
"name='" + name + '\'' +
", age=" + age +
", address=" + address +
'}';
}
}
class Address implements Serializable{
private String city;
private transient String email;
private Long telephone;
public Address() {
}
public Address(String city, String email, Long telephone) {
this.city = city;
this.email = email;
this.telephone = telephone;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Long getTelephone() {
return telephone;
}
public void setTelephone(Long telephone) {
this.telephone = telephone;
}
@Override
public String toString() {
return "Address{" +
"city='" + city + '\'' +
", email='" + email + '\'' +
", telephone=" + telephone +
'}';
}
}
字符流
编码
编码的由来:
世界上第一台计算机诞生在美国 发明人是冯诺依曼 现在所使用的电脑都是冯诺依曼结构 埃尼拉克(第一台计算机名字)
现在在研究 量子计算机
ASCII码表 最小的码表
其他的国家也要用
中国
大陆(简体)
GB2312(国标) 建国初期天朝政府 的某个部门 指定了码表
刘-----1234
洋------2345
GBK(国标扩充)
两岸 台湾 香港 澳门
繁体
BIG5
1234----龍
编码 解码
iso8859-1 欧洲国际的码表
Unicode编码 万国码 UTF-8
因为 Linux系统采用的UTF-8
windows GBK
字符输出流(FileWrite)
write(int a)
write(char[] c)
write(String s)
案例:
package cn.wzx;
import java.io.File;
import java.io.FileWriter;
public class Demo7 {
public static void main(String[] args) throws Exception{
File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
FileWriter fileWriter = new FileWriter(file,true);
String string = "最美的不是下雨天,而是和你一起躲过屋檐";
fileWriter.write(string);
fileWriter.flush();
fileWriter.close();
System.out.println("写入完毕");
}
}
字符输入流(FileReader)
read():每次读一个字符
read(char[] c):每次读字符数组长度,将读到的数据存储到数组中 返回值是每次读到几个字符
案例:
package cn.wzx;
import java.io.File;
import java.io.FileReader;
public class Demo7 {
public static void main(String[] args) throws Exception{
File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
FileReader fileReader = new FileReader(file);
char[] c = new char[5];
int len;
while ((len=fileReader.read(c))!=-1) {
System.out.println(new String(c,0,len));
}
fileReader.close();
}
}
字符过滤流
1.BufferedWriter/BufferedReader 具有缓冲的字符过滤流
案例:
package cn.wzx;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class Demo7 {
public static void main(String[] args) throws Exception{
File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
String string = "世界上头发最多的男人";
bufferedWriter.write(string, 0, 3);
bufferedWriter.close();
}
}
案例:readLine():每次可以读一行
package cn.wzx;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Demo7 {
public static void main(String[] args) throws Exception{
File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String string = "";
while (true) {
string = bufferedReader.readLine();
if (string==null) {
break;
} else {
System.out.println(string);
}
}
}
}
2.PrintWriter :可以直接输出一行字符串,不用创建节点流,也具有基本数据类型的功能(将基本数据类型转换为char类型进行输出)
package cn.wzx;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
public class Demo7 {
public static void main(String[] args) throws Exception{
File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
PrintWriter printWriter =new PrintWriter(file);
printWriter.println("大将南征胆气豪");
printWriter.println("腰横秋水雁翎刀");
printWriter.println("风吹鼍鼓山河动");
printWriter.println("电闪旌旗日月高");
printWriter.close();
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String string ;
while (true) {
string = bufferedReader.readLine();
if (string==null) {
break;
} else {
System.out.println(string);
}
}
}
}
3.桥转换流OutputStreamWriter/InputStreamReader
作用:可以将一个字节流转换为一个字符流
package cn.wzx;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.PrintWriter;
public class Demo7 {
public static void main(String[] args) throws Exception{
File file = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
PrintWriter printWriter = new PrintWriter(file);
printWriter.print(23);
printWriter.flush();
FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "GBK");
int read = inputStreamReader.read();
System.out.println(read);
int read1 = inputStreamReader.read();
System.out.println(read1);
printWriter.close();
inputStreamReader.close();
}
}
关于IO流异常的处理
package cn.wzx;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Demo7 {
public static void main(String[] args){
File file1 = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/a.txt");
File file2 = new File("D:/MyEclipse/Workspaces/JAVASE/JAVASE/file/b.txt");
FileOutputStream fileOutputStream = null;
FileInputStream fileInputStream = null;
try {
fileOutputStream = new FileOutputStream(file2);
fileInputStream = new FileInputStream(file1);
int len;
byte[] b = new byte[1024*1024];
while ((len = fileInputStream.read(b))!=-1) {
fileOutputStream.write(b, 0, len);
}
System.out.println("复制完毕");
} catch (Exception e) {
System.out.println("异常解决了");
e.printStackTrace();
}finally{
try {
if (fileInputStream!=null&&fileOutputStream!=null) {
fileInputStream.close();
fileOutputStream.close();
}
} catch (Exception e2) {
System.out.println("空指针异常");
}
}
}
}