wesker0918 发表于 2013-2-5 02:43:06

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

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);}} 
页: [1]
查看完整版本: 共5次每隔2秒检查指定文件是否存在 读取并强制删除