作者:sz_bdqn原文链接http://blog.csdn.net/sz_bdqn/article/details/5449375
例子1
- import java.io.*;
- public class test1
- {
- public static void main(String[] args) {
- File file=new File("D:/gai.jpg"); //指定文件名及路径
- String name="123";
- String filename=file.getAbsolutePath();
- if(filename.indexOf(".")>=0)
- {
- filename = filename.substring(0,filename.lastIndexOf("."));
- }
- file.renameTo(new File(name+".jpg")); //改名
- }
- }
- public class xfile {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO 自动生成方法存根
- File f=new File("d:/old.txt");
- String c=f.getParent();
- File mm=new File(c+File.pathSeparator+"new.txt");
- if(f.renameTo(mm))
- {
- System.out.println("修改成功!");
- }
- else
- {
- System.out.println("修改失败");
- }
- }
- }
- file.renameTo(new File(name+".jpg")); //改名
改成:
- <br>file.renameTo(new File(file.getPath().substring(0,file.indexOf(file.getName()))));<br>
就没问题了。总之,谢谢你的例子,给我提供了思路。
-
Re:
yan_calculate 2011-10-11 22:57发表
-
- 回复Clowind:没有删,放到工程目录去了