Windows 创建本地文件、写入文本内容、调用IE浏览器打开
try {
//先创建目录文件夹,如果直接创建文件会提示找不到对应的路径
File fileDir = new File("C:/dir/");
fileDir.mkdirs();
File file = new File("C:/dir/file4.txt"); // windows创建文件
file.createNewFile();
//将文本内容写入到本地文件
FileOutputStream o = new FileOutputStream(file.getAbsolutePath());
o.write(text.getBytes("UTF-8"));//不然乱码
o.close();
//调用本地ie浏览器
Runtime.getRuntime().exec("cmd /c start iexplore " + file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
各个系统环境下的浏览器调用
//打开浏览器并且访问地址的方法
//参数,传一个地址就可以。传入的地址需要注意,我在下面调用的方法中作说明
public static void browse(String url) throws Exception {
// 获取操作系统的名字
String osName = System.getProperty("os.name", "");
if (osName.startsWith("Mac OS")) {
// 苹果的打开方式
Class fileMgr = Class.forName("com.apple.eio.FileManager");
Method openURL = fileMgr.getDeclaredMethod("openURL",new Class[] { String.class });
openURL.invoke(null, new Object[] { url });
} else if (osName.startsWith("Windows")) {
// windows的打开方式。
//打开IE浏览器
Runtime.getRuntime().exec("cmd /c start iexplore " + url);
} else {
// Unix or Linux的打开方式
String[] browsers = { "firefox", "opera", "konqueror", "epiphany",
"mozilla", "netscape" };
String browser = null;
for (int count = 0; count < browsers.length && browser == null; count++) {
// 执行代码,在brower有值后跳出,
// 这里是如果进程创建成功了,==0是表示正常结束。
if (Runtime.getRuntime()
.exec(new String[] { "which", browsers[count] })
.waitFor() == 0)
browser = browsers[count];
if (browser == null)
throw new Exception("Could not find web browser");
else
// 这个值在上面已经成功的得到了一个进程。
Runtime.getRuntime().exec(new String[] { browser, url });
}
}
}