java hashmap 输出_JAVA如何把HashMap内容输出到文本文件
接下來我們創建一個TxtUtil工具類,來實現寫入文本文件的方法。下面這個方法是將數據追加寫入到文件文件的末尾,我們可以看到該方法有兩個參數,List就是我們要寫入的數據,因此不管是什么格式的數據我們都需要轉成List寫入,fileName是我們要寫入的文件名,如果這個文件不存在的話,下面的方法中new File(fileName)會幫我們創建。以下便是使用輸出了將數據寫入到文件的代碼,注意流在使用后一定要關閉。
/**?? * 數據追加寫到文件末尾?? *?? * @param dataList?? * @param fileName?? * @return?? */public static void appendData2File(List dataList, String fileName) ?{?? ? ?OutputStreamWriter out = null;?? ? ?FileOutputStream fos = null;?? ? ?try {?? ? ? ? ?File file = new File(fileName);?? ? ? ? ?if (!file.getParentFile().isDirectory()) {?? ? ? ? ? ? ?file.getParentFile().mkdirs();?? ? ? ? ?}?? ? ? ? ?if (!file.exists()) {?? ? ? ? ? ? ?file.createNewFile();?? ? ? ? ?} ? ? ? ?// 追加寫入文件,且使用UTF-8格式?? ? ? ? ?fos = new FileOutputStream(fileName, true);?? ? ? ? ?out = new OutputStreamWriter(fos, "UTF-8");?? ? ? ? ?for (int dataIndex = 0; dataIndex < dataList.size(); dataIndex++) {?? ? ? ? ? ? ?out.write(dataList.get(dataIndex));?? ? ? ? ? ? ?out.write("\r\n");?? ? ? ? ?}?? ? ?} catch (Exception e) {?? ? ?} finally {?? ? ? ? ?try {?? ? ? ? ? ? ?if (null != out) {?? ? ? ? ? ? ? ? ?out.flush();?? ? ? ? ? ? ? ? ?out.close();?? ? ? ? ? ? ? ? ?fos.close();?? ? ? ? ? ? ?}?? ? ? ? ?} catch (IOException e) {?? ? ? ? ?}?? ? ?}??}
總結
以上是生活随笔為你收集整理的java hashmap 输出_JAVA如何把HashMap内容输出到文本文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql语句的左外链接_MySQL中的
- 下一篇: access 更新整列数据_在acces