JAVA-文件读写模板
2016-04-19 11:14
393 查看
class FileEntry { /** * 读取文件的所有内容 * @param filePath 文件路径 * @param charset 编码 * @return 文件的全部内容,作为一个字符串返回 */ public static String readFile(String filePath, String charset) { File file = new File(filePath); Long filelength = file.length(); byte[] filecontent = new byte[filelength.intValue()]; FileInputStream in = null; String result = null; try { in = new FileInputStream(file); in.read(filecontent); result = new String(filecontent, charset); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block } catch (FileNotFoundException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } finally { try { if (null != in) in.close(); } catch (IOException e) { e.printStackTrace(); } } return result; } /** * 读取文件内容 * @param filePath 文件路径 * @param charset 编码 * @return 文件的每一行 */ public static ArrayList<String> readFileLines(String filePath, String charset) { BufferedReader reader = null; ArrayList<String> lines = new ArrayList<String>(); try { FileInputStream fis = new FileInputStream(filePath); InputStreamReader isr = new InputStreamReader(fis, charset); reader = new BufferedReader(isr); String tempString = null; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { lines.add(tempString); } reader.close(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return lines; } /** * 获取指定文件夹下的所有文件名称(递归) * @param folderPath 文件夹路径 * @return 每个文件的绝对路径 */ public static ArrayList<String> getFolderFiles(String folderPath) { ArrayList<String> ret = new ArrayList<String>(); File fileObject = new File(folderPath); if (fileObject.isFile()) ret.add(fileObject.getAbsolutePath()); else { for (String subFilePath: fileObject.list()) { ret.addAll(getFolderFiles(folderPath + File.separatorChar + subFilePath)); } } return ret; } /** * 将指定内容写入文件中 * @param filePath 文件路径 * @param content 待写入内容 * @param charset 编码 */ public static void writeFile(String filePath, String content, String charset) { BufferedWriter bw = null; try { File file = new File(filePath); file.getParentFile().mkdirs(); bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), charset)); bw.write(content); bw.flush(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (null != bw) bw.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 在文件尾追加内容 * @param content 待追加文本 * @param fileDes 文件路径 * @param charset 编码 */ public static void appendToFile(String filePath, String content, String charset) { BufferedWriter bw = null; try { File file = new File(filePath); file.getParentFile().mkdirs(); bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true), charset)); bw.write(content); bw.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != bw) bw.close(); } catch (IOException e) { e.printStackTrace(); } } } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树