写了一个方法,操作两个文件,每次运行,建立b,读取a,写入b,删除a,然后把b改名为a
为什么第一次执行成功,第二次执行就不能删除a,改名b了呢?
能建立B
String line="";
new File(tbName+".tmp").createNewFile();
RandomAccessFile raf=new RandomAccessFile(tbName+".sc","rw");
RandomAccessFile traf=new RandomAccessFile(tbName+".tmp","rw");
raf.seek(0);
while(raf.getFilePointer()!=raf.length())
{
line=raf.readLine();
line=line.toUpperCase();
if(!line.startsWith(colName))
{
traf.writeBytes(line+"\r\n");
}
}
raf.close();
traf.close();
new File(tbName+".sc").delete();
new File(tbName+".tmp").renameTo(new File(tbName+".sc"));第一次调用是成功的

解决方案 »

  1.   

    public static void main(String[] args)throws Exception
        {
            File src = new File("G:\\elec\\a.txt");
            File dst = new File("G:\\elec\\b.txt");
            if(!dst.exists() && !dst.createNewFile())
            {
                System.out.println("创建文件失败:"+dst.getAbsolutePath());
                return;
            }
            copy(src,dst);
            if(!src.delete())
            {
                System.out.println("删除文件失败:"+src.getAbsolutePath());
                return;
            }
            if(!dst.renameTo(src))
            {
                System.out.println("重命名失败");
                return;
            }
        }
        
        static void copy(File src, File dst) throws IOException {
            InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dst);        byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        }
      

  2.   

    上面程序已经过测试。
    另外,
            if(!dst.exists() && !dst.createNewFile())
            {
                System.out.println("创建文件失败:"+dst.getAbsolutePath());
                return;
            }
    这部分可以省略。
    copy函数中,如果dst不存在,会自动创建,只是看你的要求需要这个步骤,
    所以加上了。
      

  3.   

    String line="";
     File srcf=new File(tbName+".sc");
     File dstf=new File(tbName+".tmp");
     dstf.createNewFile();
     RandomAccessFile raf=new RandomAccessFile(tbName+".sc","rw");
     RandomAccessFile traf=new RandomAccessFile(tbName+".tmp","rw");
     raf.seek(0);
     while(raf.getFilePointer()!=raf.length())
     {
      line=raf.readLine();
      line=line.toUpperCase();
      if(!line.startsWith(colName))
      {
      traf.writeBytes(line+"\r\n") ;

      }

     }
     raf.close();
     traf.close();
     if(srcf.exists())
     {
      srcf.delete();
     }
     if(dstf.exists())
     {
      dstf.renameTo(srcf);
     }改成这个样了,还是不行!急死人了
      

  4.   

    我用你的方法就过不去,说删除文件失败,建立tmp文件可以
      

  5.   

    奇怪,我用上面的方法测试可以的建议把close放到finally去,不管有没有问题,这样子才是安全的
      

  6.   

    String line="";
    File srcf=new File(tbName+".sc");
    File dstf=new File(tbName+".tmp");
    dstf.createNewFile();
    RandomAccessFile raf=new RandomAccessFile(tbName+".sc","rw");
    RandomAccessFile traf=new RandomAccessFile(tbName+".tmp","rw");
    raf.seek(0);
    while(raf.getFilePointer()!=raf.length())
    {
    line=raf.readLine();
    line=line.toUpperCase();
    if(!line.startsWith(colName))
    {
    traf.writeBytes("\r\n"+line);}}
    raf.close();
    traf.close();File src=new File(tbName+".sc");
    if(!src.exists())
    {
    System.out.println("delete failed");
    }
    else
    {
    src.delete();
    if(src.exists())
    {
    System.out.println("file exists");
    }
    else
    {
    dstf.renameTo(src);
    }
    }以上是删除部分的程序
    以下是结果>display *
    TABLE TOBY
    ----------
    SEX INT
    FACE STRING
    TABLE XXC
    ----------
    SSN INT
    ID INT
    NAME STRING
    MEMO STRING
    >remove id from xxc
    Are you sure you want to REMOVE this Column?(YES or NO)yes
    file exists
    >quit
    Thank you for using MyDatabase.删除.sc文件失败因此提示文件存在,但是.tmp文件可以建立,但是因为.sc依然存在所以不能改名