留下备忘!
文件拷贝:
Java代码
public static void copyFile(File src,File dest) throws Exception{
try {
// Create channel on the source
FileChannel srcChannel = new FileInputStream(src).getChannel();
// Create channel on the destination
FileChannel dstChannel = new FileOutputStream(dest).getChannel();
// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
// Close the channels
srcChannel.close();
dstChannel.close();
} catch (IOException e) {
}
}
文件重命名:
Java代码
public static renameFile(File src,String newFilename)throws Exception{
src.renameTo(new File(newFilename));//请写明完整路径
}
将文件读成字符串:
Java代码
public static String ReadFileToString(String pathAndFilename) {
StringBuffer str = new StringBuffer();
BufferedReader in = null;
File inputFile = null;
try {
inputFile = new File(pathAndFilename);
in = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), "utf-8"));
String line = null;
str = new StringBuffer((int) inputFile.length());
while ((line = in.readLine()) != null) {
str.append(line);
}
in.close();
}
catch (FileNotFoundException e2) {
try {
if (!new File(inputFile.getParent()).exists())
new File(inputFile.getParent()).mkdirs();
inputFile.createNewFile();
}
catch (IOException e) {
e.printStackTrace();
}
}
catch (IOException e3) {
e3.printStackTrace();
}
return str.toString();
}
回复 更多评论