4.2 File

4.2.1 File类简介

java.io.File

看api:http://docs.oracle.com/javase/8/docs/api/

4.2.2 文件的创建,删除,重命名

方法:

  • exist()
  • createNewFile()
    • Atomically creates a new, empty file named by this abstract pathname if
      and only if a file with this name does not yet exist
    • 创建成功返回true,否则false 
    • 如果这个文件已经存在,不会重新创建
  • isFile()
  • isDirectory()
  • file.delete();
  • file.renameTo(fileWithNewName); 如果目标文件或者文件夹存在,重命名方法返回false。
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.         File file = new File("file1");  
  2. //        文件名字没有以/开头,是相对路径,相对于当前工程目录下  
  3. //        createNewFile()的时候会把文件创建在当前工程目录下  
  4.         if(file.exists()){  
  5.             System.out.println("File exists");  
  6.             System.out.println("isFile :" + file.<span style="background-color: rgb(255, 255, 153);">isFile</span>());  
  7.             System.out.println("isDirectory :" + file.<span style="background-color: rgb(255, 255, 153);">isDirectory</span>());  
  8.         }else {  
  9.             System.out.println("File does not exist");  
  10.             try {  
  11.                 file.<span style="background-color: rgb(255, 255, 153);">createNewFile</span>();  
  12.             } catch (IOException e) {  
  13.                 e.printStackTrace();  
  14.             }  
  15.         }  

相对路径:相对于当前工程目录下

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. File file = new File("file1");  
  2. File file1 = new File("bin/file1");  
  3. File file2 = new File("../file1");  

给文件重命名:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. File file = new File("file1");  
  2. if(file.exists()) {  
  3.     File <span style="background-color: rgb(255, 153, 102);">nameTo</span> = new File("newFileName");  
  4.     file.<span style="background-color: rgb(255, 255, 153);">renameTo</span>(<span style="color:#000000;background-color: rgb(255, 153, 102);">nameTo</span>);  
  5. }else {  
  6.     try {  
  7.         file.createNewFile();  
  8.     } catch (IOException e) {  
  9.         e.printStackTrace();  
  10.     }  
  11. }  

移动文件(用重命名方法):新的路径必须在同一个分区下,mac下本身就是一个分区。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. File nameTo = new File("src/newFileName");  

4.2.3 文件夹的创建,删除,重命名

