基于java file 文件操作operate file of java的應(yīng)用
更新時間:2013年05月02日 18:06:32 作者:
本篇文章介紹了,基于java file 文件操作operate file of java的應(yīng)用。需要的朋友參考下
java文件操作
package com.b510;
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 Hongten</br>
*
* 文件的操作
*
*
*
*/
public class OperateFiles {
/**
* @param args
*/
public static void main(String[] args) {
OperateFiles operateFiles = new OperateFiles();
//新建一個文件夾
operateFiles.newFolder("c:/hongten");
//新建一個文件,同時向里面寫入內(nèi)容
operateFiles.newFile("c:/hongten/Hello.txt", "hello,i'm Hongten.你好");
//刪除一個文件
operateFiles.deleteFile("c:/hongten/Hello.txt");
//刪除一個文件夾
operateFiles.deleteFolder("c:/hongten");
//復(fù)制文件夾
operateFiles.copyFolder("c:/hongten", "e:/hongten");
//提取文件的擴(kuò)展名
String expandedName=operateFiles.getExpandedName("c:/hongten/Hello.txt");
System.out.println(expandedName);
//提取文件的路徑
System.out.println(operateFiles.getFilePath("c:/hongten/Hello.txt"));
}
/**
* 獲得文件的擴(kuò)展名
* @param filePath 文件的路徑 如:c:/hongten/Hello.txt
* @return 文件的擴(kuò)展名 如: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("新文件創(chuàng)建失敗");
e.printStackTrace();
}
}
/**
* 新建一個文件,同時向文件中寫入內(nèi)容
*
* @param filePath
* 新建文件的目錄 如:c:\\hongten.java
* @param fileContent
* 向文件中寫入的內(nèi)容
*/
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]);// 再刪除空文件夾
}
}
}
/**
* 單個文件的復(fù)制
*
* @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; // 字節(jié)數(shù) 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
System.out.println("復(fù)制單個文件操作出錯");
e.printStackTrace();
}
}
/**
* 文件夾的復(fù)制
*
* @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("復(fù)制整個文件夾內(nèi)容操作出錯");
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);
}
/**
* 獲得系統(tǒng)根目錄絕對路徑
*
* @return
*/
public String getPath() {
String sysPath = this.getClass().getResource("/").getPath();
// 對路徑進(jìn)行修改
sysPath = sysPath.substring(1, sysPath.length() - 16);
return sysPath;
}
}
現(xiàn)在有時間把這些東西整理出來,給大家分享一下……
復(fù)制代碼 代碼如下:
package com.b510;
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 Hongten</br>
*
* 文件的操作
*
*
*
*/
public class OperateFiles {
/**
* @param args
*/
public static void main(String[] args) {
OperateFiles operateFiles = new OperateFiles();
//新建一個文件夾
operateFiles.newFolder("c:/hongten");
//新建一個文件,同時向里面寫入內(nèi)容
operateFiles.newFile("c:/hongten/Hello.txt", "hello,i'm Hongten.你好");
//刪除一個文件
operateFiles.deleteFile("c:/hongten/Hello.txt");
//刪除一個文件夾
operateFiles.deleteFolder("c:/hongten");
//復(fù)制文件夾
operateFiles.copyFolder("c:/hongten", "e:/hongten");
//提取文件的擴(kuò)展名
String expandedName=operateFiles.getExpandedName("c:/hongten/Hello.txt");
System.out.println(expandedName);
//提取文件的路徑
System.out.println(operateFiles.getFilePath("c:/hongten/Hello.txt"));
}
/**
* 獲得文件的擴(kuò)展名
* @param filePath 文件的路徑 如:c:/hongten/Hello.txt
* @return 文件的擴(kuò)展名 如: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("新文件創(chuàng)建失敗");
e.printStackTrace();
}
}
/**
* 新建一個文件,同時向文件中寫入內(nèi)容
*
* @param filePath
* 新建文件的目錄 如:c:\\hongten.java
* @param fileContent
* 向文件中寫入的內(nèi)容
*/
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]);// 再刪除空文件夾
}
}
}
/**
* 單個文件的復(fù)制
*
* @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; // 字節(jié)數(shù) 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
System.out.println("復(fù)制單個文件操作出錯");
e.printStackTrace();
}
}
/**
* 文件夾的復(fù)制
*
* @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("復(fù)制整個文件夾內(nèi)容操作出錯");
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);
}
/**
* 獲得系統(tǒng)根目錄絕對路徑
*
* @return
*/
public String getPath() {
String sysPath = this.getClass().getResource("/").getPath();
// 對路徑進(jìn)行修改
sysPath = sysPath.substring(1, sysPath.length() - 16);
return sysPath;
}
}
現(xiàn)在有時間把這些東西整理出來,給大家分享一下……
您可能感興趣的文章:
- java中File類的使用方法
- java文件操作工具類分享(file文件工具類)
- java 中InputStream,String,File之間的相互轉(zhuǎn)化對比
- 詳解Java中的File文件類以及FileDescriptor文件描述類
- 詳談java中File類getPath()、getAbsolutePath()、getCanonical的區(qū)別
- java File類的基本使用方法總結(jié)
- 基于java Files類和Paths類的用法(詳解)
- java文件操作之Path,Paths,Files
- Java File類的常用方法總結(jié)
- Java中File文件操作類的基礎(chǔ)用法
相關(guān)文章
Java使用JDBC實(shí)現(xiàn)Oracle用戶認(rèn)證的方法詳解
這篇文章主要介紹了Java使用JDBC實(shí)現(xiàn)Oracle用戶認(rèn)證的方法,結(jié)合實(shí)例形式分析了java使用jdbc實(shí)現(xiàn)數(shù)據(jù)庫連接、建表、添加用戶、用戶認(rèn)證等操作流程與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08Java OpenCV4.0.0實(shí)現(xiàn)實(shí)時人臉識別
這篇文章主要為大家詳細(xì)介紹了Java OpenCV4.0.0實(shí)現(xiàn)實(shí)時人臉識別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07聊聊@RequestMapping和@GetMapping @PostMapping的區(qū)別
這篇文章主要介紹了@RequestMapping和@GetMapping及@PostMapping的區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08Java設(shè)計(jì)模式中代理模式應(yīng)用詳解
代理模式(Proxy Parttern)為一個對象提供一個替身,來控制這個對象的訪問,即通過代理對象來訪問目標(biāo)對象。本文將通過示例詳細(xì)講解一下這個模式,需要的可以參考一下2022-11-11Java中FilterInputStream和FilterOutputStream的用法詳解
這篇文章主要介紹了Java中FilterInputStream和FilterOutputStream的用法詳解,這兩個類分別用于封裝輸入和輸出流,需要的朋友可以參考下2016-06-06Java實(shí)現(xiàn)拖拽列表項(xiàng)的排序功能
這篇文章主要介紹了Java實(shí)現(xiàn)拖拽列表項(xiàng)的排序功能,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02