Java基础视频笔记(六):本地文件操作

Java基础视频笔记第五,本地文件操作:

1、File类简介:

File file = new File("D:\\File.txt");//读取本地的file文件
创建文件:file.createNewFile();


* 如果不指定路径,默认会创建到工程目录下
   Eclipse的目录是延时显示的,不会实时刷新,按F5刷新


* 填写相对路径时,格式为:  bin/hello.txt
   当前目录的上一级目录填写:  ../hello.txt
   上两级文件夹目录:  ../../hello.txt

删除文件:file.dekete();

重命名:修改路径即可,但要保持在同一目录下

 

  1. import java.io.File;  
  2. import java.io.IOException;  
  3.   
  4. public class FileDemo01 {  
  5.   
  6.     public static void main(String[] args) {  
  7.         File file = new File("D:\\File_II.txt");  
  8.         if (file.exists()) {  
  9. /*          //文件 
  10.             System.out.println(file.isFile()); 
  11.             //路径 
  12.             System.out.println(file.isDirectory()); 
  13. */  
  14.             //file.delete();  
  15.             //System.out.println("文件删除成功");  
  16.             //重命名  
  17.             File file2 = new File("D:\\File_Yo.txt");  
  18.             file.renameTo(file2);  
  19.             //跨目录移动,修改路径即可,但一定要在同一分区下  
  20.             // File file2 = new File("D:\\bin\\File_Yo.txt");  
  21.         }else {  
  22.             System.out.println("文件不存在,开始创建...");  
  23.             try {  
  24.                 file.createNewFile();  
  25.                 System.out.println("\r文件成功创建!");  
  26.             } catch (IOException e) {  
  27.                 System.out.println("\r无法创建...");  
  28.             }  
  29.         }  
  30.     }  
  31. }  


2、文件夹的创建、删除、重命名

// 创建文件夹
File folder = new File("Folder_new");
folder.mkdir();

 

反斜杠 \ 号需要用"\\" 表示;


// 创建多级文件夹,用 mkdirs()
File folder = new File("Folder_new\Jack\Michael\system");
folder.mkdirs();

//重命名
File folder = new File("Folder_new\Jack");
File newFolder = new File("Folder_new\Jack_new")
folder.renamoTo(newFolder);

//删除
folder.delete(); // 只能删除空文件夹

 

  1. import java.io.File;  
  2. import java.io.IOException;  
  3. import java.util.TreeSet;  
  4.   
  5.   
  6. public class FileDemo02 {  
  7.   
  8.     public static void main(String[] args) {  
  9.         File folder = new File("D:\\FileDemo");  
  10.         if (folder.mkdirs()) {  // 创建文件夹  
  11.             System.out.println("目录创建成功!");  
  12.         }else {  
  13.             if(folder.exists()){ // 检测文件夹是否存在  
  14.                 System.out.println("目录已存在");  
  15.             }else {  
  16.                 System.out.println("创建失败!");  
  17.             }  
  18.         }  
  19.         // 动态获取 分区名  文件夹名 ,再加上 文件名  
  20.         File file = new File(folder.getParent()+"\\"+folder.getName()+"\\"+"File_test.txt");  
  21.         try {  
  22.             file.createNewFile();  
  23.         } catch (IOException e) {  
  24.             e.printStackTrace();  
  25.         }  
  26.         System.out.println(folder.getParent()+"\\"+folder.getName()+"\\\\");  
  27.           
  28.         // 创建多级文件夹  
  29.         File fd = new File("D:/FileDemo/Jack/Michael/s2");//重命名后,这里要更改后才可以继续  
  30.         //fd.mkdirs();  // 多级文件夹目录,重命名的时候此行请注释掉  
  31.         // 根据需求重命名  
  32.         File fdrename = new File("D:/FileDemo/Jack/Michael/5");  
  33.         if(fd.renameTo(fdrename)){  // 给创建的文件夹以覆盖方式,重命名  
  34.             System.out.println("Done");  
  35.         }else {  
  36.             System.out.println("Fail");  
  37.         }  
  38.     }  
  39. }  


3、文件属性的读取:

  判断文件是否存在  file.exists();
  读取文件名称:   file.getName();
  读取文件路径:   file.getPath();
  读取文件绝对路径:file.getAbsolutePath();
  读取文件父级路径:file.getParent();
  读取文件大小:   file.length();
  判断文件是否被隐藏:file.isHidden();
  判断文件是否可读:file.canRead();
  判断文件是否可写:file.canWrite();
  判断文件是否为文件夹:file.isDirectory();

 


