黑马程序员技术交流社区

标题: 求大神帮忙!!!!!!!!!!!!! [打印本页]

作者: 蜡笔    时间: 2015-4-26 15:01
标题: 求大神帮忙!!!!!!!!!!!!!
为什么每次给一个路径,file.exists()都返回false ??

  1. package cn.itheima.day01;

  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.PrintWriter;

  5. public class FileTest {

  6.         /**
  7.          * @param args
  8.          */
  9.         public static void main(String[] args) {
  10.                 // TODO Auto-generated method stub
  11.                 getFileList("F:\\Users\\Administrator\\Workspaces");

  12.         }
  13.         public static void getFileList(String path) {
  14.                
  15.                 File file = new File("path");
  16.                 System.out.println(file.exists());
  17.                 if (!(file.exists()))
  18.                         throw new RuntimeException("文件不存在");
  19.                 else if (!(file.isDirectory()))
  20.                         throw new RuntimeException("文件非法:指定文件不是文件夹");
  21.                 bianLiWenJianJia(file);
  22.         }
  23.         public static void bianLiWenJianJia(File file) {
  24.                 File[] files = file.listFiles();
  25.                 for (int i = 0; i < files.length; i++) {
  26.                         if (files[i].isDirectory()) {
  27.                                 bianLiWenJianJia(files[i]);
  28.                         }
  29.                         else {
  30.                                 String string = files[i].getName();
  31.                                 PrintWriter printWriter;
  32.                                 try {
  33.                                         printWriter = new PrintWriter("C:\\Users\\Administrator\\Desktop\\文件列表.txt");
  34.                                         printWriter.println(string);
  35.                                         }
  36.                                 catch (FileNotFoundException e) {
  37.                                         // TODO Auto-generated catch block
  38.                                         e.printStackTrace();
  39.                                 }
  40.                         }       
  41.                 }
  42.         }

  43. }
复制代码

作者: Ray丶少年    时间: 2015-4-27 11:43
第19行 path不要加双引号,加了双引号当然不存在啦
作者: 邓士林    时间: 2015-4-27 12:15
变量和常量区别下




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2