文本文件內容 讀取 寫入 替換 複製

@

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 文本文件工具箱
 * @author LPY
 */
public class FileUtils {
	
	/**
	 * 獲取文件文本內容
	 * @param fileUrl 文件路徑
	 * @return
	 */
	public static String getFileText(String fileUrl) {
		fileUrl = removeBackSlant(fileUrl);	//去除url結尾的所以反斜槓
		if(fileUrl==null || fileUrl.length()==0){
			String message = "文件路徑爲空!!";
			throw new RuntimeException(message);
		}
		
		File file = new File(fileUrl);
		String fileContentString = null;
		if(!file.isFile()){
			String msg = "不是一個標準的文件";
			System.err.println(msg);
			return null;
		}
		
		FileInputStream is = null;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try {
			is = new FileInputStream(file);
			
			byte[] buffers = new byte[512];
			int count = 0;
			while((count = is.read(buffers))>0){
				bos.write(buffers, 0, count);
			}
			fileContentString = new String(bos.toByteArray(),"UTF-8");
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(is != null){
				try {
					is.close();
					is = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(bos != null){
				try {
					bos.close();
					bos = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return fileContentString;
	}
	
	/**
	 * 創建文本文件
	 * @param text
	 * @param catalogUrl
	 */
	public static void text2File(String text,String catalogUrl){
		catalogUrl = removeBackSlant(catalogUrl);	//去除url結尾的所以反斜槓
		if(catalogUrl==null || catalogUrl.length()==0){
			String message = "文件路徑爲空!!";
			throw new RuntimeException(message);
		}
		File file = new File(catalogUrl);
		if(!file.exists()){
			try {
				file.createNewFile();
				if(!file.exists()){
					String msg = "不是一個標準的文件";
					System.err.println(msg);
					throw new RuntimeException(msg);
				}
			} catch (IOException e) {
				String msg = "創建文件失敗!";
				System.err.println(msg);
				e.printStackTrace();
			}
		}

		FileWriter fw = null;
		BufferedWriter out = null;
		try {
			fw = new FileWriter(file);
			out = new BufferedWriter(fw);
			out.write(text, 0, text.length());
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(fw != null){
				try {
					fw.close();
					fw = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(out != null){
				try {
					out.close();
					out = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 對文件文本內容選擇替換
	 * @param fileUrl 文件路徑
	 * @param oldChar 要替換的內容數組
	 * @param newChar 替換的新內容數組
	 */
	public static void replaceFileText(String fileUrl, String[] oldChars, String[] newChars) {
		fileUrl = removeBackSlant(fileUrl);	//去除url結尾的所以反斜槓
		if(fileUrl==null || fileUrl.length()==0){
			String message = "文件路徑爲空!!";
			throw new RuntimeException(message);
		}
		if(oldChars==null){
			String message = "要替換的內容數組爲null";
			throw new RuntimeException(message);
		}
		if(newChars==null){
			String message = "替換的新內容數組爲null";
			throw new RuntimeException(message);
		}
		if(oldChars.length!=newChars.length){
			String message = "舊內容數組與新內容數組長度不一致";
			throw new RuntimeException(message);
		}
		String fileContentString = getFileText(fileUrl);	//得到文件文本內容
		for(int i=0;i<oldChars.length;i++){
			String oldChar = oldChars[i];
			String newChar = newChars[i];
			fileContentString = fileContentString.replace(oldChar, newChar);
		}
		text2File(fileContentString,fileUrl);
		
	}
	
	/**
	 * 提取文本文件內容並可替換內容後到新文件
	 * @param fileUrl
	 * @param catalogUrl
	 * @param oldChars
	 * @param newChars
	 */
	public static void fileText2NewFile(String fileUrl,String catalogUrl , String[] oldChars, String[] newChars){
		fileUrl   =  removeBackSlant(fileUrl);	//去除url結尾的所以反斜槓
		catalogUrl = removeBackSlant(catalogUrl);
		if(fileUrl==null || fileUrl.length()==0){
			String message = "文件路徑爲空!!";
			throw new RuntimeException(message);
		}
		if(catalogUrl==null || catalogUrl.length()==0){
			String message = "文件路徑爲空!!";
			throw new RuntimeException(message);
		}
		if(oldChars==null){
			String message = "要替換的內容數組爲null";
			throw new RuntimeException(message);
		}
		if(newChars==null){
			String message = "替換的新內容數組爲null";
			throw new RuntimeException(message);
		}
		if(oldChars.length!=newChars.length){
			String message = "舊內容數組與新內容數組長度不一致";
			throw new RuntimeException(message);
		}
		String fileContentString = getFileText(fileUrl);	//得到文件文本內容
		for(int i=0;i<oldChars.length;i++){
			String oldChar = oldChars[i];
			String newChar = newChars[i];
			fileContentString = fileContentString.replace(oldChar, newChar);
		}
		text2File(fileContentString,catalogUrl);
	}
	
	/**
	 * 去除字符串後的反斜槓"\"
	 * 會將正斜槓換爲兩個反斜槓,並且去除結尾的反斜槓"\"
	 * @param url
	 * @return
	 */
	public static String removeBackSlant(String url){
		String _url = url.replaceAll("/", "\\");
		boolean endsWith = _url.endsWith("\\");
		if(endsWith){
			int lastIndexOf = _url.lastIndexOf("\\");
			String _new = _url.substring(0, lastIndexOf);
			return removeBackSlant(_new);
		} else {
			return _url;
		}
	}
	 
}



@


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章