Java 閱讀TXT檔案

來源:互聯網
上載者:User

標籤:

public class GenCategoryAttrItemHandler {private final static String INPUT_FILE_PATH = "input/category_attr_item.txt";private final static String OUTPUT_FLIE_PATH = "output/category_attr_itemList.txt";/** * @param args * @throws IOException  */public static void main(String[] args) throws IOException {// TODO Auto-generated method stubGetDataFromFile();WriteDataToFile();System.out.println("ok");}/** * 從檔案提取資料。格式化到程式中 * @throws IOException */public static void GetDataFromFile() throws IOException {final String DELIMITER = "\t";BufferedReader br = null;try {File file = new File(INPUT_FILE_PATH);br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));String line = null;while (null != (line=br.readLine())) {String[] strs = line.split(DELIMITER);Category category = new Category(Long.parseLong(strs[0]),strs[1]);Attribute attribute = new Attribute(Long.parseLong(strs[2]),strs[3]);Item item = new Item(Long.parseLong(strs[4]),strs[5]);...}// while} catch (Exception e) {e.printStackTrace();} finally {if (null != br) {br.close();}}}/** * 將格式化的資料寫入檔案裡 * @throws IOException  */public static void WriteDataToFile() throws IOException {final String DELIMITER = "\t";BufferedWriter bw = null;try {File file = new File(OUTPUT_FLIE_PATH);bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));for (int i=1; i<12; ++i) {                                ...bw.write(outValue);  // 將資料寫入檔案裡bw.newLine();        // 建立一個分行符號bw.flush();}} catch (Exception e) {e.printStackTrace();} finally {if (null != bw) {bw.close();}}}}

著作權聲明:本文部落格原創文章,部落格,未經同意,不得轉載。

Java 閱讀TXT檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.