从加载.txt文件的数组? (Android版) [英] Loading an array from a .txt file? (Android)

查看:205
本文介绍了从加载.txt文件的数组? (Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已搜索周围,一直没能有一个解决方案,这样一来COEM起来..
我的(第一个)问题是,我对获得NPE 的FileInputStream河道= context.getApplicationContext()openFileInput(textname); getLengthOfText( ); 方法。我已经调试和正确的文件名似乎传递给此方法。我一直停留在这几个星期,真希望它的工作(福利局)。

I've searched around and haven't been able to coem up with a solution to this one.. my (first) problem is that i'm getting NPE on FileInputStream instream = context.getApplicationContext().openFileInput(textname);in the getLengthOfText(); method. I've debugged and the correct filename appears to be passed to this method. I've been stuck on this for weeks and really want it to work (newb).

这种方法正被另一个类调用。此外,文件中有数据/数据​​和其他一切都是理所应当的。请帮忙!!
-Tricknology

this method is being called by another class. Also, the files are there in data/data and everything else is as it should be. please help!! -Tricknology

/***** this class takes a text file and loads it into an array **/

public class loadArray {

Context context;
textWriter textwriter;
public loadArray (Context cxt) {
     this.context = cxt;
     textwriter = new textWriter (cxt);
     context = cxt;
}

    FileInputStream instream;
    textWriter tw = new textWriter(context);
    String[] choiceAry, dummyAry;
    P p = new P(); // shared prefs helper
    String Choice;
    String comp = "notnull";

    int addChoiceCount, length;

    // Context context = this.getApplicationContext();

    public void buildDummies(int length) {
        // addChoiceCount = p.gisI("addChoiceCount");
        choiceAry = new String[length];
        p.pisI("length", length, context);

        // dummyAry = new String[100];

    }

    public Integer getLengthOfText(String textname)
            throws FileNotFoundException {// counts how many lines of text
        // DataInputStream dis = new DataInputStream(openFileInput("file.dat"));
        int length = 0;
        instream = context.getApplicationContext().openFileInput(textname);
        InputStreamReader inputreader = new InputStreamReader(instream);
        BufferedReader buffreader = new BufferedReader(inputreader);

        try {
            // while (!comp.equals(null)){
            while (buffreader.ready()) {
                String temp = buffreader.readLine();
                length++;
            }
            buffreader.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return length;
    }

    public String[] laft(String textname) throws FileNotFoundException {
        // loads a text file to an array...load array from text........
        length = getLengthOfText(textname);
        buildDummies(length);
        try {
            instream = context.getApplicationContext().openFileInput(textname);
            InputStreamReader inputreader = new InputStreamReader(instream);
            BufferedReader buffreader = new BufferedReader(inputreader);
            // load array from text
            for (int i = 0; i < (length); i++) {
                try {
                    Choice = buffreader.readLine();
                    choiceAry[i] = Choice;
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                buffreader.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return choiceAry;

    }

    public String getOrderName() {
        // TODO Auto-generated method stub
        return null;
    }

}

我找到了答案感谢利兹和台球来自#Android的开发

I found the answer thanks to Leeds and billiard from #android-dev

我在做什么,也从另一个类,没有延伸活动呼吁loadArry ..所以这是活动>调用>类,这并不扩展活动>调用>类,这并不延伸活动。不知何故在那里的情况下丢失了。我基本上是在顶部类似的应用开始的行

what I was doing is also calling loadArry from another class that did not extend activity.. so it was activity >calling> class that doesnt extend activity >calling> class that doesnt extend activity. somehow in there the context was lost. I basically applied similar lines at the top starting with

Context context;
textWriter textwriter;
public loadArray (Context cxt) {
     this.context = cxt;
     textwriter = new textWriter (cxt);
     context = cxt;

希望这样可以节省别人很多的时间在未来。

hope this saves someone a lot of time in the future.

推荐答案

您已经取得的东西在code太复杂了自己。最终,复杂的code =更难调试。

You have made things much too complicated for yourself in the code. In the end, complicated code = harder debugging.

我打开文本文件到阵列的所有时间。

I load text files into arrays all the time.

考虑使用 java.util.StringTokenizer中类或类java.lang.String字符串split()方法。

Consider using the java.util.StringTokenizer class or the String split() method in class java.lang.String.

TenFour4带来了另一个好点。

TenFour4 brings up another good point.

这是code应该是什么样子...

This is what the code should look like...

Context context;
textWriter textwriter;
public loadArray (Context cxt){
     this.context = cxt;
     textwriter = new textWriter (cxt);
}

- 更新 -

一个常见的​​错误是,您正试图从中读取数据的文件没有正确关闭,所以你得到一个 NullPointerException异常每当您尝试访问仍然打开的文件

A common mistake is that the file you are trying to read from has not been closed properly, therefore you are getting a NullPointerException whenever you try to access the still open file.

例如

PrintWriter pw = new PrintWriter (new FileWriter (fileName));
pw.println ("This is output data: " + data);
//new loadArray ().getLengthOfText (fileName); //ERROR Throws NPE
pw.close(); //Make Sure you Close Before trying to read the file again!
new loadArray ().getLengthOfText (fileName); //Works like a charm :)

这篇关于从加载.txt文件的数组? (Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