[英]Read Text File to Database in android
I need to read data from a text file and save it to database. 我需要从文本文件读取数据并将其保存到数据库。
I have written this code but it is giving me a runtime error: 我已经编写了这段代码,但是它给了我一个运行时错误:
FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
My code is: 我的代码是:
public void GetFrom_Text() {
String []message=null;
BufferedReader br=null;
String name1, faculty1, deparment1, officeNumber1, email1, phone1;
long e;
try{
String sCurrentLine = null;
br =new BufferedReader(new InputStreamReader(getAssets().open("ab.txt")));
while((sCurrentLine = br.readLine()) != null) {
if(sCurrentLine != null )
message=sCurrentLine.split(",");
faculty1 = message[0];
deparment1 = message[1];
name1 = message[2];
officeNumber1 = message[3];
phone1 = message[4];
email1 = message[5];
e = db2.insertRecord(faculty1, deparment1, name1, officeNumber1, phone1, email1, email1);
}
}
catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (br !=null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
the lines in text file contain data such that: 文本文件中的行包含如下数据:
it,csa,Mohammad Bsoul,it239,4573,mbsoul@hu.edu.jo 它,csa,Mohammad Bsoul,it239,4573,mbsoul @ hu.edu.jo
I need to split each record and save it to database and i need to see if the data is save it successfully to datbase 我需要拆分每条记录并将其保存到数据库,并且我需要查看数据是否成功将其保存到datbase
One of the lines of your text file is not being split into 6 elements. 文本文件的其中一行不会被拆分为6个元素。 This causes the exception to happen on this line...
这导致该行发生异常...
email1=message[5];
...because there is no 5th index. ...因为没有第5个索引。 Check your text file and make sure the data is correct.
检查您的文本文件,并确保数据正确。
Try set Log.d("Line from file", sCurrentLine)
before split and you can see string for split in LogCat. 尝试在拆分之前设置
Log.d("Line from file", sCurrentLine)
然后在LogCat中可以看到要拆分的字符串。 I think your string miss last "," 我想你的字符串想念最后的“,”
Few things: 一些事情:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.