首页 > 代码库 > 【DataStructure】A useful util class for reading and writing files
【DataStructure】A useful util class for reading and writing files
Faced with the upcoming exam, Some useful methods referred to file operation drew tremenous attention. Now I make a summary to reading file.
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.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class FileUtil { public static final String SEP = ":"; public static final String ENCODING = "UTF-8"; /** * Read the content from file, the format of content is like "Key:Value" * TODO * * @time Jul 17, 2014 11:21:50 AM * @param filePath * @return * @throws Exception * @return Map<String,String> */ public static Map<String, String> readFileToMap(String filePath) throws Exception { System.out.println("读文件[" + filePath + "]开始..."); Map<String, String> contents = new HashMap<String, String>(); File file = new File(filePath); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader( new FileInputStream(file), ENCODING)); String info = null; while ((info = reader.readLine()) != null) { contents.put(info.split(SEP)[0], info.split(SEP)[1]); } } catch (FileNotFoundException e) { System.out.println("文件[" + filePath + "]未找到."); throw e; } catch (IOException e) { System.out.println("文件IO操作出错, 异常信息: " + e.getMessage()); throw e; } catch (Exception e) { System.out.println("读文件出错, 异常信息: " + e.getMessage()); throw e; } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { System.out.println("关闭IO流出错, 异常信息: " + e.getMessage()); throw e; } } } System.out.println("读文件[" + filePath + "]结束."); return contents; } /** * Read the content from file, every line will be an element of list. * * @time Jul 17, 2014 11:39:07 AM * @param fileName * @return * @return List<String> */ public static List<String> readFileToList(String fileName) throws Exception { List<String> infos = new ArrayList<String>(); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader( new FileInputStream(new File(fileName)), "utf-8")); String info = null; while ((info = reader.readLine()) != null) { infos.add(info); } } catch (FileNotFoundException e) { System.out.println("文件[" + fileName + "]未找到."); throw e; } catch (IOException e) { System.out.println("文件IO操作出错, 异常信息: " + e.getMessage()); throw e; } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } } return infos; } /** * Read all the content from filePath at a time. * * @time Jul 17, 2014 11:45:55 AM * @param filePathOrFileName * @return * @return String */ public static String readFileToString(String filePath) throws Exception { InputStreamReader isReder = null; BufferedReader br = null; StringBuffer sb = new StringBuffer(); try { File f = new File(filePath); if (f.isFile() && f.exists()) { isReder = new InputStreamReader(new FileInputStream(f), "UTF-8"); br = new BufferedReader(isReder); String line; while ((line = br.readLine()) != null) { sb.append(line).append("\r\n"); } } } catch (Exception e) { System.out.println("exception when reading the file" + filePath); throw e; } finally { if (isReder != null) { try { isReder.close(); } catch (IOException e) { e.getMessage(); } } if (br != null) { try { br.close(); } catch (IOException e) { e.getMessage(); } } } return sb.toString(); } /** * Write result to the fileName * * @time Jul 17, 2014 4:57:57 PM * @param result * @param fileName * @return void */ public static void writeFile(String result, String fileName) { File f = new File(fileName); OutputStreamWriter osw = null; BufferedWriter bw = null; try { if (!f.exists()) { f.createNewFile(); } osw = new OutputStreamWriter(new FileOutputStream(f), "UTF-8"); //"gbk" bw = new BufferedWriter(osw); bw.write(result); } catch (IOException e) { System.out.println("IOException when writing result, " + e.getMessage()); } catch (Exception e) { System.out.println("Exception when writing result, " + e.getMessage()); } finally { if (bw != null) { try { bw.close(); } catch (IOException e) { System.out.println("IOException when closing FileWriter, " + e.getMessage()); } } if (osw != null) { try { osw.close(); } catch (IOException e) { System.out.println("IOException when closing bufferwriter, " + e.getMessage()); } } } } public static void main(String[] args) throws Exception { Map<String, String> testMap = FileUtil.readFileToMap("input.txt"); System.out.println("---------------------"); for (Map.Entry<String, String> entry : testMap.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println("---------------------"); List<String> testList = FileUtil.readFileToList("input.txt"); for (String str : testList) { System.out.println(str); } System.out.println("---------------------"); String test = FileUtil.readFileToString("input.txt"); FileUtil.writeFile(test, "output.txt"); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。