4、文件属性的设置

  将文件设定为可写:file.setWritable();
  将文件设定为可读: file.setReadable();
  将文件设定为只读: file.setReadOnly();

 

  1. import java.io.File;  
  2.   
  3. public class FileDemo4_ScanFile {  
  4.       
  5.     public static void scanFile(File dir,int tab){  
  6.         // 判断 传入的参数 是否是路径  
  7.         if (dir.isDirectory()) {  
  8.             // 如果是路径 就输出 文件和子文件的结构  
  9.             File next[] = dir.listFiles();  
  10.             for (int i = 0; i < next.length; i++) {  
  11.                 // 判断 如果是文件 输出文件名,如果是文件夹输出路径  
  12.                 System.out.println(next[i].getName());  
  13.                 // 获取tab值,让缩进显示更清晰  
  14.                 for (int j = 0; j < tab; j++) {  
  15.                     System.out.print("|--");  
  16.                 }  
  17.                 // 判断是否为路径  
  18.                 if (next[i].isDirectory()) {  
  19.                     // 如果是文件,输出下一级文件结构  
  20.                     scanFile(next[i],tab+1);  
  21.                 }  
  22.             }  
  23.         }  
  24.     }  
  25.   
  26.     public static void main(String[] args) {  
  27.         scanFile(new File("F:/SoftWare/China Mobile"),1);  
  28.     }  
  29. }  


5、遍历文件夹: FileDemo4_ScanFile.java

File next[] = file.listFiles();

6、文件的简单读写:

 File file = new File("text.txt");
 2 if(file.exits()){
 3 System.out.println("exist");
 4 // 添加 try catch 语句
 5 // 输出流
 6 FileInputStrean fis = new FileInputStream(file);
 7 InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
 8 // 带缓冲的Reader
 9 BufferedReader br = new BufferedReader(isr);
10 //String 存放读取数据
11 String line;
12 while(line = br.readLine() != null){
13 system.out.println(line); 
14 }
 

// 关闭流
br.close();
isr.close();
fis.close();
}

// 文件写入
FileOutputStream fos = new FileOutputStream();
OutputStreamWriter osw = new OutputStreamWriter();
BufferedWriter bw = new BufferWriter();

bw.write("覆盖内容");
bw.close();
osw.close();
fos.close();

  

详细代码:

    1. public class ReadWriteTestFile {  
    2.     public static void readFile(){  
    3.         File file = new File("D:/FileDemo/Jack/Michael/test.txt");  
    4.         if(file.exists()){  
    5.             System.out.println("文件存在,准备读取。。。");  
    6.             try {                 
    7.                 //三大读取流  
    8.                 // 首先创建 文件输入流  
    9.                 FileInputStream fis = new FileInputStream(file);  
    10.                 // 创建文件输入流的Reader  
    11.                 InputStreamReader isr = new InputStreamReader(fis);  
    12.                 // 创建带缓冲输入流  
    13.                 BufferedReader br = new BufferedReader(isr);  
    14.                 //把读取的流存入String  
    15.                 String line;  
    16.                 // 读取一行,如果不为空,说明文件没结束,就把文件输出  
    17.                 while((line = br.readLine()) != null){  
    18.                     System.out.println(line);  
    19.                 }  
    20.                 // while之外,读取一行为空,此时把文件流关闭  
    21.                 // 先打开的流后关闭,后打开的先关闭  
    22.                 br.close();  
    23.                 isr.close();  
    24.                 fis.close();  
    25.                 System.out.println("\r文件读取完毕!");  
    26.                   
    27.             } catch (FileNotFoundException e) {  
    28.                 e.printStackTrace();  
    29.             } catch (IOException e) {  
    30.                 e.printStackTrace();  
    31.             }  
    32.         }  
    33.     }  
    34.     // 文 件 覆 盖 方法  
    35.     public static void replaceFile(){  
    36.         try {  
    37.             File newFile = new File("D:/FileDemo/Jack/Michael/test.txt");  
    38.             FileOutputStream  fos = new FileOutputStream(newFile);  
    39.             OutputStreamWriter osw = new OutputStreamWriter(fos);  
    40.             BufferedWriter bw = new BufferedWriter(osw);  
    41.             // 给空文件覆盖内容,原始内容将消失  
    42.             bw.write("yo!!这是新加入的一行!!!"); // 文件被此内容覆盖  
    43.             System.out.println("覆盖完毕...");  
    44.             bw.close();  
    45.             osw.close();  
    46.             fos.close();  
    47.               
    48.         } catch (FileNotFoundException e) {  
    49.             e.printStackTrace();  
    50.         } catch (IOException e) {  
    51.             e.printStackTrace();  
    52.         }         
    53.     }  
    54.   
    55.     public static void main(String[] args) {  
    56.         //readFile();  
    57.         replaceFile();  
    58.     }  
    59. }
原文地址:https://www.cnblogs.com/jackchiang/p/4585189.html