写了一个方法,操作两个文件,每次运行,建立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"));第一次调用是成功的
为什么第一次执行成功,第二次执行就不能删除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"));第一次调用是成功的
解决方案 »
- 求和为一个数的所有可能,比如5=1+1+1+1+1=2+3.....
- java解析数据包(不知道发对区没有)
- 使用dom4j向XML文件增加节点问题
- 我的第一个struts程序,遇到无比巨大的困难,恳求高手指点一二。
- 怎么能让文字显示在文本框的中间,而不是从文本框的顶头开始显示???
- rrdw:如何访问声明在别的包里的default的类?
- 关于javax.swing.undo的问题
- 请问在使用InstallAnyWhere制作安装程序时,如何添加自定义的对话框
- 我是java的初学者,谁能告诉我什么书最好,谢谢
- 一个简单问题,来抢100分,来者有分.
- Swing的简单问题,请大家帮我看看
- 这个能用算法实现吗?
{
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();
}
另外,
if(!dst.exists() && !dst.createNewFile())
{
System.out.println("创建文件失败:"+dst.getAbsolutePath());
return;
}
这部分可以省略。
copy函数中,如果dst不存在,会自动创建,只是看你的要求需要这个步骤,
所以加上了。
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);
}改成这个样了,还是不行!急死人了
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依然存在所以不能改名