renameTo为什么用不了?请进!

iaroyiaroy 2002-01-17 04:34:28
try{
File pathfile = new File(strRealPathfile);
if (pathfile.exists())
{
String[] strallfile=pathfile.list();
for(int i=0;i<strallfile.length;i++){
if(strallfile[i].equals(strnewfile)){
m_iErrTag=-1;
}
}
pathfile.renameTo(new File (pathfile.getParent (), strnewfile));
m_iErrTag=0;
}else {
m_iErrTag=-1;
}
}catch (SecurityException ex){
ex.printStackTrace();
throw new GeneralException("CommonBean:renameFile() SecurityException:"+ex.getMessage());
}catch (Exception ex) {
ex.printStackTrace();
throw new GeneralException("CommonBean:renameFile() Exception:"+ex.getMessage());
}

一运行就抛出异常CommonBean:renameFile() Exception:null
而我查看了一些资料后,好像不少人都遇到renameTo不好使,是不是和操作系统有关?

...全文
166 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyyoung 2002-01-18
  • 打赏
  • 举报
回复
If you're using 1.1.3 version of JavaMail you can use this method of the Folder class:
boolean renameTo(Folder f) throws MessagingException
You have to create a new Folder object with the desired new name and then apply this method to the Folder you want to rename.
If something goes wrong (wrong name, not existing folder, already existing folder...) it returns FALSE.

You must take care of two things:
The folder you want to rename must be closed.
This works only for IMAP protocol (or others similar), if you call this method in a POP3 environment you obtain a MethodNotSupportedException.
xmvigour 2002-01-18
  • 打赏
  • 举报
回复
/*根据下面的程序片段自己修改,实现的功能,相当于在同一目录下,把hello.txt复制为areyouchange.txt*/


File f=new File("hello.txt");
String s1="areyouchange.txt";

try{
//URL path=f.toURL();
String path1=f.getCanonicalPath();
String path2=f.getName();
String pathname=path1.substring(0,path1.indexOf(path2))+s1;
System.out.println(pathname+" "+path2+" "+path1);
File file1 = new File(pathname);
//file1.createNewFile();
try
{
/*FileOutputStream fileOut = new FileOutputStream(file);
fileOut.write(m_parent.m_binArray, m_startData, m_size);
fileOut.close();*/
try {
BufferedInputStream in =
new BufferedInputStream(
new FileInputStream(f));
// Produces deprecation message:
PrintStream out =
new PrintStream(
new BufferedOutputStream(
new FileOutputStream(file1)));
System.setIn(in);
System.setOut(out);
System.setErr(out);

BufferedReader br =
new BufferedReader(
new InputStreamReader(System.in));
String s;
while((s = br.readLine()) != null)
System.out.println(s);
out.close(); // Remember this!
} catch(IOException e) {
e.printStackTrace();
}

}
catch(Exception e)
{
throw new IllegalArgumentException("Path's name is invalid or does not exist (1135).");
}
}catch(Exception e){}
iaroyiaroy 2002-01-18
  • 打赏
  • 举报
回复
不好使啊!!!!!
我是用jsp调用这个函数,别的如读取啊,删除的都可以用,但就这个renameTo有问题,who能解决呢?我可以再加分
xmvigour 2002-01-17
  • 打赏
  • 举报
回复
pathfile.renameTo(new File (pathfile.getParent (), strnewfile));
改为:
File f1=new File (pathfile.getParent (), strnewfile);
pathfile.renameTo(f1);
pathfile=f1;

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