JAVA-文件读写模板
来源:互联网 发布:linux设置共享目录 编辑:程序博客网 时间:2024/05/07 04:49
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(); } } }}
1 0
- JAVA-文件读写模板
- java读写文件的模板
- java 读写 模板文件-分享
- (模板)java文件读写与保留小数
- ACM文件读写模板
- android文件读写模板
- python 文件读写模板
- 读写文件函数【模板】
- java 读写word java 动态写入 模板文件
- ireport java 读写模板
- java 读写ini文件
- java读写文件
- Java读写文件方法
- java 读写属性文件
- JAVA同时读写文件
- java 文件读写
- java文件读写
- JAVA IO读写文件
- live555
- 欢迎使用CSDN-markdown编辑器
- Windows下gvim的编码问题
- Android防止按钮连续点击
- 浏览器定位
- JAVA-文件读写模板
- Java_处理日期的工具类
- Jenkins配置自动化构建
- Statistical Decision Theory and Bayesian Analysis 笔记
- storm学习笔记
- 谈谈iOS7及以上版本navigationBar带来的坑
- Oracle启动监听提示:The listener supports no services 的处理方法
- 多线程学习04-NSThread创建
- Struts 原理