首先复制文件到一个目录下,然后将原来的文件删除。。 public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
System.out.println("newPath:"+newPath);
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1024];
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
//System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace(); } }
public static void delFile1(String filePathAndName) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
java.io.File myDelFile = new java.io.File(filePath);
myDelFile.delete(); }
catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace(); } } 有时可以将文件删除,有时就删除不了这是什么原因呢。。
try {
int bytesum = 0;
int byteread = 0;
System.out.println("newPath:"+newPath);
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1024];
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
//System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace(); } }
public static void delFile1(String filePathAndName) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
java.io.File myDelFile = new java.io.File(filePath);
myDelFile.delete(); }
catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace(); } } 有时可以将文件删除,有时就删除不了这是什么原因呢。。
当我把copyFile()方法中 while循环里面的 Sysem.out.println("..."); 注释去掉时,有的文件也能删掉。。有的也删不掉。
public static void copyFile(String oldPath, String newPath) {
int bytesum = 0;
int byteread = 0;
System.out.println("newPath:"+newPath);
File oldfile = new File(oldPath);
InputStream inStream = null ;
FileOutputStream fs =null;
try {
if (oldfile.exists()) { //文件存在时
inStream = new FileInputStream(oldPath);//读入原文件
fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1024];
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
//System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
// inStream.close();
//fs.close();
}
}catch (FileNotFoundException e) {
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
inStream.close();
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
} } public static void delFile1(String filePathAndName) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
java.io.File myDelFile = new java.io.File(filePath);
myDelFile.deleteOnExit(); }
catch (Exception e) {
System.out.println("删除文件操作出错");
e.printStackTrace();
} }
import java.io.*;public class Cut {
public static void main(String[] args) throws Exception {
String srcFileName = args[0]; // 要复制的源文件名
String dstFileName = args[1]; // 粘贴生成的文件名
File srcFile = new File(srcFileName);
BufferedReader reader = new BufferedReader(new FileReader(srcFile));
PrintWriter writer = new PrintWriter(dstFileName);
writer.println("new file..."); // 随便写个标志,说明这个是新文件,删除这行就行了。
String line = null;
while ((line = reader.readLine()) != null) {
writer.println(line);
}
writer.flush();
writer.close();
reader.close(); // 关闭读写文件的流
srcFile.delete(); // 然后再删除文件
}
}
这句是在程序退出的时候删除在程序中创建的临时文件createTempFile(String prefix, String suffix),不用于普通文件。
ClassName.copyFile("c:\\aaa.rmvb","d:\\1232.rmvb"); //将c盘的aaa.rmvb文件复制到d盘并命名为1232.rmvb
ClassName.delFile1("c:\\aaa.rmvb");//删除"c:\\aaa.rmvb"
单独执行删除文件是可以删除的。
用上面的那个Cut 我试了下可以。。
System.out.prinltn(myDelFile.delete());
System.out.println("start"); System.out.println(myDelFile.delete());
} else {
System.out.println("error");
}