public static void main(String args[]) throws Exception {
// 陣列的長度是一行記錄的欄位數,陣列的每個值分別表示該欄位的長度
// 根據你的檔案要求改
int[] RecordStructure = new int[] { 16, 5, 4, 2, 3, 4, 6, 4, 19, 4 };
InputStreamReader b = new InputStreamReader(new FileInputStream(
new File("d:/test.txt")), "gbk");
// 最終結果
List
// 存放一整條記錄的中間變數
int i = 0;
// 設定快取區,記錄中單個數據的長度不可超過快取區的長度
String temp = "";
// 欄位位置標誌,範圍:[0-你的欄位數]
int fieldPosition = 0;
//標題的字元的行數 這裡可以改!!!
int labelLength = 6;
BufferedReader a = new BufferedReader(b);
for(int j = 0; j < labelLength; j++) {
a.readLine();
}
while (true) {
i = a.read();
if (i == "\r" || i == "\n") {
continue;
} else {
temp += (char) i;
int fieldLength = RecordStructure[fieldPosition];
if (temp.length() >= fieldLength) {
fieldPosition += 1;
date.add(temp);
if (fieldPosition == RecordStructure.length) {
result.add(date);
fieldPosition = 0;
date = new ArrayList
temp = "";
if (i == -1) {
break;
for (int j = 0; j < result.size(); j++) {
System.out.println(Arrays.toString(result.get(j).toArray()));
b.close();
a.close();
public static void main(String args[]) throws Exception {
// 陣列的長度是一行記錄的欄位數,陣列的每個值分別表示該欄位的長度
// 根據你的檔案要求改
int[] RecordStructure = new int[] { 16, 5, 4, 2, 3, 4, 6, 4, 19, 4 };
InputStreamReader b = new InputStreamReader(new FileInputStream(
new File("d:/test.txt")), "gbk");
// 最終結果
List
// 存放一整條記錄的中間變數
List
int i = 0;
// 設定快取區,記錄中單個數據的長度不可超過快取區的長度
String temp = "";
// 欄位位置標誌,範圍:[0-你的欄位數]
int fieldPosition = 0;
//標題的字元的行數 這裡可以改!!!
int labelLength = 6;
BufferedReader a = new BufferedReader(b);
for(int j = 0; j < labelLength; j++) {
a.readLine();
}
while (true) {
i = a.read();
if (i == "\r" || i == "\n") {
continue;
} else {
temp += (char) i;
int fieldLength = RecordStructure[fieldPosition];
if (temp.length() >= fieldLength) {
fieldPosition += 1;
date.add(temp);
if (fieldPosition == RecordStructure.length) {
result.add(date);
fieldPosition = 0;
date = new ArrayList
}
temp = "";
}
}
if (i == -1) {
break;
}
}
for (int j = 0; j < result.size(); j++) {
System.out.println(Arrays.toString(result.get(j).toArray()));
}
b.close();
a.close();
}