为什么每次给一个路径,file.exists()都返回false ??
- package cn.itheima.day01;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- public class FileTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- getFileList("F:\\Users\\Administrator\\Workspaces");
- }
- public static void getFileList(String path) {
-
- File file = new File("path");
- System.out.println(file.exists());
- if (!(file.exists()))
- throw new RuntimeException("文件不存在");
- else if (!(file.isDirectory()))
- throw new RuntimeException("文件非法:指定文件不是文件夹");
- bianLiWenJianJia(file);
- }
- public static void bianLiWenJianJia(File file) {
- File[] files = file.listFiles();
- for (int i = 0; i < files.length; i++) {
- if (files[i].isDirectory()) {
- bianLiWenJianJia(files[i]);
- }
- else {
- String string = files[i].getName();
- PrintWriter printWriter;
- try {
- printWriter = new PrintWriter("C:\\Users\\Administrator\\Desktop\\文件列表.txt");
- printWriter.println(string);
- }
- catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- }
复制代码 |