JAva 1行読み出しtxtドキュメントへの書き込み

2432 ワード

package com.ynu.www.tool;
import java.io.*;
import java.util.*;

//      :      ,      ,       java.io.IOException: Stream closed  
//  txt      ,     。
public class Txt2Database {
private final static String PATH = System.getProperty("user.dir");
public final static String DIR = PATH + "/src/com/ynu/www/data/";

public List readeTxt(String filename) {
//     txt        
List txtList = new ArrayList();
try {
//            
int id = 0;
FileReader fr = new FileReader(DIR + filename);
BufferedReader bf = new BufferedReader(fr);
String str = null;
while ((str = bf.readLine()) != null) {
//     txt       
List tempList = new ArrayList();
id++;
String[] strs = str.split(" ");
if (strs[0] != null && !strs[0].equals("") && strs[1] != null
&& !strs[1].equals("")) {
tempList.add(Integer.parseInt(strs[0]));
tempList.add(Integer.parseInt(strs[1]));
} else {
System.out.println("   " + id + " txt     ,         !");
}
txtList.add(tempList);
}
} catch (Exception e) {
System.out.println("  txt     ,    !");
e.printStackTrace();
}
return txtList;
}


//              ,  txt。
public void writerTxt(List txtList) {
try {
FileOutputStream fos = new FileOutputStream("E:/test.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");//        ,             
BufferedWriter bw = new BufferedWriter(osw);
for (int i = 0; i < txtList.size(); i++) {
List tempList = new ArrayList();
tempList = txtList.get(i);
int tempValue0 = (Integer) tempList.get(0);
int tempValue1 = (Integer) tempList.get(1);
if (tempValue0 < 5000 && tempValue1 < 5000) {
bw.write(tempValue0 + " " + tempValue1 + "\r
"); } } // , , bw.close(); osw.close(); fos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // list public void printList(List txtList) { // txt List tempList = new ArrayList(); for (int i = 0; i < txtList.size(); i++) { tempList = txtList.get(i); System.out.print(tempList.get(0) + " " + tempList.get(1)); System.out.println(); } System.out.println(" "); } }