创建文件
1.new File(String filepath)
String filepath = "D:\\a01.txt";
File file = new File(filepath);
file.createNewFile();
// 只有执行了createNewFile()方法后才能真正的在磁盘上创建该文件
System.out.println("文件1创建成功");
2.new File(File parent,String child)根据父目录文件+子路径创建
File parentfile = new File("D:\\");
String fileName = "a02.txt";
File file1 = new File(parentfile,fileName);
file1.createNewFile();
System.out.println("文件2创建成功");
3.new File(String parent,String child)根据父目录+子路径创建
String parentfile1 = "D:\\";
String fileName1 = "a03.txt";
File file2 = new File(parentfile1,fileName1);
file2.createNewFile();
System.out.println("文件3创建成功");
常用的文件方法
1.getName() 获取文件名字
File file = new File("D:\\a.txt");
file.createNewFile();
System.out.println("文件名字=" + file.getName());// a.txt
2.getAbsolutePath() 获取文件绝对路径
System.out.println("文件绝对路径=" + file.getAbsolutePath());// D:\a.txt
3.getParent() 获取文件父目录
System.out.println("文件父目录=" + file.getParent()); //D:\
4.length() 获取文件大小(字节) Utf-8一个汉字三个字节
System.out.println("文件大小(字节)=" + file.length());// 9
5.exists() 判断文件是否存在
System.out.println("文件是否存在=" + file.exists());// true
6.file.isFile() 判断是否是一个文件
System.out.println("是不是一个文件=" + file.isFile());// true
7.isDirectory() 判断是否是一个目录
System.out.println("是不是一个目录=" + file.isDirectory());// false
8. boolean delete删除空目录或文件
File file = new File("D:\\a.txt");
if (file.exists()) {
if (file.delete()) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
} else {
System.out.println("该文件不存在");
}
9.boolean mkdirs创建多级目录
File file1 = new File("D:\\demo\\a\\b\\c");
if (file1.exists()) {
System.out.println("该文件已经存在");
} else {
if (file1.mkdirs()) {
System.out.println("创建成功");
} else {
System.out.println("创建失败");
}
}
10.boolean mkdir创建一级目录
File file2 = new File("D:\\demo");
if (file1.exists()) {
System.out.println("该文件已经存在");
} else {
if (file1.mkdir()) {
System.out.println("创建成功");
} else {
System.out.println("创建失败");
}
}
FileInputStream
1. int read()一个字节一个字节的读
String filepath = "D:\\a.txt";
int readDate = 0;
// finally里需要关闭fileinputstream,但是fileinputstream作用域只在try里,需要扩大fileinputstream的作用域
FileInputStream fileinputstream = null;
try {
fileinputstream = new FileInputStream(filepath);
// int read()一个字节一个字节地读
while ((readDate = fileinputstream.read()) != -1) {
// readDate是int型需要转换成char型输出,byte就变成二进制数据了
System.out.print((char)readDate);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
fileinputstream.close();
}// hello world
2.int read(byte[] b)
String filepath = "D:\\a.txt";
int len = 0;
byte[] buf = new byte[8];
FileInputStream fileinputstream = null;
try {
fileinputstream = new FileInputStream(filepath);
// int read()一个字节一个字节地读
while ((len = fileinputstream.read(buf)) != -1) {
// readDate是int型需要转换成char型输出,byte就变成二进制数据了
System.out.print(new String(buf,0,len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
fileinputstream.close();
}// hello world
FileReader
new FileReader(File/String)
相关API:
1.new String(char[]):将char[]转换成String
2.new String(char[],off,len):将char[]的指定部分转换成String
1.int read() 一个字符一个字符的读
String filepath = "D:\\a.txt";
FileReader filereader = null;
int data = 0;
try {
filereader = new FileReader(filepath);
while ((data = filereader.read()) != -1) {
System.out.print((char)data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (filereader != null) {
try {
filereader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}// 你好呀
2.int read(char[] buf)
String filepath = "D:\\a.txt";
FileReader filereader = null;
int len = 0;
char[] buf = new char[8];
try {
filereader = new FileReader(filepath);
// int read(char[] buf)
while ((len = filereader.read(buf)) != -1) {
System.out.println(new String(buf,0,len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
} finally {
if (filereader != null) {
try {
filereader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}// 你好呀
FileWriter
new FileWriter(File/String):覆盖模式,相当于流的指针在首端
new FileWriter(File/String,true):追加模式,相当于流的指针在尾端
相关API:String类:toCharArray:将String转换成char[]
FileWriter使用后必须要关闭(close)或刷新(flush)否则写不到指定的文件
1.void write(int c)一个字符一个字符的写
String filepath = "D:\\a.txt";
FileWriter filewriter = null;
try {
filewriter = new FileWriter(filepath);
// void write(int c)一个字符一个字符的写
filewriter.write('Z');
} catch (IOException e) {
e.printStackTrace();
} finally {
if (filewriter != null) {
try {
filewriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}// H
2.void write(char[] buf)
String filepath = "D:\\a.txt";
FileWriter filewriter = null;
char[] buf = {'a','b','c','d'};
try {
filewriter = new FileWriter(filepath);
// void write(char[] buf)
filewriter.write(buf);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (filewriter != null) {
try {
filewriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}// abcd
3.void write(char[],off,len)
String filepath = "D:\\a.txt";
FileWriter filewriter = null;
try {
filewriter = new FileWriter(filepath);
// void write(char[],off,len)
filewriter.write("韩顺平".toCharArray(), 0, 3);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (filewriter != null) {
try {
filewriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}// 韩顺平
4.void write(String)
String filepath = "D:\\a.txt";
FileWriter filewriter = null;
try {
filewriter = new FileWriter(filepath);
// void write(String)
filewriter.write("你好北京~");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (filewriter != null) {
try {
filewriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}// 北京你好~
5.void write(String,off,len)
String filepath = "D:\\a.txt";
FileWriter filewriter = null;
try {
filewriter = new FileWriter(filepath);
// void write(String,off,len)
filewriter.write("上海天津", 0, 2);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (filewriter != null) {
try {
filewriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}// 上海
BufferedReader
String filepath = "D:\\a.txt";
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filepath));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}// 北京上海广州深圳
BufferedWriter
String filepath = "D:\\a.txt";
BufferedWriter bw = null;
try {
// new FileWriter(filepath,true)追加
bw = new BufferedWriter(new FileWriter(filepath,true));
bw.write("北京欢迎你");
// 插入一个和系统相关的换行
bw.newLine();
bw.write("上海欢迎你");
bw.newLine();
bw.write("广州欢迎你");
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}// 北京欢迎你 上海欢迎你 广州欢迎你 北京欢迎你 上海欢迎你 广州欢迎你
用BufferedReader和BufferedWriter进行文件拷贝(不能拷贝图片视频音乐等)
String srcfilepath = "D:\\a.txt";
String destfilepath = "D:\\b.txt";
BufferedReader br = null;
BufferedWriter bw = null;
String line;
try {
br = new BufferedReader(new FileReader(srcfilepath));
bw = new BufferedWriter(new FileWriter(destfilepath));
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
if (bw != null) {
bw.close();
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
ObjectOutputStream
String filepath = "D:\\data.dat";
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(filepath));
oos.writeInt(100);
oos.writeBoolean(true);
oos.writeChar('a');
oos.writeDouble(9.5);
oos.writeUTF("中国制造");
oos.writeObject(new Dog("旺财",10));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
ObjectInputStream
String filepath = "D:\\data.dat";
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(filepath));
System.out.println(ois.readInt());
System.out.println(ois.readBoolean());
System.out.println(ois.readChar());
System.out.println(ois.readDouble());
System.out.println(ois.readUTF());
Object dog = ois.readObject();
System.out.println("运行类型=" + dog.getClass());
System.out.println("dog信息=" + dog);
Dog dog2 = (Dog)dog;
System.out.println(dog2.getName());
System.out.println(dog2.getAge());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
// 100 true a 9.5 中国制造 运行类型=class lesson02.Dog dog信息=Dog [name=旺财, age=10] 旺财 10
1.读写顺序要一直
2.要求实现序列化或反序列化对象,实现Serializable
3.序列化的类中建议添加serialVersionUID,为了提高版本兼容性
4.序列化对象时,默认将里面所有属性都进行序列化,但除了static或transient修饰的成员
5.序列化对象时,要求里面属性的类型也需要实现序列化接口
6.序列化具备可继承性,也就是如果某类已经实现了序列化,则它的所有子类也已经默认实现了序列化
public class Dog implements Serializable{
private String name;
private int age;
private static final long serialVersionUID = 1L;
public Dog() {
}
public Dog(String name, int age) {
this.name = name;
this.age = 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;
}
@Override
public String toString() {
return "Dog [name=" + name + ", age=" + age + "]";
}
InputStreamReader
String filepath = "D:\\a.txt";
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath),"gbk"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}// 北京欢迎你 上海欢迎你 广州欢迎你
OutputStreamWriter
String filepath = "D:\\b.txt";
String charset = "gbk";
OutputStreamWriter osw = null;
try {
osw = new OutputStreamWriter(new FileOutputStream(filepath), charset);
osw.write("hi,IOS");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}// hi,IOS
PrintStream
PrintStream ps = System.out;
ps.print("john,hello");
try {
ps.write("nihao,IOS".getBytes());
// 可以修改输出的位置
System.setOut(new PrintStream("D:\\b.txt"));
System.out.println("hello,nizainaya");
} catch (IOException e) {
e.printStackTrace();
} finally {
ps.close();
}// john,hellonihao,IOS
// D:\\b.txt里输出hello,nizainaya
PrintWriter
PrintWriter pw = new PrintWriter(System.out);
PrintWriter pw1 = new PrintWriter(new FileWriter("D:\\b.txt"));
pw.print("你好,北京");
pw.close();// 你好,北京