File基本操作
This commit is contained in:
		
							parent
							
								
									01d80d8ad2
								
							
						
					
					
						commit
						2852b5f178
					
				
							
								
								
									
										58
									
								
								src/chapter7/Example01.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								src/chapter7/Example01.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,58 @@
 | 
			
		||||
package chapter7;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
 | 
			
		||||
public class Example01 {
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        /**
 | 
			
		||||
         * Step 1 创建普通文件
 | 
			
		||||
         */
 | 
			
		||||
        File file1 = new File("out/chapter7/file1.txt");
 | 
			
		||||
        if (file1.exists()) {
 | 
			
		||||
            // 如果文件存在就删除
 | 
			
		||||
            System.out.println("文件file1.txt存在");
 | 
			
		||||
            file1.delete();
 | 
			
		||||
            System.out.println("文件file1.txt已删除");
 | 
			
		||||
        } else {
 | 
			
		||||
            // 如果文件不存在就创建
 | 
			
		||||
            System.out.println("文件file1.txt不存在");
 | 
			
		||||
 | 
			
		||||
            try {
 | 
			
		||||
                file1.createNewFile();
 | 
			
		||||
                System.out.println("文件file1.txt已创建");
 | 
			
		||||
            } catch (Exception e) {
 | 
			
		||||
                e.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        System.out.println("\n");
 | 
			
		||||
 | 
			
		||||
        /**
 | 
			
		||||
         * Step 2 创建目录
 | 
			
		||||
         */
 | 
			
		||||
        File parent = file1.getParentFile();
 | 
			
		||||
        if (! parent.exists()) {
 | 
			
		||||
            // 如果父目录不存在就创建
 | 
			
		||||
            System.out.println("父目录不存在");
 | 
			
		||||
            parent.mkdirs();
 | 
			
		||||
            System.out.println("父目录已创建");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (file1.exists()) {
 | 
			
		||||
            // 如果文件存在就删除
 | 
			
		||||
            System.out.println("文件存在");
 | 
			
		||||
            file1.delete();
 | 
			
		||||
            System.out.println("文件已删除");
 | 
			
		||||
        } else {
 | 
			
		||||
            // 如果文件不存在就创建
 | 
			
		||||
            System.out.println("文件不存在");
 | 
			
		||||
            try {
 | 
			
		||||
                file1.createNewFile();
 | 
			
		||||
                System.out.println("文件已创建");
 | 
			
		||||
            } catch (Exception e) {
 | 
			
		||||
                e.printStackTrace();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										16
									
								
								src/chapter7/Example03.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								src/chapter7/Example03.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,16 @@
 | 
			
		||||
package chapter7;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
 | 
			
		||||
public class Example03 {
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        File file = new File("src/chapter7");
 | 
			
		||||
        if (file.isDirectory()) {
 | 
			
		||||
            System.out.println("是目录");
 | 
			
		||||
            String[] names = file.list();
 | 
			
		||||
            for (String name : names) {
 | 
			
		||||
                System.out.println(name);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										31
									
								
								src/chapter7/Example04.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								src/chapter7/Example04.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,31 @@
 | 
			
		||||
package chapter7;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.FilenameFilter;
 | 
			
		||||
 | 
			
		||||
public class Example04 {
 | 
			
		||||
    public static void main(String[] args) {
 | 
			
		||||
        // 创建 File 对象
 | 
			
		||||
        File file = new File("src/chapter7");
 | 
			
		||||
        // 创建过滤器对象
 | 
			
		||||
        FilenameFilter filter = new FilenameFilter() {
 | 
			
		||||
            // 实现 accept 方法
 | 
			
		||||
            @Override
 | 
			
		||||
            public boolean accept(File dir, String name) {
 | 
			
		||||
                File currFile = new File(dir, name);
 | 
			
		||||
                if (currFile.isFile() && name.endsWith(".java")) {
 | 
			
		||||
                    return true;
 | 
			
		||||
                } else {
 | 
			
		||||
                    return false;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        if (file.exists() && file.isDirectory()) {
 | 
			
		||||
            String[] names = file.list(filter);
 | 
			
		||||
            for (String name : names) {
 | 
			
		||||
                System.out.println(name);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user