簡單的file獲取文字內容且, 修改文字內容(java8)
題主, 因入職新公司, 表設計混亂, 不得不手動寫一個小指令碼,獲取所有欄位後,重新寫入至新表中;
思路
順序如下
原sql
具體, 獲取行 , 根據行開頭的" ,"擷取內容, 重新輸入到txt, 中就可以了;
程式碼
這裡就寫下 java8 的如何讀取和寫入吧
package xxx.xxx.xx; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; /** * @author :wuxw * @ClassName : test * @description: TODO * @date :2019/3/22 11:54 * @version: 1.0 */ public class test { public static void main(String[] args){ String filePath = "e:/textDb.txt"; List<String> lineLists = readTextFile(filePath); // 將每行內容分割 最後輸出到文字 Map<String,ColumnBean> tempMap = new HashMap<String,ColumnBean>(); lineLists.forEach(i ->{ String[] temp = i.split("\\s+"); ColumnBean tempBean = new ColumnBean(); if(temp.length>0){ tempBean.setCode(temp[1]); tempBean.setCtype(temp[2]); tempBean.setIsEmpty(temp[3]); if(temp.length == 6 ){ tempBean.setCommont(temp[5]); } if(temp.length == 7 ){ tempBean.setNote(temp[6]); } if(!tempMap.containsKey(temp[1])){ tempMap.put(temp[1],tempBean); } } }); for(ColumnBean c : tempMap.values()){ System.out.println(c.toString()+"----------"); } List<ColumnBean> mapKeyList = new ArrayList<ColumnBean>(tempMap.values()); mapKeyList.forEach(i -> { System.out.println("--------"+i.toString()); }); beanToTxt("e:/beanDb3.txt",mapKeyList); } /** * @Author wuxw * @Description 行內容版 - 將字串內容寫入文字 * @Date 17:12 2019/3/22 * @Param [filePath, content] * @return void **/ public static void contentToTxt(String filePath, String content) { try{ BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filePath),true)); writer.write(content); writer.close(); }catch(Exception e){ e.printStackTrace(); } System.out.println("檔案寫入完畢,檔案路徑:"+filePath); } /** * @Author wuxw * @Description 陣列迴圈寫入文字 * @Date 17:12 2019/3/22 * @Param [filePath, listStr] * @return void **/ public static void contentToTxt(String filePath, List<String> listStr) { try{ BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filePath),true)); listStr.forEach(str ->{ try { if(str.startsWith(" `")) { writer.newLine(); writer.write(str); } } catch (IOException e) { e.printStackTrace(); } }); writer.close(); }catch(Exception e){ e.printStackTrace(); } System.out.println("檔案寫入完畢,檔案路徑:"+filePath); } /** * @Author wuxw * @Description bean轉成行輸出至txt * @Date 17:11 2019/3/22 * @Param [filePath, listStr] * @return void **/ public static void beanToTxt(String filePath, List<ColumnBean> listStr) { try{ BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filePath),true)); listStr.forEach(str ->{ try { writer.newLine(); writer.write(str.toString()); } catch (IOException e) { e.printStackTrace(); } }); writer.close(); }catch(Exception e){ e.printStackTrace(); } System.out.println("檔案寫入完畢,檔案路徑:"+filePath); } /** * @Author wuxw * @Description 根據檔案路徑讀取檔案內容 * @Date 17:10 2019/3/22 * @Param [file] * @return java.util.List<java.lang.String> **/ public static List<String> readTextFile(String file){ //讀取檔案 List<String> lineLists = null; try{ lineLists = Files.lines(Paths.get(file), Charset.defaultCharset()) .flatMap(line -> Arrays.stream(line.split("\n"))) .collect(Collectors.toList()); } catch (IOException e) { //Your exception handling here } return lineLists; } }
/** * @author :wuxw * @ClassName : ColumnBean * @description: TODO * @date :2019/3/22 15:12 * @version: 1.0 */ @Data public class ColumnBean { private String code; private String ctype; private String isEmpty; private String commont; private String note; public String toString(){ return code+" "+ctype+" "+isEmpty+" "+commont+" "+note; } }
效果
相關推薦
簡單的file獲取文字內容且, 修改文字內容(java8)
開發十年,就只剩下這套架構體系了! >>>
ubantu系統獲取root 許可權 且 修改檔案內容的方法
獲取root 許可權 首先普及一個知識 就是ubantu 終端上輸入密碼是不現實的。不要把他看做出問題了 首次root,sudo passwd root 一 輸入賬戶密碼 二 輸入unix密碼(root密碼) 三 再次輸入unix 密碼(root
單行文字,編輯修改文字的一些技巧
我們在日常的CAD繪圖工作中,常常會接觸到文字文字編輯的相關操作。為了更加精準地繪圖工作,我們常常需要對文字文字進行各種各樣需要的編輯。今天小編就給大家分享一些文字編輯的一些小方法。具體操作如下:單行文字在專業的製圖軟體——迅捷CAD編輯器專業版裡操作如下:1.點選單行文字編輯圖示,按照相應的命令指示,我們輸
Delphi 修改文字框中內容
Delphi 修改Excel 中文字框內容 首先看一下Excel中錄製的巨集程式碼吧 Sub Macro1() ' ' Macro1 Macro ' 巨集由 HFBiao 錄製,時間: 2010-5-15 ' &
PDF如何編輯文字,如何修改PDF檔案內容
不管是在學習中還是在工作中,都會使用到PDF檔案大家都會使用到PDF檔案,那麼,對於PDF檔案的修改,則是需要使用到PDF編輯軟體的,那麼,PDF如何編輯文字,如何修改PDF檔案內容呢,是不是有很多的小夥伴都想知道應該怎麼做呢,那就可以看看下面的
隨機流修改文字內容
package com.oldboy.hbase; import java.io.*; public class Edit { public static void main(String[] args) throws IOException { File file = n
java檔案操作 (1)——判別指定檔案是否存在,讀取檔案修改時間和大小,讀取文字檔案內容,向文字檔案中寫入指定內容
任務要求: 完成一個java application應用程式,判別指定路徑下指定檔名的檔案是否存在。 如果指定檔案存在,讀取並分別顯示其修改時間和檔案大小等屬性。 以文字方式開啟某一指定路徑指定檔名的文字檔案,讀取其內容並顯示。 以文字方式向某
ueditor文字框初始化如何顯示預設內容,以及文字框獲取焦點後清空預設內容
<div> <form id="myForm" method="post" action="<?=Url::toRoute(['save'])?>">
android studio中編輯的文字框EditText以及文字框輸入內容的獲取方法
首先需要在佈局中加入如下內容,這樣一個文字框就有了 <EditText android:id="@+id/edit_text"//設定唯一的標識,呼叫時可通過id找到它 android:layout_width="match_pare
linux檔案操作—獲取文字中指定行的內容
1、用fgets函式可以讀取檔案中某行的資料,某列資料就必須一個一個讀入每行的第幾個字元,再存入到一個字串當中。 2、使用strncpy函式取指定行的某一段 char * strncpy(char *s1,char *s2,size_t n); 將字串s2中最多n
linux C 修改文字檔案中指定行的內容
1、可隨意修改內容 /* * Author:aniu12<[email protected]> * Date: 2014.02.13 */ #include<stdio.h> #include<stdlib.h> #in
JavaScript 獲取option value 值以及文字內容的方法
首先附上原始碼 <!DOCTYPE html> <html> <head> <title></title> </head
MFC 獲取列表控制元件的文字內容
int nRow = m_ListBankCtrl.GetItemCount(); int nRow = m_ListBankCtrl.GetItemCount(); int nCol = m_ListBankCtrl
獲取CKEditor的html文字、純文字、被選中的內容及賦值
1、獲取CKEditor被選中的內容 var mySelection = CKEDITOR.instances.WORK_INTRODUCTION.getSelection(); if (CKEDITOR.env.ie) {
獲取WebView加載的網頁內容並進行動態修改
datawit cli cap jsoup 動態修改 成功 scale 技術 parse http://www.jianshu.com/p/3f207a8e32cb 【Android】WebView讀取本地圖片 http://www.cnblogs.com/kimmy/p/
jQuery獲取radio選中後的文字
div text java 朋友 方法 dev 選中 type sdn 原文鏈接:http://blog.csdn.net/zhanyouwen/article/details/51393216 jQuery獲取radio選中後的文字轉載 2016年05月13日 10:32
利用DOM的方式點擊切換圖片及修改文字
ntb stc 文字 document div rst 我們 source dev 本案例主要學習理解,用到的幾個DOM方法 01、getAttribute()方法,獲取元素的屬性值 02、setAttribute(‘src‘,source) 方法,用後邊的值修改前邊這個元
Atiitt 圖片影象驗證碼生成法原理 目錄 1.1. 常見的最簡單圖片驗證碼是利用影象api把隨機數文字轉影象 1 1.2. 常見較為複雜圖片驗證碼的方法 ( 鏤空文字 打散 干擾線 文字扭曲
Atiitt 圖片影象驗證碼生成法原理 目錄 1.1. 常見的最簡單圖片驗證碼是利用影象api把隨機數文字轉影象 1 1.2. 常見較為複雜圖片驗證碼的方法 ( 鏤空文字 打散 干擾線 文字扭曲 粘連 膨脹,填充 ) 1 1.3. 實現 2
批量修改文字檔案的編碼格式
1.使用EditPlus開啟要修改編碼格式的檔案所屬目錄 2.按shift,選中左下角列表中需要轉換編碼格式的文字,右擊選擇“開啟”,開啟的效果如下: 3.依次選擇選單欄中的“文件”->“文字編碼”->“批量更改文字編碼”,按shift選擇要更改編碼格式的文字,然後單擊“
Js和Jquery獲取選中select值和文字
原文連結:http://www.cnblogs.com/shenyixin/archive/2012/12/11/2813221.html JS: <body> <select name="PaymentType" style="width:110px" &g