1个回答
展开全部
import java.io.File;
import java.io.RandomAccessFile;
/**
* June 18 2015
*
* @author HP TODO 修改文件夹里面的文件的後缀名
*
*/
public class Exercise1 {
private static String dir = "D://12052010/Jsp/mysite/week6/WebContent/week6/";
/**
* 获取folderDir文件夹下面的所有文件
*
* @param folderDir
* 本地磁盤文件夹路径
* @return
*/
public static String[] getAllFile(String folderDir) {
File fileDir = new File(folderDir);
return fileDir.list();
}
/**
* 修改文件夹下面的所有文件的後缀名
*
* @param saveDir
* 改变後缀名之後文件保存路径
* @param fileName
* 需要修改的文件名
* @param subName
* 新的後缀名
* @return
*/
public static boolean changeSubName(String saveDir, String fileName,
String subName) {
try {
String[] str = fileName.split("\\.");
String saveName = str[0] + "." + subName;
RandomAccessFile read = new RandomAccessFile(dir + fileName, "rw");
RandomAccessFile writer = new RandomAccessFile(saveDir + "/"
+ saveName, "rw");
byte[] b = new byte[1024];
int len = 0;
while (-1 != (len = read.read(b))) {
writer.write(b, 0, len);
}
writer.close();
read.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static void main(String[] args) {
for (String name : getAllFile(dir)) {
if (!changeSubName(dir, name, "txt"))
System.out.println(name + " 修改後缀名不成功");
}
System.out.println("程序结束");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询