方法:

  • mkdir(); 
    • 创建成功返回true,否则false 
    • 如果这个目录已经存在,不会重新创建
      [java]  view plain copy 在CODE上查看代码片 派生到我的代码片
      1. File folder = new File("newFolder");  
      2. if(folder.mkdir()){  
      3.     System.out.println("Folder created");  
      4. }else{  
      5.     if (folder.exists()) {  
      6.         System.out.println("folder exists already");  
      7.     } else {  
      8.         System.out.println("cannot create folder");  
      9.     }  
      10. }  
    • 用mkdir创建一个文件夹在一个路径下,无论这个路径是相对路径还是绝对路径,这个路径必须存在,否则文件夹创建失败
      [java]  view plain copy 在CODE上查看代码片 派生到我的代码片
      1. File folder = new File("newFolder/one/two");  
      如果./newFolder/one不存在,folder.mkidr()返回false。

  • mkdirs() 根据路径创建整个文件夹结构
  • [java]  view plain copy 在CODE上查看代码片 派生到我的代码片
    1.  File folder = new File("newFolder/one/two");  
    2.         if(folder.<span style="background-color: rgb(255, 255, 153);">mkdirs</span>()){  
    3.             System.out.println("Folder created");  
    4.         }else{  
    5.             if (folder.exists()) {  
    6.                 System.out.println("folder exists already");  
    7.             } else {  
    8.                 System.out.println("cannot create folder");  
    9.             }  
    10.         }  
  • folder.delete(); 只有文件夹是空文件夹的时候,才可以删除成功。

    给文件夹重命名
    [java]  view plain copy 在CODE上查看代码片 派生到我的代码片
    1. File folder = new File("newFolder");  
    2. if (folder.mkdirs()) {  
    3.     System.out.println("Folder created");  
    4. else {  
    5.     if (folder.exists()) {  
    6.         System.out.println("folder exists already");  
    7.     } else {  
    8.         System.out.println("cannot create folder");  
    9.     }  
    10. }  
    11. File folder_renamed = new File("newFolder-renamed");  
    12. if(folder.renameTo(folder_renamed)){  
    13.     System.out.println("renamed");  
    14. else{  
    15.     System.out.println("rename failure");  
    16. }  

4.2.4 文件属性的读取

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //准备工作: 在工程目录下创建一个文件,名字是file.txt,输入些内容  
  2. File f = new File("file.txt");  
  3. System.out.println("file exists : " + f.exists());  
  4. System.out.println("file name : " + f.getName());  
  5. System.out.println("file reletive path: " + f.getPath());  
  6. System.out.println("file absolute path: " + f.getAbsolutePath());  
  7. System.out.println("file's parent path: " + new File(f.getAbsolutePath()).getParent());  
  8. System.out.println("file's length: " + f.length());  
  9. System.out.println("file is hidden:" + f.isHidden());  
  10. System.out.println("file can read:" + f.canRead());  
  11. System.out.println("file can white:" + f.canWrite());  
  12. System.out.println("file is direcotry:" + f.isDirectory());  

4.2.5 文件属性的设置

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //准备工作: 在工程目录下创建一个文件,名字是file.txt,输入些内容  
  2. File f = new File("file.txt");  
  3. //将文件设定为可读  
  4. f.setWritable(true);  
  5. //将文件设计为可读  
  6. f.setReadable(true);  
  7. //将文件设计为只读  
  8. f.setReadOnly();  

4.2.6 递归遍历文件夹

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public static void main(String[] args) throws IOException {  
  2.     File folder = new File("newFolder");  
  3.     printFolderFiles(folder, 1);  
  4. }  
  5.   
  6. public static void printFolderFiles(File dir, int tab){  
  7.     if(dir.isDirectory()){  
  8.         File[] arrFiles = dir.listFiles();  
  9.         for (int i = 0; i < arrFiles.length; i++) {  
  10.             for (int j = 0; j < tab; j++) {  
  11.                 System.out.print("|---");  
  12.             }  
  13.             System.out.println(arrFiles[i].getName());  
  14.             if(arrFiles[i].isDirectory()){  
  15.                 printFolderFiles(arrFiles[i], tab+1);  
  16.             }  
  17.         }  
  18.     }else{  
  19.         System.out.println("path is not directory");  
  20.     }  
  21. }  

4.2.7 文件的简单读写

文件输入(read file to ram)

  1. 创建FileInputStream
  2. 创建InputStreamReader
  3. 创建带BufferedReader(带buffer的reader)
    [java]  view plain copy 在CODE上查看代码片 派生到我的代码片
    1. File file = new File("file.txt");  
    2. if(file.exists()){  
    3.     FileInputStream fis = null;  
    4.     try {  
    5.         fis = new FileInputStream(file);  
    6.         //FileInputStream是字节流, for reading streams of raw bytes  
    7.         //An InputStreamReader is a bridge from byte streams to character streams: It  
    8.         //reads bytes and decodes them into characters using a specified charset  
    9.         //  
    10.         InputStreamReader isr = new InputStreamReader(fis, "UTF-8");  
    11.         BufferedReader br = new BufferedReader(isr);  
    12.         String line;  
    13.         while((line = br.readLine()) != null){  
    14.             System.out.println(line);  
    15.         }  
    16.         br.close();  
    17.         isr.close();  
    18.         fis.close();  
    19.     } catch (FileNotFoundException e) {  
    20.         e.printStackTrace();  
    21.     } catch (UnsupportedEncodingException e) {  
    22.         e.printStackTrace();  
    23.     } catch (IOException e) {  
    24.         e.printStackTrace();  
    25.     }  
    26. }  

文件output(write ram  to file)

  1. 创建文件的输出流
  2. 创建带有输出流的writer
  3. 创建带有buffer的输出writer
    [java]  view plain copy 在CODE上查看代码片 派生到我的代码片
    1. File newFile = new File("newFile.txt");  
    2. try {  
    3.     //new FileOutputStream的时候,如果文件不存在,会被创建  
    4.     FileOutputStream fos = new FileOutputStream(newFile);  
    5.     OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");  
    6.     BufferedWriter bw = new BufferedWriter(osw);  
    7.     //white方法会覆盖原来的文件  
    8.     bw.write("1111111111\n");  
    9.     bw.write("2222222222");  
    10.     bw.write("3333333333");  
    11.     bw.write("4444444444");  
    12.     bw.close();  
    13.     osw.close();  
    14.     fos.close();  
    15.   
    16. catch (FileNotFoundException e) {  
    17.     e.printStackTrace();  
    18. catch (UnsupportedEncodingException e) {  
    19.     e.printStackTrace();  
    20. catch (IOException e) {  
    21.     e.printStackTrace();  
    22. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值