java中对文件怎么重命名
我先创建一个a.txt文件,然后在把a.txt删除之前,copy给了b.txt,,再删除a.txt之后,,又想把b.txt改名成a.txt。。我用renameto()..实现不了。。求给我帮助一下。。
package ys.TestDemo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
public class TestFiles {
public static void main(String[] args) {
File file=new File("d:aa//bb//c.txt");
file.getParentFile().mkdirs();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
PrintStream ps=new PrintStream(file);
ps.println("你在干嘛");
ps.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String s=file.getAbsolutePath();
System.out.println("ssss"+s);
File file1=new File("d:aa//bb//b.txt");
if(file.exists()){
Copy( file, file1);
file.deleteOnExit();
}
File file2=new File(s);
file1.renameTo(file2);
System.out.println("......"+file1.getAbsolutePath());
}
private static void Copy(File file, File file2) {
try {
BufferedReader buf=
new BufferedReader(
new InputStreamReader(
new FileInputStream(file)));
//BufferedReader和Ps连用(70%)
PrintStream ps=new PrintStream(file2);
String str=null;
while((str=buf.readLine())!=null){
ps.println(str);
}
ps.close();
buf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
[解决办法]
你需要把file.deleteOnExit();换成file.delete();方法实现删除,因为这里的删除如果没有删除干净的话,重命名是不成功的。你可以试试。
[解决办法]
String s=file.getAbsolutePath();//
System.out.println("ssss"+s);
File file1=new File("d:aa//bb//b.txt");
if(file.exists()){
Copy( file, file1);
file.deleteOnExit();// 这个方法是在虚拟机终止时,请求删除此抽象路径名表示的文件或目录。你觉得此时会删除么?
//这里添加一句,然后看结果,如果最后结果报异常,把异常发上来
System.out.println(file.exists()):
}
File file2=new File(s);
file1.renameTo(file2);
[解决办法]
package com.usctinfo.star;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import org.junit.Test;
public class Demo1 {
@Test
public void CalendarTest(){
//String reg = "query@@yyyyMMdd@@";
//String temp = "@@yyyyMMdd@@";
//Calendar cal = Calendar.getInstance();
//Date date = new Date();
//cal.setTime(date);
//System.out.println(cal);
//System.out.println(date);
//String info = "+1";
//info = "-1";
//if(info.indexOf("+")!=-1)
//info = info.substring(1);
//cal.add(Calendar.YEAR, Integer.parseInt(info));
//System.out.println(cal);
//String s = new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
//System.out.println(s);
//reg = reg.replaceFirst(temp,new SimpleDateFormat("yyyyMMdd").format(cal.getTime()));
//cal.add(Calendar.HOUR,2);
//String date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cal.getTime());
//System.out.println(reg);
//System.out.println(date1);
/**
* 把字符串改成时间格式
*/
//String time = "2013-7-31 9:12:23";
//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//try {
//Date date = sdf.parse(time);
//String dateStr = sdf.format(date);
//System.out.println(date);
//System.out.println(dateStr);
//} catch (ParseException e) {
//// TODO Auto-generated catch block
//e.printStackTrace();
//}
/**
* 创建一个文件a.txt 在复制给b.txt 在删除一个a.txt 把b.txt重命名为a.txt
* 注意带有缓存的输出字符流 需要注意flush和关闭流 不然又可能写入的内容缺失
*/
//File aFile = new File("D:"+File.separator+"orzdbb"+File.separator+"demoFile");
//try {
//FileOutputStream fos = new FileOutputStream(aFile);
//String s="dubingbing";
//byte[] bytes = s.getBytes("UTF-8");
//fos.write(bytes);
//fos.close();
//System.out.println(aFile.length());
//} catch (Exception e) {
//// TODO Auto-generated catch block
//e.printStackTrace();
//}
File file1 = new File("d:\\orzdbb\\ab.txt");
try {
file1.createNewFile();
PrintStream print = new PrintStream(file1);
print.println("dubingbing");
print.println("wangzhi");
print.flush();
print.close();
String parentPath = file1.getParent();
File file2 = new File(parentPath+File.separator+"b.txt");
file2.createNewFile();
if(file1.exists()){
copy(file1,file2);
file1.delete();
file2.renameTo(file1);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void copy(File sourceFile,File taskFile){
FileInputStream fis;
try {
fis = new FileInputStream(sourceFile);
InputStreamReader isd = new InputStreamReader(fis);
BufferedReader bs = new BufferedReader(isd);
PrintStream ps = new PrintStream(taskFile);
String content = null;
if(bs.ready()){
while((content=bs.readLine())!=null){
ps.println(content);
}
}
//ps.flush();
bs.close();
isd.close();
fis.close();
ps.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
主要的还是 file.delete() 与方法 file.deleteOnExit()的区别