import java.io.File; |
import java.io.FileInputStream; |
import java.io.FileOutputStream; |
import java.io.FileWriter; |
import java.io.InputStream; |
import java.io.PrintWriter; |
/** |
* |
* @author |
* |
* 文件操作类 |
* |
* (新建文件夹、删除文件夹、复制文件夹、移动文件夹、新建文件、删除文件、复制文件、移动文件、获取文件扩展名、获取文件路径) |
* |
*/ |
public class OperateFiles { |
/** |
* @param args |
*/ |
public static void main(String[] args) { |
OperateFiles operateFiles = new OperateFiles(); |
// 新建一个文件夹 |
operateFiles.newFolder( "c:/hongten" ); |
// 新建一个文件,同时向里面写入内容 |
operateFiles.newFile( "c:/hongten/Hello.txt" , "hello,i'm Hongten.你好" ); |
// 删除一个文件 |
operateFiles.deleteFile( "c:/hongten/Hello.txt" ); |
// 删除一个文件夹 |
operateFiles.deleteFolder( "c:/hongten" ); |
// 复制文件夹 |
operateFiles.copyFolder( "c:/hongten" , "e:/hongten" ); |
// 提取文件的扩展名 |
String expandedName = operateFiles |
.getExpandedName( "c:/hongten/Hello.txt" ); |
System.out.println(expandedName); |
// 提取文件的路径 |
System.out.println(operateFiles.getFilePath( "c:/hongten/Hello.txt" )); |
} |
/** |
* 获得文件的扩展名 |
* |
* @param filePath |
* 文件的路径 如:c:/hongten/Hello.txt |
* @return 文件的扩展名 如:txt |
*/ |
public String getExpandedName(String filePath) { |
return filePath.substring(filePath.lastIndexOf( "." ) + 1 ); |
} |
/** |
* 获得文件的路径 |
* |
* @param file |
* 文件的路径 |
* @return 文件的路径 |
*/ |
public String getFilePath(String file) { |
return file.substring( 0 , file.lastIndexOf( "/" )); |
} |
/** |
* 新建一个目录 |
* |
* @param folderPath |
* 新建目录的路径 如:c:\\newFolder |
*/ |
public void newFolder(String folderPath) { |
try { |
File myFolderPath = new File(folderPath.toString()); |
if (!myFolderPath.exists()) { |
myFolderPath.mkdir(); |
} |
} catch (Exception e) { |
System.out.println( "新建目录操作出错" ); |
e.printStackTrace(); |
} |
} |
/** |
* 新建一个文件 |
* |
* @param filePath |
* 新建文件的目录 如:c:\\hongten.java |
*/ |
public void newFile(String filePath) { |
try { |
File myFilePathFile = new File(filePath.toString()); |
if (!myFilePathFile.exists()) { |
myFilePathFile.createNewFile(); |
} |
} catch (Exception e) { |
System.out.println( "新文件创建失败" ); |
e.printStackTrace(); |
} |
} |
/** |
* 新建一个文件,同时向文件中写入内容 |
* |
* @param filePath |
* 新建文件的目录 如:c:\\hongten.java |
* @param fileContent |
* 向文件中写入的内容 |
*/ |
public void newFile(String filePath, String fileContent) { |
try { |
newFile(filePath); |
FileWriter resultFile = new FileWriter(filePath); |
PrintWriter myFile = new PrintWriter(resultFile); |
myFile.println(fileContent); |
resultFile.close(); |
} catch (Exception e) { |
System.out.println( "新建文件操作出错" ); |
e.printStackTrace(); |
} |
} |
/** |
* 删除一个文件 |
* |
* @param filePath |
* 要删除文件的绝对路径 如:c:\\hongten\\Hello.txt |
*/ |
public void deleteFile(String filePath) { |
try { |
File preDelFile = new File(filePath); |
if (preDelFile.exists()) { |
preDelFile.delete(); |
} else { |
System.out.println(filePath + "不存在!" ); |
} |
} catch (Exception e) { |
System.out.println( "删除文件操作出错" ); |
e.printStackTrace(); |
} |
} |
/** |
* 删除一个文件夹 |
* |
* @param folderPath |
* 要删除的文件夹的绝对路径 如:c:\\hongten |
*/ |
public void deleteFolder(String folderPath) { |
try { |
delAllFiles(folderPath); |
File preDelFoder = new File(folderPath); |
if (preDelFoder.exists()) { |
preDelFoder.delete(); |
} else { |
System.out.println(folderPath + "不存在!" ); |
} |
} catch (Exception e) { |
System.out.println( "删除文件操作出错" ); |
e.printStackTrace(); |
} |
} |
/** |
* 删除一个文件夹下的所有文件 |
* |
* @param folderPath |
* 要删除的文件夹的绝对路径 如:c:\\hongten |
*/ |
public void delAllFiles(String folderPath) { |
File file = new File(folderPath); |
if (!file.exists()) { |
return ; |
} |
if (!file.isDirectory()) { |
return ; |
} |
String[] tempList = file.list(); |
File temp = null ; |
for ( int i = 0 ; i < tempList.length; i++) { |
if (folderPath.endsWith(File.separator)) { |
temp = new File(folderPath + tempList[i]); |
} else { |
temp = new File(folderPath + File.separator + tempList[i]); |
} |
if (temp.isFile()) { |
temp.delete(); |
} |
if (temp.isDirectory()) { |
delAllFiles(folderPath + "/" + tempList[i]); // 先删除文件夹里面的文件 |
deleteFolder(folderPath + "/" + tempList[i]); // 再删除空文件夹 |
} |
} |
} |
/** |
* 单个文件的复制 |
* |
* @param oldPath |
* 原路径 如:c:\\Hello.java |
* @param newPath |
* 新路径 如:f:\\Hello.java |
*/ |
public void copyFile(String oldPath, String newPath) { |
try { |
int bytesum = 0 ; |
int byteread = 0 ; |
File oldfile = new File(oldPath); |
if (oldfile.exists()) { // 文件存在时 |
InputStream inStream = new FileInputStream(oldPath); // 读入原文件 |
FileOutputStream fs = new FileOutputStream(newPath); |
byte [] buffer = new byte [ 1444 ]; |
// int length = 0; |
while ((byteread = inStream.read(buffer)) != - 1 ) { |
bytesum += byteread; // 字节数 文件大小 |
fs.write(buffer, 0 , byteread); |
} |
inStream.close(); |
} |
} catch (Exception e) { |
System.out.println( "复制单个文件操作出错" ); |
e.printStackTrace(); |
} |
} |
/** |
* 文件夹的复制 |
* |
* @param oldPath |
* 原文件夹路径 如: c:\\hello |
* @param newPath |
* 新文件夹路径 如: e:\\hello |
*/ |
public void copyFolder(String oldPath, String newPath) { |
try { |
( new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹 |
File a = new File(oldPath); |
String[] file = a.list(); |
File temp = null ; |
for ( int i = 0 ; i < file.length; i++) { |
if (oldPath.endsWith(File.separator)) { |
temp = new File(oldPath + file[i]); |
} else { |
temp = new File(oldPath + File.separator + file[i]); |
} |
if (temp.isFile()) { |
FileInputStream input = new FileInputStream(temp); |
FileOutputStream output = new FileOutputStream(newPath |
+ "/" + (temp.getName()).toString()); |
byte [] b = new byte [ 1024 * 5 ]; |
int len; |
while ((len = input.read(b)) != - 1 ) { |
output.write(b, 0 , len); |
} |
output.flush(); |
output.close(); |
input.close(); |
} |
if (temp.isDirectory()) { // 如果是子文件夹 |
copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]); |
} |
} |
} catch (Exception e) { |
System.out.println( "复制整个文件夹内容操作出错" ); |
e.printStackTrace(); |
} |
} |
/** |
* 移动单个文件 |
* |
* @param oldPath |
* 源文件路径 如:c:\\hello.java |
* @param newPath |
* 新文件路径 如:e:\\hello.java |
*/ |
public void moveFile(String oldPath, String newPath) { |
copyFile(oldPath, newPath); |
deleteFile(oldPath); |
} |
/** |
* 移动文件夹 |
* |
* @param oldPath |
* 原文件夹路径 如:c:\\hongten |
* @param newPath |
* 新文件夹路径 如:e:\\hongten |
*/ |
public void moveFolder(String oldPath, String newPath) { |
copyFolder(oldPath, newPath); |
deleteFolder(oldPath); |
} |
/** |
* 获得系统根目录绝对路径 |
* |
* @return |
*/ |
public String getPath() { |
String sysPath = this .getClass().getResource( "/" ).getPath(); |
// 对路径进行修改 |
sysPath = sysPath.substring( 1 , sysPath.length() - 16 ); |
return sysPath; |
} |
} |
初级程序员
by: 云代码会员 发表于:2015-01-20 20:02:23 顶(0) | 踩(0) 回复
这个肯定用得上、
回复评论