1. 程式人生 > >生成和解析txt文件

生成和解析txt文件

stat zha 上海 查找內容 list lose list() checked types

package txt;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
/**
 * 功能描寫敘述:創建TXT文件並進行讀、寫、改動操作
 * @author lizhiyong
 * @version $Id: ReadWriteFile.java, v 0.1
		2014年8月5日 下午1:27:38 Exp $
 */
public class ReadWriteFile {
    //指定文件路徑和名稱
    private static String path     = "C:/測試.txt";
    private static File   filename = new File(path);
    private static String readStr  = "    ";

    /**
     * 創建文本文件.
     * @throws IOException 
     * 
     */
    public static void creatTxtFile() throws IOException {
        if (!filename.exists()) {
            filename.createNewFile();
            System.err.println(filename + "已創建!

"); } else { filename.delete(); creatTxtFile(); } } /** * 讀取文本文件. * @throws UnsupportedEncodingException * */ @SuppressWarnings("resource") public static String readTxtFile() throws UnsupportedEncodingException { String readData = null; //BufferedReader br = null; BufferedReader br = null; try { //br = new BufferedReader(new InputStreamReader(new FileInputStream(filename))); br = new BufferedReader(new FileReader(filename)); try { while ((readData = br.readLine()) != null) { System.out.println("readData:" + readData); readStr = readStr + readData + "\r\n"; } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println("文件內容2是:" + "\r\n" + readStr); return readStr; } /** * 給文件寫內容. * @param content 寫入的文件內容 * @throws IOException */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static void writeTxtFile(List contentList, HashMap<String, String> map) throws IOException { //先讀取原有文件內容。然後進行寫入操作 FileWriter writer = null; String filein = map.get("1") + readStr + map.get("2") + readStr + map.get("3") + readStr + map.get("4"); try { writer = new FileWriter(filename, true); writer.write(filein); } catch (IOException e1) { e1.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (IOException e2) { e2.printStackTrace(); } } } for (Iterator iterator = contentList.iterator(); iterator.hasNext();) { HashMap<String, String> map2 = (HashMap<String, String>) iterator.next(); String name = map2.get("name"); String age = map2.get("age"); String postion = map2.get("postion"); String complit = map2.get("complit"); String filein1 = "\r\n" + name + readStr + age + readStr + postion + readStr + complit + "\r\n"; try { writer = new FileWriter(filename, true); writer.write(filein1); } catch (Exception e) { e.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (IOException e2) { e2.printStackTrace(); } } } } readTxtFile(); } /** * 將文件裏指定內容的第一行替換為其他內容. * * @param oldStr * 查找內容 * @param replaceStr * 替換內容 */ @SuppressWarnings("unused") public static void replaceTxtByStr(String oldStr, String replaceStr) { String temp = ""; try { File file = new File(path); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); StringBuffer buf = new StringBuffer(); // 保存該行前面的內容 for (int j = 1; (temp = br.readLine()) != null && !temp.equals(oldStr); j++) { buf = buf.append(temp); buf = buf.append(System.getProperty("line.separator")); } // 將內容插入 buf = buf.append(replaceStr); // 保存該行後面的內容 while ((temp = br.readLine()) != null) { buf = buf.append(System.getProperty("line.separator")); buf = buf.append(temp); } br.close(); FileOutputStream fos = new FileOutputStream(file); PrintWriter pw = new PrintWriter(fos); pw.write(buf.toString().toCharArray()); pw.flush(); pw.close(); } catch (IOException e) { e.printStackTrace(); } } /** * main方法測試 * @param s * @throws IOException */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static void main(String[] s) throws IOException { ReadWriteFile.creatTxtFile(); //ReadWriteFile.readTxtFile(); List list = new ArrayList(); HashMap<String, String> map = new HashMap<String, String>(); map.put("1", "姓名"); map.put("2", "年齡"); map.put("3", "職位"); map.put("4", "工作單位"); HashMap<String, String> map2 = new HashMap<String, String>(); map2.put("name", "李四"); map2.put("age", "25"); map2.put("postion", "Java開發project師"); map2.put("complit", "上海汽車財務集團有限公司"); list.add(map2); HashMap<String, String> map3 = new HashMap<String, String>(); map3.put("name", "李四1"); map3.put("age", "251"); map3.put("postion", "Java開發project師1"); map3.put("complit", "上海汽車財務集團有限公司1"); list.add(map3); ReadWriteFile.writeTxtFile(list, map); // ReadWriteFile.replaceTxtByStr("ken", "zhang"); } }


生成和解析txt文件