directory.java

来自「Java学习源代码检索系统免费版」· Java 代码 · 共 59 行

JAVA
59
字号
//==============================================================
// Directory.java - Demonstrates the File class
//
// Java学习源代码检索系统 Ver 1.0 20031015 免费正式版
// 版权所有: 中国IT认证实验室(www.ChinaITLab.com)
// 程序制作: ChinaITLab网校教研中心
// 主页地址: www.ChinaITLab.com    中国IT认证实验室
// 论坛地址: bbs.chinaitlab.com  
// 电子邮件: Java@ChinaITLab.com
//==============================================================

import java.io.*;

public class Directory {

 // Input a string
 public static String readLine()
  throws IOException {
  BufferedReader br = 
   new BufferedReader(new InputStreamReader(System.in));
  return br.readLine();
 }

 // Prompt for and input a string
 public static String readLine(String prompt)
  throws IOException {
  System.out.print(prompt);
  return readLine();
 }

 // Construct File object for directory path
 public static File getFileForPath(String path)
  throws IOException {
  File dir = new File(path);
  if (!dir.isDirectory())
   throw new IOException("Not a directory");
  return dir;
 }

 // Main program method
 public static void main(String args[]) {
  String path;
  try {
   // Get pathname from command line or prompt user
   if (args.length > 0)
    path = args[0];
   else
    path = readLine("Path name? ");
  // List directory
   File dir = getFileForPath(path);
   String[] filenames = dir.list();
   for (int i = 0; i < filenames.length; i++)
    System.out.println(filenames[i]);
  } catch (IOException e) {            // Trap exception
   System.err.println(e.toString());   // Display error
  }
 }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?