MyException - 我的异常网
当前位置:我的异常网» J2SE » 两天无法解决的有关问题!java文件重命名有关问题!

两天无法解决的有关问题!java文件重命名有关问题!为什么文件无法重命名?多谢哈……

www.MyException.Cn  网友分享于:2013-12-14  浏览:22次
两天无法解决的问题!java文件重命名问题!为什么文件无法重命名?谢谢哈……
[code=Java][/code]

import java.io.*;
//字节流
class ZiJie{

String file2 = "HelloB.txt";
void zijie(){
try{
BufferedOutputStream out2 = new BufferedOutputStream(new FileOutputStream(file2));
System.out.println("以字节流自动保存到c:\\HelloB.txt中!");
BufferedInputStream in2 = new BufferedInputStream(System.in);
int b = in2.read();
out2.write(b);
out2.close();
}catch (Exception e) {
// TODO: handle exception
}
}
}


import java.io.*;

//字符流
class ZiFu {

String file1 = "HelloA.txt";

void zifu(){
try{
BufferedWriter out1 = new BufferedWriter(new FileWriter(file1));
System.out.println("请输入字符串改字符串自动保存到c:\\HelloA.txt中!");
BufferedReader in1 = new BufferedReader(new InputStreamReader(System.in));
String a = in1.readLine();
out1.write(a);
out1.close();
}catch (Exception e) {
// TODO: handle exception
}
}
}


import java.io.*;
import java.text.*;

class HeBing {

String file3 = "Hello.txt";
String file4 = "newHello.txt";

ZiFu zf = new ZiFu();
ZiJie zj = new ZiJie();

void hebing(){
try{//合并上面两文件并显示
BufferedWriter out3 = new BufferedWriter(new FileWriter(file3));
System.out.println("字节流形式合并HelloA.txt和HelloB.txt到c:\\HelloB.txt中!");
InputStream k1 = new FileInputStream(new File(zf.file1));
InputStream k2 = new FileInputStream(new File(zj.file2));
SequenceInputStream in4 = new SequenceInputStream(k1,k2);
int c = 0;
while((c=in4.read())!=-1){
out3.write(c);
}
out3.close();
InputStream k3 = new FileInputStream(new File(file3));
int d = 0; 
while((d=k3.read())!=-1){
System.out.println(d);
}
[color=#FF0000][/color][b][/b] 问题出在这里!!!重命名始终不成功都两天了……哎……
//改名字
File file5 = new File(file4);
File file6 = new File(file3);
if(file5.exists()){
file5.delete();
}else {
boolean x = file5.renameTo(file5);
System.out.println(x);
}

//打印文件属性
if(file5.exists()){
System.out.println(file5.getAbsolutePath());
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file5.lastModified()));
System.out.println(file5.length());  
System.out.println(file5.canRead());  
System.out.println(file5.canWrite());  
}

}catch (Exception e) {
// TODO: handle exception
}
}
}
//主函数
class IoTest{
public static void main(String[] agrs) {

ZiFu zf = new ZiFu();
ZiJie zj = new ZiJie();
HeBing hb = new HeBing();

zf.zifu();
zj.zijie();
hb.hebing();
}
}


------解决方案--------------------
你这什么逻辑?存在的时候删除,不存在的时候改名?
文件不存在你改什么?
Java code
if(file5.exists()){
file5.delete();
}else {
boolean x = file5.renameTo(file5);
System.out.println(x);
}

------解决方案--------------------
探讨
改过来了还是提示false 代码和结果如下:
//改名字
File file5 = new File(file4);
File file6 = new File(file3);
if(file5.exists()){
file5.delete();
}else {
boolean x = file6.renameTo(file5);
System.out.println(x);
……

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有