一、定义
I/O操作-程序与其他外部设备进行交互的操作。
二、Java流
流(Stream)是指在计算机的输入输出操作中各部件之间的数据流动。
Java中I/O操作以流的形式进行交互操作。
三、分类
1、字节输入流InputStream 字节输出流Outputstream
进行语音、图片、音乐等I/O操作时一般选用字节流
public void byteIO() throws IOException {
File file = new File("F:\\test.txt");// 实例化外部设备对象
FileOutputStream fos = new FileOutputStream(file);// 建立通道
String str = "hello java";
byte[] outByte = str.getBytes();
fos.write(outByte);
fos.close();//关闭流
//
FileInputStream fis = new FileInputStream(file);// 建立通道
byte[] inByte = new byte[fis.available()];
// InputStream是字节输入流,针对字节操作,需要用字节数组接收,fis.available()可以读取的字节数
fis.read(inByte);// 读取数据
fis.close();//关闭流
//FileOutputStream fos = new FileOutputStream(file,true)填入参数true表示不覆盖文件内原内容,新内容追加到末尾
}
2、字符输入流Reader 字符输出流Writer
进行文本I/O操作时一般选用字符流
public void ReaderWriter() throws IOException {
File file = new File("F:\\abc.txt");
FileWriter fw = new FileWriter(file);
String str = "hello java";
fw.write(str);// 写入一个字符串
fw.close();
//fw.write(write, 0, length);写入一个字符串数组,从下标为0的元素开始,长度为数组长度length
FileReader fr = new FileReader(file);
char[] read = new char[1024];
fr.read(read);
fw.close();
}
3、缓冲流 BufferedReader BufferedWriter 转换流InputStreamReader
public void bufferedReader() throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);// System.in系统字节输入流,将字节输入流转换为字符输入流
BufferedReader br = new BufferedReader(isr);// 将字符输入流转换为字符缓冲流
String str = br.readLine();
isr.close();//关闭字符流
br.close();//关闭缓冲流
}
4、DataOutputStream DataInputStream
根据数据类型读或者写入数据
public void dataIO() throws IOException {
int number = 0;
String str = null;
File file = new File("F:\\test.txt");
FileOutputStream fos = new FileOutputStream(file);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeInt(number);// 写入一个int型数据
dos.writeUTF(str);// 写入一个String型数据
fos.close();
dos.close();
FileInputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fis);
number = dis.read();// 读取一个int型数据
str = dis.readUTF();// 数去一个String
fis.close();
dis.readInt();
}
5、ObjectOutputStream ObjectInputstream
public void ObjuecIO() throws IOException, ClassNotFoundException {
Student student = new Student();
ArrayList<Student> studentList = new ArrayList<Student>();
studentList.add(student);
student.name = "张三";
student.number = 1001;
File file = new File("F:\\objtest.txt");
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(student);// 写入对象student
oos.writeObject(studentList);// 写入对象数组studentList
fos.close();
oos.flush();
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
student = (Student) ois.readObject();// 读取对象时需要强制转型
fis.close();
ois.close();
}
四、File类
File可以表示一个文件也可以表示路径
public void fileDemo() throws IOException {
String dir = "F://test//test.txt";
File file = new File(dir);
if (file.isFile()) {// 判断file是否是一个文件
if (!file.exists()) {// file.exists()判读文件或者路径是否存在,存在返回true,反之false
file.createNewFile();// 创建文件
}
} else if (file.isDirectory()) {// 判断file是否是路径
if (!file.exists()) {
file.mkdirs();// 创建路径,.mkdirs()可以一次性创建多重路径
}
}
int index = dir.lastIndexOf("/");
String newDir = dir.substring(0, index);
File newFile = new File(newDir);
newFile.listFiles();// 返回一个字符串数组,元素为newFile下所有文件名称
newFile.listFiles();// 返回一个File数组,元素为newFile目录下所有文件
newFile.getName();// 返回文件名
}