package meify.other;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.regex.Pattern;
/**
* @description 文件各種操作
* 相關函數
*(boolean) mkdir() 創建此抽象路徑名指定的目錄
*(boolean) mkdirs() 創建此抽象路徑名指定的目錄,包括所有必需但不存在的父目錄。
*(boolean) delete() 刪除此抽象路徑名錶示的文件或目錄
*(boolean) createNewFile() 當不存在此路徑名指定名稱的文件時,創建一個新的空文件。
* @version 1.0
* @author meify 2013-7-24 上午9:53:35
*/
public class FileOperator {
/**
*
* @param filePath
* @version 1.0
* @description 創建一個文件
* @author meify 2013-7-24 上午9:58:47
*/
public static void createFile(String filePath){
File file=new File(filePath);
try {
if(!file.exists()){
file.createNewFile();
}else{
file.delete();
file.createNewFile();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @param path
* @version 1.0
* @description 創建一個文件夾
* @author meify 2013-7-24 上午9:59:48
*/
public static void creatFileDir(String path){
//注意:File文件類不僅僅指文件,也包含文件目錄的意思
File file=new File(path);
if(!file.exists()){
file.mkdirs();
}else{
file.delete();
file.mkdirs();
}
}
/**
*
* @param str
* @param filePath
* @version 1.0
* @description 使用 FileWriter類將字符串str寫入到路徑爲filePath的文件中去
* @author meify 2013-7-24 上午10:07:24
*/
public static void writeIntoFileByFileWriter(String str,String filePath){
File file=new File(filePath);
try {
Writer fwriter=new FileWriter(file);
fwriter.write(str);
fwriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("將"+str+"寫入到"+filePath+"文件中失敗");
}
}
/**
*
* @param str
* @param filePath
* @version 1.0
* @description 使用BufferedWriter 將字符串寫入到文件中
* @author meify 2013-7-24 上午10:16:38
*/
public static void writeIntoFileByBufferedWriter(String str,String filePath){
try {
BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(new File(filePath)));
bufferedWriter.write(str);
bufferedWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("將"+str+"寫入到"+filePath+"文件中失敗");
}
}
/**
*
* @param filePath 文件路徑
* @return
* @version 1.0
* @description 使用FileReader讀取文件中的內容
* @author meify 2013-7-24 上午10:26:12
*/
public static String readFromFileByFileReader(String filePath){
String str="";
try {
Reader reader=new FileReader(filePath);
char[] arrs=new char[1024*10];
try {
int len=reader.read(arrs);
str= new String(arrs,0,len);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("讀取文件內容失敗");
}
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("reader關閉失敗");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("找不到文件");
}
return str;
}
/**
*
* @param filePath 文件路徑
* @return
* @version 1.0
* @description 使用FileInputStream來讀取文件中內容
* @author meify 2013-7-24 上午10:28:24
*/
public static String readFromFileByFileInputStream(String filePath){
String str="";
try {
InputStream inputStream=new FileInputStream(new File(filePath));
byte arrs[]=new byte[1024*10];
try {
int len=inputStream.read(arrs);
str=new String(arrs,0,len);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("讀取文件內容失敗");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("找不到文件");
}
return str;
}
/**
*
* @param filePath 問價路徑
* @return
* @version 1.0
* @description 使用BufferedReader讀取文件中的信息
* @author meify 2013-7-24 上午10:38:15
*/
public static String readFromFileByBufferedReader(String filePath){
StringBuffer sb=new StringBuffer();
try {
BufferedReader br=new BufferedReader(new FileReader(new File(filePath)));
String str=null;
while((str=br.readLine())!=null){
sb.append(str);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("出異常啦");
}
return sb.toString();
}
/**
*
* @param srcFilePath 源文件路徑
* @param destFilePath 目標文件路徑
* @version 1.0
* @description 字符流賦值文件 將srcFile中的內容複製到destFile中去
* @author meify 2013-7-24 上午10:40:22
*/
public static void CopyFileByReaderAndWriter(String srcFilePath,String destFilePath){
try {
FileReader fileReader=new FileReader(new File(srcFilePath));
FileWriter fileWriter=new FileWriter(new File(destFilePath));
int len=fileReader.read(); //讀取單個字符 並返回讀取的字符 (字母--->數字對應)
System.out.println(len);
while(len!=-1){
fileWriter.write(len);
len=fileReader.read(); //Reads a single character.@return The character read, or -1 if the end of the stream has been reached
}
fileReader.close();
fileWriter.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @param srcFilePath 源文件路徑
* @param destFilePath 目標文件路徑
* @version 1.0
* @description 使用字節流 將srcFile中的內容複製到destFile中去
* @author meify 2013-7-24 上午11:00:07
*/
public static void copyFileByStream(String srcFilePath,String destFilePath){
try {
InputStream inputStream=new FileInputStream(new File(srcFilePath));
FileOutputStream outPutStream=new FileOutputStream (new File(destFilePath));
int len=inputStream.read(); //讀取單個字符 並返回讀取的字符 (字母--->對應的數字)
System.out.println(len);
while(len!=-1){
outPutStream.write(len);
len=inputStream.read();
}
inputStream.close();
outPutStream.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @param srcFilePath 源文件路徑
* @param destFilePath 目標文件路徑
* @version 1.0
* @description 使用BufferedReader 和BufferedWriter將srcFile中的內容複製到destFile中去
* @author meify 2013-7-24 上午11:06:52
*/
public static void copyFileByBufferedReaderAndWriter(String srcFilePath,String destFilePath){
BufferedReader bufferedReader=null;;
BufferedWriter bufferedWriter=null;
try {
bufferedReader=new BufferedReader(new FileReader(new File(srcFilePath)));
bufferedWriter=new BufferedWriter(new FileWriter(new File(destFilePath)));
String line=null;
while((line=bufferedReader.readLine())!=null){
bufferedWriter.write(line);
line=bufferedReader.readLine();
}
bufferedReader.close();
bufferedWriter.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @param dirPath
* @version 1.0
* @description 顯示某目錄下的所有文件名稱
* @author meify 2013-7-24 上午11:33:05
*/
public static void showFilesOfDir(String dirPath){
File fileDir=new File(dirPath);
if(fileDir.isDirectory()){
File[] files=fileDir.listFiles();
System.out.println(dirPath+"目錄下文件個數爲"+files.length);
for(File file:files){
System.out.println(file.getName());
}
}
}
/**
*
* @param dirPath
* @version 1.0
* @description 獲取dirPath目錄下拓展名爲txt的文件列表
* @author meify 2013-7-24 上午11:36:39
*/
public void showFileOfExcelFromDir(String dirPath){
File fileDir=new File(dirPath);
if(fileDir.isDirectory()){
String[] fileNames=fileDir.list(new DirFilter("*txt"));
System.out.println(dirPath+"目錄下文件個數爲"+fileNames.length);
for(String fileName:fileNames){
System.out.println(fileName);
}
}
}
class DirFilter implements FilenameFilter{
private Pattern pattern;
public DirFilter(String regex){
pattern=Pattern.compile(regex);
}
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
}
public static void main(String[] args) {
// createFile("D://test01.txt");
// creatFileDir("D://test");
// writeIntoFileByFileWriter("hello my name is meify","D://test01.txt");
// String str=readFromFileByFileReader("D://test01.txt");
// String str=readFromFileByFileInputStream("D://test01.txt");
// String str=readFromFileByBufferedReader("D://test01.txt");
// System.out.println(str);
// copyFileByStream("D://test01.txt","D://test02.txt");
// copyFileByBufferedReaderAndWriter("D://test01.txt","D://test02.txt");
// showFilesOfDir("D:");
new FileOperator().showFileOfExcelFromDir("D:");
}
}
java中關於File類的相關操作
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.