六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 358|回复: 0

共5次每隔2秒检查指定文件是否存在 读取并强制删除

[复制链接]

升级  62%

7

主题

7

主题

7

主题

童生

Rank: 1

积分
31
 楼主| 发表于 2013-2-5 02:43:06 | 显示全部楼层 |阅读模式
package cn.wangy.io;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class IOTest {private String path = "d:/temp/aa";private inttimes = 5;private String content = "";public String doTrade() {while (times> 0) {try {Thread.sleep(2000);} catch(InterruptedException e) {e.printStackTrace();}if(checkFile()) {readFileContent();times =-1;} else{times--;}}return content;}/*** 检查rev文件夹中 文件是否存在** @return*/public boolean checkFile() {File file =new File(path);if(file.exists()) {System.out.println("exist");return true;}System.out.println("no exist");return false;}/*** 读取文件内容 然后存入content 删除文件*/public void readFileContent() {File file =new File(path);BufferedReader reader = null;try {reader = newBufferedReader(new FileReader(file));String line= "";while ((line= reader.readLine()) != null) {content +=line + "\n";}//reader.close(); 不关闭流 使file.delete()==falsedelFile();} catch(FileNotFoundException e) {e.printStackTrace();} catch(IOException e) {e.printStackTrace();}}/*** 调用运行环境 强制删除文件*/public void delFile() {Runtime rTime = Runtime.getRuntime();String str ="cmd /c del " + path;try {rTime.exec(str);} catch(IOException e) {e.printStackTrace();}}} 
package cn.wangy.io;public class Main {public static void main(String[] args) {String content = new IOTest().doTrade();System.out.println(content);}} 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表