File类代表一个文件或者文件目录
构造器:

  • File(String pathname)
  • File(String pathname,String child)
  • File(String parent,String child)
    parent目录路径 child表示从parent的下一层目录
  1. File file = new File("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\123.txt");
  2. File file1=new File("123.txt");
  3. File file2=new File("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream","123.txt");
  4. File file3=new File("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd","iostream");
  • getAbsolutePath()获取绝对路径
  • getAbsoluteFile()获取绝对路径表示的文件
  • getPath()获取路径
  • getParent()获取上一层文件目录路径,若无返回null
  • length()获取文件长度(字节数)
  • lastModified() 获取最后一次修改时间
  • String [] list()获取目录下所有文件和文件夹的名称数组,返回String数组
  • File[] listFiles()获取指定目录下的所有文件夹和文件目录File数组
  1. //获取绝对路径
  2. System.out.println(file.getAbsolutePath());
  3. //获取绝对路径表示的文件
  4. System.out.println(file.getAbsoluteFile());
  5. //获取路径
  6. System.out.println(file.getPath());
  7. //获取上一层文件目录路径,若无返回null
  8. System.out.println(file.getParent());
  9. //获取文件长度(字节数),不能获取目录的大小
  10. System.out.println(file.length());
  11. //获取最后一次修改时间
  12. System.out.println(file.lastModified());
  13. //获取目录下所有文件和文件夹的名称数组,返回String数组
  14. String []filenames=file3.list();
  15. for (String o:filenames)
  16. {
  17. System.out.println(o);
  18. }
  19. //获取指定目录下的所有文件夹和文件目录File数组
  20. File [] filenames2=file3.listFiles();
  21. for (File files : filenames2)
  22. {
  23. System.out.println(files.getName());
  24. }

renameTo()把文件重命名为指定的文件路径

  1. file2.renameTo(new File("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\suanfa\\binarytree\\123.txt"));

createNewFile() 创建文件,若文件存在,则不创建,返回false
mkdir() 创建文件目录,如果此文件存在,就不创建,如果上级目录不存在也不创建
mkdirs() 创建文件目录,上级目录不存在也会创建

  1. //创建文件,若文件存在,则不创建,返回false
  2. file1.createNewFile();
  3. //创建文件目录,如果此文件存在,就不创建,如果上级目录不存在也不创建
  4. file4.mkdir();
  5. //创建文件目录,上级目录不存在也会创建
  6. file4.mkdirs();

Java程序中输入与输出都是以流的方式的进行,I/O技术是用于处理设备之间的数据传输。在java中把不同的输入/输出源(键盘,文件,网络连接等)抽象表述为“流”(stream)。通过流的形式允许java程序使用相同的方式来访问不同的输入/输出源。stram是从起源(source)到接收的(sink)的有序数据。
主要的IO流

数据单位 字节流 字符流
抽象基类 InputStream OutputStream Reader Writer
节点流 FileInputStream FileOutputStream FileReader FileWriter
处理流 BufferedInputStream BufferedOutputStream(数据流、对象流、打印流等) BufferedReader BufferedWriter

