以下代码仅供参考
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
Ant[] ants;
public Main() {
ants = new Ant[100];
}
public static void main(String[] args) {
File f = new File("D:\\123.txt");
Main main = new Main();
main.decodeFile(f);
for (int i = 0, k = main.ants.length; i < k; i++) {
System.out.println(main.ants[i]);
}
}
public boolean decodeFile(File f) {
BufferedReader br;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String lineString = "";
int txtNameIndex;
Ant ant;
String[] antString;
String txtName;
String[] childString;
int index = 0;
while ((lineString = br.readLine()) != null && index < 100) {
antString = lineString.split("#");
ant = new Ant();
txtNameIndex = antString[0].indexOf(" ");
txtName = antString[0].substring(0, txtNameIndex);
antString[0] = antString[0].substring(txtNameIndex + 1);
for (int i = 0, k = antString.length; i < k; i++) {
ant = new Ant();
childString = antString[i].split("/");
ant.txtID = index + 1;
ant.txtName = txtName;
ant.keyword = childString[0];
ant.style = childString[1];
ant.wWeight = Double.parseDouble(childString[2]);
ant.wCount = Integer.parseInt(childString[3]);
ants[index] = ant;
index++;
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
}
class Ant {
int txtID;
String txtName;
String keyword;
String style;
double wWeight;
int wCount;
@Override
public String toString() {
return "[txtID:" + txtID + ",txtName:" + txtName + ",keyword:" + keyword + ",style:" + style + ",wWeight:"
+ wWeight + ",wCount:" + wCount + "]";
}
}
本回答被提问者和网友采纳