文本文件内容 读取 写入 替换 复制
2013-06-28 21:52
531 查看
@
@
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; } } }
@
相关文章推荐
- python 学习记录(11)-文件处理/读取文件/文件写入内容/文件删除/文件复制/文件重命名/后缀名/内容查找与替换/文件比较/ 配置文件访问/目录创建与删除/遍历目录/定向输出
- PHP文件写入,读取与替换内容
- PHP 读取文本文件内容写入数据库
- Java操作读取写入文本TXT及XML文件内容
- python读取 .txt 文本内容以及将程序执行结果写入txt文件
- 一个模式字符串,一个替换用字符串,以及两个文件名 它应该读取第一个文件, 并将内容写入第二个文件
- asp.net创建并写入文本和读取文件内容
- 读取文本的内容和将文本写入文件中
- PHP文件写入,读取与替换内容strReplace.php
- Java中读取txt文本中内容+目录文件复制到指定目录
- Java读取和写入文本文件内容
- android 读取assets下的文本文件内容
- IO流_合并流读取多个文件的内容复制到一个文件中
- java按指定编码写入和读取文件内容的类
- python 使用-读取文件、替换文件中指定的内容
- Scala文件的读取和写入 从控制台输入内容
- python 读取文本文件内容转化为python的list
- Java利用SMB上传、下载、读取(内容或文件名)、复制、删除远程文件
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- 通过java字节流复制文件,逐个字节读取写入