Java重要的节点流有FileInputStream FileOutputStream(字节流)和FileReader FileWriter(字符流)
注意:字符流是对字节流进行封装,字符流的底层也是字节流,进行封装转化,更方便简单处理字符文档(可以用记事本打开的)
例子:使用FileInputStream FileOutputStream(字节流)进行文件复制

  • 第一步:创建流 输入流输出流分别指向
  • 第二步:操作流 创建一个中转站(字节数组),在使用输入流read方法将字节流读入数组,直到文件复制完成。
  • 第三步:关闭流
    需要使用try–catch–finally捕获异常
  1. /***
  2. * 使用FileInputStream和FileOutputStream实现文件复制
  3. */
  4. public class TestFileStream {
  5. public static void main(String[] args) {
  6. //1.创建流
  7. FileInputStream fis=null;
  8. FileOutputStream fos=null;
  9. try{
  10. fis = new FileInputStream("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\123.txt");
  11. fos = new FileOutputStream("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\12345.txt");
  12. //2.使用流
  13. //2.1使用一个中转站
  14. byte [] buf=new byte[1024];
  15. int len;//记录每次读取字节长度
  16. while((len=fis.read(buf))>=0)
  17. {
  18. fos.write(buf,0,len);//将读取的字节,写入新的文件中
  19. }
  20. }catch(Exception e)
  21. {
  22. e.printStackTrace();
  23. }finally {
  24. //3.关闭流
  25. if(fis!=null)
  26. {
  27. try {
  28. fis.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. if(fos!=null)
  34. {
  35. try {
  36. fos.close();
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. }
  42. }
  43. }

例子:使用FileReader FileWriter(字符流)进行txt文档的复制
注意:字符流只能复制字符文档,如果要复制视频、音频、图片等需要使用字节流,字符流复制跟字节流流程一样,就是中转站采用的是字符数组。

  • 第一步:创建流 输入流输出流分别指向
  • 第二步:操作流 创建一个中转站(字符数组),在使用输入流read方法将字符流读入数组,直到文件复制完成。
  • 第三步:关闭流
    需要使用try–catch–finally捕获异常
  1. /***
  2. * 使用FileReader和FileWriter实现文件复制
  3. */
  4. public class TestFileReader {
  5. public static void main(String[] args) {
  6. FileReader fr=null;
  7. FileWriter fw=null;
  8. try {
  9. //创建流
  10. fr = new FileReader("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\123.txt");
  11. fw = new FileWriter("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\12345.txt");
  12. fw = new FileWriter("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\12345.txt",true);//每次在后面追加,不覆盖
  13. //使用流
  14. char []buf=new char[1024];
  15. int n;
  16. while((n=fr.read(buf))>=0)
  17. {
  18. fw.write(buf,0,n);
  19. }
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }finally {
  23. //关闭流
  24. try {
  25. if(fr!=null)
  26. {
  27. fr.close();
  28. }
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. try {
  33. if(fw!=null)
  34. {
  35. fw.close();
  36. }
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. }
  42. }

作用:缓冲流分为缓存字节流和缓存字符流,目的是提高访问速度,提高便捷。
原理:分别开辟一个输入流缓冲区,和输出流缓冲区,不用每次读取写入都直接操作硬盘,中转站会先从缓冲区读取或写入数据,如果缓冲区没有或者缓存写满,这时缓冲区才会操作硬盘,大大减少操作硬盘次数,节约时间。
bb411cfaaa2daf2d330bcd6a894a8279.png
注意

  • 缓冲流BufferedInputStream BufferedOutputStream 和 BufferedReader BufferedWriter是高层流,资源回收时,只需要关闭高层流,低层流会自动的关闭。
  • 何时缓冲区写入硬盘,三种情况:缓冲区满、关闭缓冲流、手动使用flush函数。

例子1:使用FileInputStream FileOutputStream(字节流)进行文件复制,并使用BufferedInputStream BufferedOutputStream缓冲流提高速度。

  1. /**
  2. * 使用缓冲流Buffered来加快文件复制的过程
  3. */
  4. public class TestBufferedStream {
  5. public static void main(String[] args) {
  6. BufferedInputStream bis=null;
  7. BufferedOutputStream bos=null;
  8. try {
  9. bis=new BufferedInputStream(new FileInputStream("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\2007.07481.pdf"));
  10. bos=new BufferedOutputStream(new FileOutputStream("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\2007.pdf"));
  11. byte [] buf=new byte[1024];
  12. int n;
  13. while((n=bis.read(buf))!=-1)
  14. {
  15. bos.write(buf,0,n);
  16. }
  17. } catch (Exception e) {
  18. e.printStackTrace();
  19. }finally {//关闭流
  20. if(bis!=null)
  21. {
  22. try {
  23. bis.close();
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. if(bos!=null)
  29. {
  30. try {
  31. bos.close();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. }
  37. }
  38. }

例子2:使用 BufferedReader BufferedWriter(字符流)进行字符文档复制,要求一行一行的复制

  1. /**
  2. * 使用缓冲流字符流逐行读取文档,并复制到另一个文档上
  3. */
  4. public class TestBufferedReader {
  5. public static void main(String[] args) {
  6. BufferedReader bor=null;
  7. BufferedWriter bow=null;
  8. try {
  9. //创建流
  10. bor=new BufferedReader(new FileReader("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\123.txt"));
  11. bow=new BufferedWriter(new FileWriter("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\12345.txt"));
  12. //使用流
  13. String s;
  14. while((s=bor.readLine())!=null)//如果读不到数据,返回null
  15. {
  16. bow.write(s);//写入数据
  17. bow.newLine();//换行
  18. }
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }finally {
  22. //关闭流
  23. try {
  24. if(bor!=null)
  25. {
  26. bor.close();
  27. }
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. try {
  32. if(bow!=null)
  33. {
  34. bow.close();
  35. }
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }
  41. }

方便操作各种数据类型,可以将各种数据类型,写入文件,读取出来也能还原出各种数据类型

  1. public class TestDataInputStream {
  2. public static void main(String[] args) {
  3. DataInputStream dain=null;
  4. DataOutputStream daou=null;
  5. try{
  6. //创建数据流
  7. dain=new DataInputStream(new FileInputStream("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\Data.txt"));
  8. daou=new DataOutputStream(new FileOutputStream("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\Data.txt"));
  9. daou.writeBoolean(true);
  10. daou.writeChar('伟');
  11. daou.writeInt(12);
  12. daou.writeUTF("hhhhhhh");
  13. //读取数据
  14. System.out.println(dain.readBoolean());
  15. System.out.println(dain.readChar());
  16. System.out.println(dain.readInt());
  17. System.out.println(dain.readUTF());
  18. }catch (Exception e)
  19. {
  20. e.printStackTrace();
  21. }finally {
  22. if(dain!=null)
  23. {
  24. try {
  25. dain.close();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. if(daou!=null)
  31. {
  32. try {
  33. daou.close();
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39. }
  40. }

注意
使用对象流写入和读取对象时,需要将类继承Serialization(序列化)或者DeSerialization(反序列化)
对象(内存)—->字节数组(外存,网络)—->(对象)内存

  • static 修饰不参加序列化,可以使用transient使类的变量属性不参加序列化
  • 序列化后不能修改类的内容(除非在创建类的时候,生成类的序列号)
  • 如果对象写入文件,不仅要保证对象序列化,也要保证对象成员变量序列化
  1. public class TestObjectInputStream {
  2. public static void main(String[] args) {
  3. ObjectInputStream dain=null;
  4. ObjectOutputStream daou=null;
  5. try{
  6. //创建数据流
  7. daou=new ObjectOutputStream(new FileOutputStream("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\Data.txt"));
  8. dain=new ObjectInputStream(new FileInputStream("C:\\Users\\cw\\Desktop\\Project\\Java\\study\\src\\com\\cwstd\\iostream\\Data.txt"));
  9. daou.writeBoolean(true);
  10. daou.writeChar('伟');
  11. daou.writeInt(12);
  12. daou.writeUTF("hhhhhhh");
  13. daou.writeObject(new Person(0,"cwstd",22));
  14. //读取数据
  15. System.out.println(dain.readBoolean());
  16. System.out.println(dain.readChar());
  17. System.out.println(dain.readInt());
  18. System.out.println(dain.readUTF());
  19. System.out.println(dain.readObject().toString());
  20. }catch (Exception e)
  21. {
  22. e.printStackTrace();
  23. }finally {
  24. if(dain!=null)
  25. {
  26. try {
  27. dain.close();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. if(daou!=null)
  33. {
  34. try {
  35. daou.close();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }
  41. }
  42. }

版权声明:本文为cwstd原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/cwstd/p/14765437.html