文件的创建与删除

当使用File类创建一个文件对象后,例如

File file = new File("C:\\myletter", "letter.txt");

如果目录中没有名字为letter.txt文件,文件对象file调用方法

public boolean createNewFile();

删除文件用方法file.delete

代码展示如下所示:

import java.io.File;
import java.io.IOException; public class Test03 { public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("E:/test", "xiaoming.txt");
if(!file.exists())
{
file.createNewFile();
}
} }

在使用createNewFile()方法时,记住要抛出异常,否则会报错。

在路径E:/下本来没有"xiaoming.txt",代码运行后,“xiaoming.txt”文件被创建。

运行可执行文件

当要执行一个本地机器上的可执行文件时,可以使用java.lang包中的Runtime类。首先使用Runtime类声明一个对象,如:

Runtime rc;

然后使用该类的getRuntime()静态方法创建这个对象:

rc = Runtime.getRuntime();

rc可以调用exec(String command)方法打开本地机器上的可执行文件或执行一个操作。

代码展示如下所示:

 import java.io.File;

 public class Test04 {

     public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Runtime rt = Runtime.getRuntime();
File file = new File("D:/eclipse", "eclipse.exe");
rt.exec(file.getAbsolutePath());
rt.exec("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe");
File file1 = new File("C:/Program Files (x86)/Google/Chrome/Application",
"chrome www.nwsuaf.edu.cn");
rt.exec(file1.getAbsolutePath());
File file2 = new File("C:/Program Files/Internet Explorer","IEXPLORE www.baidu.com");
rt.exec(file2.getAbsolutePath());
}
catch(Exception e){}
} }

代码运行后,第10条代码是打开eclipse,第11条代码时打开谷歌浏览器,第13条代码时用谷歌浏览器打开特定网址,第16条语句是用默认浏览器打开百度网址。

File file1 = new File("C:/Program Files (x86)/Google/Chrome/Application","chrome www.nwsuaf.edu.cn");
写代码的时候要注意,网址前面需要加上浏览器的名字

review23的更多相关文章

随机推荐

  1. 使用 Docker 部署 MongoDB 复制集

    启用三个 mongod 建立复制集. MongoDB 版本选择目前最新的稳定版 3.6.2-jessie. docker run --name mongo0 -d mongo:3.6.2-jessie ...

  2. spring security原理

    spring security通过一系列过滤器实现其功能,入口过滤器如下(web.xml): <filter> <filter-name>springSecurityFilte ...

  3. Python中的默认参数(转)

    add by zhj: Python设计者为何将默认参数设计成这样呢?参见Python函数参数默认值的陷阱和原理深究 原文:https://github.com/acmerfight/insight_ ...

  4. MySQL权限系统(一).The MySQL Access Privilege System 概述

    纯属个人阅读,如有翻译错误,请指出 The primary function of the MySQL privilege system is to authenticate a user who c ...

  5. Python线程包装器

    import threading import subprocess import time def need_thread(func, *args, **kwargs): def fun(): pr ...

  6. [LeetCode-21]Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...

  7. 解决You are using pip version 9.0.1, however version 9.0.3 is available. You should consider upgra

    直接运行命令:python -m pip install --upgrade pip

  8. 2.2 使用ARDUINO控制MC20发短信

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...

  9. getchar,scanf以及缓冲区

    getchar()是stdio.h中的库函数,它的作用是从stdin流中读入一个字符,也就是说,如果stdin有数据的话不用输入它就可以直接读取了.getch()和getche()是conio.h中的 ...

  10. 前端基础之css样式(选择器)

    一.css概述 CSS是Cascading Style Sheets的简称,中文称为层叠样式表,对html标签的渲染和布局 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. 例如 二.c ...