简体   繁体   English

在Android中将文本文件读取到数据库

[英]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: 一些事情:

  1. Any decent IDE like Eclipse provides ways to properly format the code. 任何像Eclipse这样的体面的IDE都提供了正确格式化代码的方法。 In your code, indentation is so off, it's hard to read. 在您的代码中,缩进是如此复杂,很难阅读。 You can set a 'save action' for your project to format properly. 您可以为项目设置“保存操作”以正确格式化。 This way each time you hit save, your code gets formatted. 这样,每次您点击保存时,您的代码都会被格式化。
  2. Why can't you use a debugger and step through the code? 为什么不能使用调试器来逐步执行代码? Put a breakpoint after message=sCurrentLine.split(","); 在message = sCurrentLine.split(“,”);之后放置一个断点。 and look at the message variable in the 'expressions' window. 并在“表达式”窗口中查看message变量。
  3. If you look at the code for readLine, it looks at '\\r', '\\n', or '\\r\\n'. 如果您查看readLine的代码,则它的内容为'\\ r','\\ n'或'\\ r \\ n'。 In your text file, are you sure you have the appropriate new line character? 您确定您的文本文件中具有合适的换行符吗? Else if your last line of the text file is not properly formatted, then you can crash 否则,如果文本文件的最后一行格式不正确,则可能会崩溃
  4. Using the debugger you should clearly be able to see where you are crashing. 使用调试器,您应该可以清楚地看到崩溃的位置。 You are assuming it's at-it,csa,Mohammad Bsoul,it239,4573,mbsoul@hu.edu.jo but that may be fine but in your code if there's a comma missing somewhere, you can still get an ArrayIndexOutOfBounds. 您假设它是at-it,csa,Mohammad Bsoul,it239,4573,mbsoul @ hu.edu.jo,但这可能很好,但是在您的代码中,如果某处缺少逗号,您仍然可以获取ArrayIndexOutOfBounds。
  5. Can you use a debugger? 可以使用调试器吗? If not, then google how to debug and put breakpoints and look at watch windows. 如果没有,那么谷歌如何调试和放置断点,并看着监视窗口。 Without it, you will spend 10x (figuratively) the time to debug such trivial issues 如果没有它,您将花费10倍(象征性的)时间来调试这些琐碎的问题
  6. Feel free to PM me and send me your file so I can see if anything glaring is missing. 请随时与我联系,并将您的文件发送给我,以便我查看是否缺少任何明显的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM