review23
文件的创建与删除
当使用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的更多相关文章
随机推荐
- Servlet学习笔记【2】---Http数据包
本文主要讲Http协议相关知识. 1 Http协议特点 单向性:客户端和服务端是单向通信的,只有客户端发请求,服务端才会响应产生.(异于推送模式) 无状态:协议本身并没有状态的记录,当客户端多次访问服 ...
- Python代码实现删除一个list里面的重复元素
lst=[11,22,33,44,22,11,22,44] print(list(set(lst))) # 打印结果:[33, 11, 44, 22] d = {} for index,item in ...
- jQuery 属性操作
1 css操作 2 文本操作 3 属性操作 4 位置 5 尺寸 1.css操作 addClass();// 添加指定的CSS类名. removeClass();// 移除指定的CSS类名. hasCl ...
- Server responded "Algorithm negotiation failed" SSH Secure链接服务器错误
Ubuntu 16.04安装openssh-server后,使用ssh客户端连接时可能报此错误,情况如下图所示: server responded "algorithm negotiatio ...
- 《Mining of Massive Datasets》笔记(一)
数据挖掘基本概念 数据挖掘定义 最广为接受得到定义是,数据挖掘是数据"模型"的发现过程.而"模型"却可以有多种含义. 1)统计建模 统计学家认为数据挖掘就是统计 ...
- 升级到tomcat8遇到The method getDispatcherType() is undefined for the type HttpServletRequest
今天升级到tomcat8,发现原来的项目不能运行了,遇到下面的错误:The method getDispatcherType() is undefined for the type HttpServl ...
- MySQL basics
@1: MySQL有三大类数据类型, 分别为数字.日期\时间.字符串, 这三大类中又更细致的划分了许多子类型: 数字类型 整数: tinyint.smallint.mediumint.int.bigi ...
- Guide to Spring @Autowired
Guide to Spring @Autowired Spring希望由@Autowired注解的依赖在某个依赖bean被构造时是可以访问的.如果框架不能解析这个用于wiring的bean,就会抛出异 ...
- Django 之models进阶操作
到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行数据库操作 ...
- 【LeetCode】【矩阵旋转】Rotate Image
描述 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...