我正在尝试从文本文件中读取并将每一行放在一个节点中,文件如下所示:
2000;阿斯顿马丁;阿斯顿马丁;DB9;汽车;A
2001;阿斯顿马丁;阿斯顿马丁;DB9;汽车;B
2002 ;阿斯顿马丁;阿斯顿马丁;V8 VANTAGE;汽车;C
2003;阿斯顿马丁;阿斯顿马丁;V8 VANTAGE;汽车;D
我想用“;”分隔行并将每个字符串放在节点中的一个值中
但链接仍然是空的
虽然在 JAVA 简单程序中这个链接列表代码表现不错,但是阅读有问题
请帮助
这是代码:
class node{
node next ;
node previous ;
String year ;
String company ;
String manufacture ;
String modelname;
String type ;
String owner;
}
public class MainActivity extends Activity {
node head = null ;
node tail = new node ();
node nn= null;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[]tokens = new String [5] ;
try{
FileInputStream fstream = new FileInputStream("config.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line="";
while ((line=br.readLine()) != null) {
tokens = line.split(";");
if (head==null){
head = new node ();
head.year=tokens[0];
head.company =tokens[1];
head.manufacture=tokens[2];
head.modelname =tokens[3];
head.type= tokens[4];
head.owner=tokens[5];
head.next = null;
head.previous=null;
tail=head;
}
else {
nn = new node ();
nn.year=tokens[0];
nn.company =tokens[1];
nn.manufacture=tokens[2];
nn.modelname =tokens[3];
nn.type= tokens[4];
nn.owner=tokens[5];
tail.next = nn ;
nn.previous = tail ;
tail=nn;
nn.next = null;
}
}
in.close();
}
catch (Exception e){//Catch exception if any
e.printStackTrace();
}