fileinfo.java

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

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

import java.io.*;
import java.util.Date;

public class FileInfo {

 // 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 named file
 public static File getFileForFilename(String filename)
  throws IOException {
  File fi = new File(filename);
  // Do not move the following statements;
  // order is critical
  if (!fi.exists())
   throw new IOException("File not found");
  if (!fi.isFile())
   throw new IOException("Not a file");
  return fi;
 }

 // Show a labeled string
 public static void showLabel(String label, String s) {
  System.out.print(label);
  System.out.println(s);
 }

 // Display information about file fi
 public static void showInformation(File fi) {
  showLabel("Path     = ", fi.getPath());
  showLabel("Filename = ", fi.getName());
  showLabel("Length   = ",
   new Long(fi.length()).toString());
  showLabel("Readable = ", 
   new Boolean(fi.canRead()).toString());
  showLabel("Writable = ", 
   new Boolean(fi.canWrite()).toString());
  showLabel("Modified = ",
   new Date(fi.lastModified()).toString());
 }

 // Main program method
 public static void main(String args[]) {
  String filename;
  try {
   // Get pathname from command line or prompt user
   if (args.length > 0)
    filename = args[0];
   else
    filename = readLine("File name? ");
   File fi = getFileForFilename(filename);
   showInformation(fi);
  } catch (IOException e) {            // Trap exception
   System.err.println(e.toString());   // Display error
  }
 }
}

⌨️ 快捷键说明

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