首先复制文件到一个目录下,然后将原来的文件删除。。  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();        }    } 有时可以将文件删除,有时就删除不了这是什么原因呢。。

解决方案 »

  1.   

    不是路径问题,,单独运行删除是可以删掉的。
    当我把copyFile()方法中 while循环里面的 Sysem.out.println("...");  注释去掉时,有的文件也能删掉。。有的也删不掉。
      

  2.   

    file.deleteOnExit();  试了不行。
      

  3.   

    我试了一下你的代码 在我机器上好使。还有就是 把 try catch 块移动一下,不要把所有代码都包含里面, 把最后在 finally 中关闭 文件流。
      

  4.   

    上面说的都试了。还是没有删掉文件。
     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();
           }    } 
      

  5.   

    试试都进行完操作后关闭读写文件流再删除文件.
    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(); // 然后再删除文件
        }
    }
      

  6.   

    myDelFile.deleteOnExit();
    这句是在程序退出的时候删除在程序中创建的临时文件createTempFile(String prefix, String suffix),不用于普通文件。
      

  7.   

    。程序中这么写的。
    ClassName.copyFile("c:\\aaa.rmvb","d:\\1232.rmvb"); //将c盘的aaa.rmvb文件复制到d盘并命名为1232.rmvb
    ClassName.delFile1("c:\\aaa.rmvb");//删除"c:\\aaa.rmvb"
    单独执行删除文件是可以删除的。
    用上面的那个Cut 我试了下可以。。
      

  8.   

    你在控制台打印下,看它返回的值是什么?会不会你的文件太大了,或者格式之类的?
    System.out.prinltn(myDelFile.delete());
      

  9.   

    System.out.prinltn(myDelFile.delete());  返回false
      

  10.   

    看看可以对文件进行写入吗? if (myDelFile.canWrite()) {
                                System.out.println("start"); System.out.println(myDelFile.delete());
    } else {
    System.out.println("error");
    }