checkfile.java
来自「《A first book of java》by Gary J.Bronson 」· Java 代码 · 共 47 行
JAVA
47 行
import java.io.*;
public class CheckFile
{
public static void main(String[] args)
throws java.io.IOException
{
File inFile = new File("C:\\jdk\\prices.dat"); // note that a full pathname
// requires two delimiters
File newName = new File("backup.dat"); // a relative pathname can also be used
if (inFile.exists())
{
System.out.println("The file prices.dat exists");
// check the type of file
if (inFile.isDirectory())
System.out.println(" This is a directory");
else if (inFile.isFile())
System.out.println(" This is a regular file");
// check if file is readable
if (inFile.canRead())
System.out.println(" The file can be read");
else
System.out.println(" The file is not readable");
// check if file is writable
if (inFile.canWrite())
System.out.println(" The file can be written to");
else
System.out.println(" The file is not writeable");
// report the file's length
System.out.println("The file length is " + inFile.length() + " bytes.");
// report the file's directory path
System.out.println("The pathname of this file's parent is " + inFile.getParent());
// rename the file
inFile.renameTo(newName);
System.out.println("The file has been renamed " + newName);
System.out.println("The full pathname for this file is " + newName.getAbsolutePath());
}
else
System.out.println("The file prices.dat does not exist.");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?