当使用File类创建一个文件对象后,例如:
File file = new File("C:\\myletter","letter.txt");
如果C:\myletter目录中没有名字为letter.txt文件,文件对象file调用方法:
public boolean createNewFile();
可以在C:\myletter目录中建立一个名字为letter.txt的文件。文件对象调用方法public boolean delete()可以删除当前文件,例如:
file.delete();
当要执行一个本地机器上的可执行文件时,可以使用java.lang包中的Runtime类。首先使用Runtime类声明一个对象:
Runtime ec;
然后使用该类的getRuntime()静态方法创建这个对象:
ec = Runtime.getRuntime();
ec可以调用exec(String command)方法打开本地机器上的可执行文件或执行一个操作。
例如,Runtime对象打开Windows平台上的记事本程序和浏览器:
import java.io.*; public class Main { public static void main(String args[]) { try { Runtime ce = Runtime.getRuntime(); File file = new File("c:/windows","Notepad.exe"); ce.exec(file.getAbsolutePath()); file = new File("C:\\Program Files\\Internet Explorer","IEXPLORE www.sohu.com"); ce.exec(file.getAbsolutePath()); } catch(Exception e) { System.out.println(e); } } }
本文固定URL:https://www.dotcpp.com/course/864