File
file类是java操作文件和文件夹的类
绝对路径
File file1 = new File("E:\\idea\\untitled1\\src\\com\\cc\\iostudy\\file1.txt");
在E:\idea\untitled1\src\com\cc\iostudy下有一个file1.txt文件
相对路径
File file2 = new File("src/com/cc/iostudy/file2.txt");
在E:\idea\untitled1\src\com\cc\iostudy下有一个file2.txt文件
常用方法
boolean createNewFile() // 创建一个文件
String getParent() // (文件与目录)的上一级目录
boolean isDirectory() //判断当前的File对象是否是一个目录
boolean isFile() // 判断当前的File对象是否是一个文件
boolean mkdir() // 创建一个目录
boolean mkdirs() // 创建多级目录
String getAbsolutePath() //得到绝对路径
File[] listFiles() // 返回一个File[]数组,
boolean exists() //判断文件是否存在
应用 : 输出一个文件夹下所有文件的绝对路径
public class Demo1 {
public static void main(String[] args) throws IOException {
File src = new File("C:\\Users\\CC\\Desktop\\前端");
scanfFile(src);
}
public static void scanfFile (File src) {
if(src == null) {
System.out.println("文件不存在");
}
//递归出口
if(src.isFile()) {
System.out.println(src.getAbsolutePath());
return;
}
File[] list = src.listFiles();
for(File file : list) {
scanfFile(file);
}
}
}
文件结构
结果
FileInputStream和FileOutputStream
FileInputStream
1、 public void close() :关闭此输入流并释放与此流相关联的任何系统资源。
2、public int read(): 从输入流读取数据的下一个字节。3、 public int read(byte[] b): 该方法返回的int值代表的是读取了多少个字节,读到几个返回几个,读取不到返回-1
FileOutputStream
1、 public void close() :关闭此输出流并释放与此流相关联的任何系统资源。
2、 public void flush() :刷新此输出流并强制任何缓冲的输出字节被写出。
3、 public void write(byte[] b):将 b.length个字节从指定的字节数组写入此输出流。
4、 public void write(byte[] b, int off, int len) :从指定的字节数组写入 len字节,从偏移量 off开始输出到此输出流。 也就是说从off个字节数开始读取一直到len个字节结束
5、 public void write(int b) :将指定的字节输出流。
应用:复制一个整个文件夹(C:\Users\CC\Desktop\前端)的文件到 (D:\Users\CC\Desktop\前端)
public class Demo2 {
//复制一个整个文件夹(C:\Users\CC\Desktop\前端)的文件到 (D:\Users\CC\Desktop\前端)
public static void main(String[] args) throws FileNotFoundException {
File src = new File("C:\\Users\\CC\\Desktop\\前端");
File dest = new File("D:\\");
try {
copy(src,dest);
} catch (IOException e) {
e.printStackTrace();
}
}
//递归复制
public static void copy(File src,File dest) throws IOException {
if(src.isFile()) {
copyFile(src,dest);
return;
}
for (File file : src.listFiles()) {
if(file.isDirectory()) {
copyDir(file,dest);
}
copy(file,dest);
}
}
//文件夹生成
private static void copyDir(File src, File dest) {
String srcPath = src.getAbsolutePath();
String destPath = dest.getAbsolutePath();
String newPath = destPath + srcPath.substring(3);
File dir = new File(newPath);
if(!dir.exists()) {
dir.mkdirs();
}
}
//文件复制
private static void copyFile(File src, File dest) throws IOException {
String srcPath = src.getAbsolutePath();
String destPath = dest.getAbsolutePath();
String newPath = destPath + srcPath.substring(3);
FileInputStream is = new FileInputStream(src);
FileOutputStream os = new FileOutputStream(newPath);
byte[] buffer = new byte[1000];
int len = 0;
while ((len = is.read(buffer)) != -1) {
os.write(buffer,0,len);
os.flush();
}
is.close();
os.close();
}
}
结果
缓冲流
构造方法
public BufferedInputStream(InputStream in) :创建一个新的缓冲输入流,注意参数类型为InputStream。
public BufferedOutputStream(OutputStream out): 创建一个新的缓冲输出流,注意参数类型为OutputStream。
缓冲流往往比较高效
public class Demo3 {
public static void main(String[] args) throws IOException {
Long start = System.currentTimeMillis();
FileInputStream is = new FileInputStream("C:\\Users\\CC\\Desktop\\英语四级单词.pdf");
FileOutputStream os = new FileOutputStream("C:\\Users\\CC\\Desktop\\英语四级1.pdf");
byte[] buffer = new byte[10];
int len = 0;
while ((len = is.read(buffer)) != - 1) {
os.write(buffer,0,len);
os.flush();
}
is.close();
os.close();
Long end = System.currentTimeMillis();
System.out.println("时间:" + (end - start));
}
}
时间:3601 ms
public class Demo3 {
public static void main(String[] args) throws IOException {
Long start = System.currentTimeMillis();
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\CC\\Desktop\\英语四级单词.pdf"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\CC\\Desktop\\英语四级1.pdf"));
byte[] buffer = new byte[10];
int len = 0;
while ((len = bis.read(buffer)) != - 1) {
bos.write(buffer,0,len);
bos.flush();
}
bis.close();
bos.close();
Long end = System.currentTimeMillis();
System.out.println("时间:" + (end - start));
}
}
时间:2891
对象流
ObjectOutputStream
类,将Java对象的原始数据类型写出到文件,实现对象的持久存储。
ObjectInputStream
反序列化流,将之前使用ObjectOutputStream序列化的原始数据恢复为对象。
能将对象变成文本,储存在硬盘中
public class Student implements Serializable {
//序列号,唯一
private static final long serialVersionUID = 1L;
private int age;
private String name;
public Student() {
}
public Student(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
public class Demo4 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Student student = new Student(10,"cc");
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("E:\\idea\\untitled1\\src\\com\\cc\\iostudy\\a.txt"));
ObjectInputStream is = new ObjectInputStream(new FileInputStream("E:\\idea\\untitled1\\src\\com\\cc\\iostudy\\a.txt"));
os.writeObject(student);
Student s = (Student) is.readObject();
System.out.println(s);
}
}