比如下面的例子,
一般在命令行下
java Delete ft.txt
在Eclipse中有其他的变通的方式么?
怎样输入 ft.txt这个参数?
难道在 main()之前定义 string[] args数组?
可能问题比较幼稚,还望解答。
谢谢!
import java.io.*;
public class Delete {
public static void main(String[ ] args) {
if (args.length != 1) { // Check command-line arguments
System.err.println("Usage: java Delete ");
System.exit(0);
}
// Call delete( ) and display any error messages it throws.
try { delete(args[0]); }
catch (IllegalArgumentException e) {
System.err.println(e.getMessage( ));
}
}
public static void delete(String filename) {
File f = new File(filename);
if (!f.exists( )) fail("Delete: no such file or directory: " +filename);
if (!f.canWrite( )) fail("Delete: write protected: " + filename);
if (f.isDirectory( )) {
String[ ] files = f.list( );
if (files.length > 0)
fail("Delete: directory not empty: " + filename);
}
boolean success = f.delete( );
if (!success) fail("Delete: deletion failed");
}
protected static void fail(String msg) throws IllegalArgumentException {
throw new IllegalArgumentException(msg);
}
}