黑马程序员技术交流社区

标题: IO流操作的小问题? [打印本页]

作者: 金肖    时间: 2012-5-19 23:22
标题: IO流操作的小问题?
怎么样从键盘上录入一个目录,比如这种格式:  F:\\d\\a  并进行判断?
作者: 袁錦泰    时间: 2012-5-19 23:27
涉及到判断? 应该会用到正则表达式吧? 沾上正则我可真不会写,我现在看到IO问题就知道是你问的。
作者: 孙宇晨    时间: 2012-5-19 23:27
你是想问判断什么  是不是一个文件夹还是有没有这个目录
作者: 孙宇晨    时间: 2012-5-19 23:33
读取键盘录入有一个标准的格式 视频上有的
bufferedReader bufr =new bufferedReader(new InputstreamReader(system.in))
只要用键盘录入.就用它 不知道你要找的是不是这个 你也没说清楚你要判断什么
作者: 金肖    时间: 2012-5-19 23:37
袁錦泰 发表于 2012-5-19 23:27
涉及到判断? 应该会用到正则表达式吧? 沾上正则我可真不会写,我现在看到IO问题就知道是你问的。 ...

呵呵,一下都被你看见了,
哦, 我就是想 这样在键盘上输入这个目录后,可以遍历出这个目录中的文件
作者: 袁錦泰    时间: 2012-5-20 11:33
本帖最后由 袁錦泰 于 2012-5-20 11:44 编辑
金肖 发表于 2012-5-19 23:37
呵呵,一下都被你看见了,
哦, 我就是想 这样在键盘上输入这个目录后,可以遍历出这个目录中的文件 ...

哥们儿,我写出来了,不知道和你心里面想的一样,你先看看再说。
作者: 袁錦泰    时间: 2012-5-20 11:36
本帖最后由 袁錦泰 于 2012-5-20 13:37 编辑
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStreamWriter;

  7. public class FileTest {

  8.     /**
  9.      * @param args
  10.      */
  11.     public static void main(String[] args) {

  12.         BufferedReader bufr = null;
  13.         BufferedWriter bufw = null;
  14.         File dir = null;
  15.         try {
  16.             bufr = new BufferedReader(new InputStreamReader((System.in)));
  17.             bufw = new BufferedWriter(new OutputStreamWriter(System.out));
  18.             String path = null;
  19.             while ((path = bufr.readLine()) != null) {
  20.                 dir = new File(path);
  21.                 if (!(dir.exists())) {
  22.                     throw new RuntimeException("文件名錯誤");
  23.                 } else {
  24.                     listAll(dir, 0);
  25.                 }
  26.             }
  27.         } catch (IOException e) {
  28.             throw new RuntimeException("Error");
  29.         } finally {
  30.             try {
  31.                 if (bufr != null) {
  32.                     bufr.close();
  33.                 }
  34.             } catch (IOException e) {
  35.                 bufr = null;
  36.                 throw new RuntimeException("Error");
  37.             }
  38.             try {
  39.                 if (bufw != null) {
  40.                     bufw.close();
  41.                 }
  42.             } catch (IOException e) {
  43.                 bufw = null;
  44.                 throw new RuntimeException("Error");
  45.             }
  46.         }
  47.     }

  48.     public static void listAll(File dir, int level) {
  49.         System.out.println(getSpace(level) + dir.getName());
  50.         level++;
  51.         File[] files = dir.listFiles();
  52.         for (int i = 0; i < files.length; i++) {
  53.             if (files[i].isDirectory()) {
  54.                 listAll(files[i], level);
  55.             } else {
  56.                 System.out
  57.                         .println(getSpace(level) + files[i].getAbsolutePath());

  58.             }
  59.         }
  60.     }

  61.     private static String getSpace(int level) {

  62.         StringBuilder sb = new StringBuilder();
  63.         sb.append("|--");
  64.         for (int i = 0; i < level; i++) {
  65.             sb.insert(0, "  ");
  66.         }
  67.         return sb.toString();
  68.     }

  69. }
  70. /*
  71. 程序写得并不是很完美,但大体功能已经实现,你可以检查一下,如果你在这基础上做了优化的话记得和我交流一下经验。
  72. */
复制代码

作者: 金肖    时间: 2012-5-20 11:55
袁錦泰 发表于 2012-5-20 11:36
  1. public class ListFiles {
  2. public static void main(String[] args) throws IOException {

  3. File pathName = getPath(); //获取键盘录入的目录路径

  4. listFiles(pathName);
  5. }
  6. /**
  7. * 对目录进行深度遍历
  8. * @param pathName
  9. */
  10. public static void listFiles(File pathName){

  11. File[] files = pathName.listFiles();
  12. for (File file : files) {
  13. if (file.isDirectory()) {
  14. listFiles(file);
  15. } else {
  16. System.out.println();
  17. System.out.println(file);
  18. }
  19. }
  20. }

  21. /**
  22. * 获取 键盘录入的路径,并判断输入的目录是否存在
  23. * @return
  24. */
  25. public static File getPath() {
  26. System.out.println("请输入目录:格式为:x:\\xx\\xx\\");
  27. BufferedReader bufr = new BufferedReader(new InputStreamReader(
  28. System.in));
  29. File file = null;
  30. String line;
  31. try {
  32. line = bufr.readLine();
  33. file = new File(line);
  34. if (!file.exists()) {
  35. throw new RuntimeException("该目录不存在,,请重新输入!");
  36. }
  37. }catch(IOException e){
  38. }

  39. return file;
  40. }
  41. }
复制代码
昨晚想出来的...
作者: 袁錦泰    时间: 2012-5-20 12:02
本帖最后由 袁錦泰 于 2012-5-20 12:04 编辑
金肖 发表于 2012-5-20 11:55
昨晚想出来的...

唉,昨晚晚睡会儿好了。你这写得比我那个高级。我还把控制台写上面了,刚开始就觉得用处不大。而且你对文件路径进行了判断,我想到那个之后就是不知道写在哪里。我再研究研究你这个。




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