1.File
File可以用于操作文件以及查看文件属性的方法
public class TestFile {
public static void main(String[] args) throws IOException {
File file2 = new File("C:\\Users\\WHD\\Desktop\\a.txt");
System.out.println(file2.createNewFile());
File file1 = new File("a.txt");
System.out.println(file1.createNewFile());
System.out.println("是否是一个文件" + file1.isFile());
System.out.println("是否是一个文件夹" + file1.isDirectory());
System.out.println("相对路径" + file1.getPath());
System.out.println("绝对路径" + file1.getAbsolutePath());
System.out.println("文件名称" + file1.getName());
System.out.println("文件大小" + file1.length());
System.out.println("文件是否存在" + file1.exists());
System.out.println("文件删除是否成功" + file1.delete());
File file3 = new File("A");
file3.mkdir();// make directory
File file4 = new File("B/C/D");
file4.mkdirs();
file3.delete();
file4.delete();
}
}
2.字节流(读取)
2.1InputStream
字节读取流父类,抽象类
2.2FileInputStream
父类为InputStream
方法有:
read():每次读取一个字节,返回值为读取的内容
read(byte [] data):返回值为读取的个数,读取到的内容存放在数组中
close():关闭资源
public class TestFileInputStream1 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("A.txt");
int data = fis.read();
System.out.println("第一次读取" + (char)data);
int readData = -1;
while((readData = fis.read()) != -1) {
System.out.println((char)readData);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.字节流(写入)
3.1OutputStream
字符写入流父类,抽闲类
3.2FileOutputStream
父类为OutputStream
方法:
write(int data):写一个字节
write(byte [] data):写一个字节数组
close()
public class TestFileOutputStream {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("B.txt");
fos.write(97);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.字符流(读取)Reader
字符读取流父类,抽象类
4.1InputStreamReader
可以指定读取编码格式
read():每次读取一个字符
public class TestInputStreamReader {
public static void main(String[] args) {
FileInputStream fis = null;
InputStreamReader isr = null;
try {
fis = new FileInputStream("A.txt");
isr = new InputStreamReader(fis);
int data = -1;
while((data = isr.read())!= -1) {
System.out.println((char)data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fis != null) {
fis.close();
}
if(isr != null) {
isr.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.1.1FileReader
只能按照本地默认的编码格式读取文件,字符流
public class TestFileReader {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("A.txt");
int data = -1;
while(( data = fr.read()) != -1) {
System.out.println((char)data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
// 关闭资源
}
}
}
4.2BufferedReader
独有的读取一行的方法:readLine()
public class TestBufferedReader1 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("A.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}
// 关闭资源
}
}
5.字符流(写入)Writer
字符写入流父类,抽象类
5.1OutputStreamWriter
Writer(String str)可以直接写入字符串,可以指定写入的编码格式
public class TestOutputStreamWriter1 {
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D.txt",true),"utf-8");
osw.write("hello world世界你好");
osw.close();
}
}
5.1.1FileWriter
只能按照本地默认的编码格式写入文件
public class TestFileWriter1 {
public static void main(String[] args) {
FileWriter fw;
try {
fw = new FileWriter("E.txt");
fw.write("abcdefg");
fw.flush(); // 刷新 表示将内容从内存刷新到硬盘
} catch (IOException e) {
e.printStackTrace();
}finally {
// 关闭资源
}
}
}
5.2BufferedWriter
独有的换行方法newLine()
public class TestBufferedWriter1 {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("F.txt")));
bw.write("abcd\n");
bw.write("efgh");
bw.newLine();
bw.write("hello world");
bw.flush();
}
}
6数据流
用于读取二进制文件(图片,视频,音频等)
DateInputStream读取二进制文件
DateOutputStream写入二进制文件
public class TestDataStream {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("C:\\Users\\WHD\\Desktop\\zs1.jpg");
DataInputStream dis = new DataInputStream(fis);
byte data [] = new byte[fis.available()];
dis.read(data);
DataOutputStream dos = new DataOutputStream(new FileOutputStream("copyzs.jpg"));
dos.write(data);
fis.close();
dis.close();
dos.close();
}
}
7.序列化
序列化:将对象写入二进制文件
反序列化:将写有对象的二进制文件读取为对象
要求:被序列化的对象的实现类必须实现Serializable接口,此接口是空接口,相当于一个标识
被transient修饰的属性不能被序列化
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 2751418021503949497L;
private String name;
private int age;
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 Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class TestObjectStream {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
Student stu = new Student("张三", 13);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("stu.txt"));
oos.writeObject(stu);
if (oos != null) {
oos.close();
}
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("stu.txt"));
Student student = (Student) ois.readObject();
System.out.println(student);
if (oos != null) {
ois.close();
}
}
}