使用InputStream和Scanner android从.txt文件读取数据

时间:2014-07-01 06:15:23

标签: android file text inputstream

我无法从.txt文件中读取数据。当我运行应用程序时,我收到一条错误消息并且应用程序崩溃,我怀疑这与读取文本文件或将其从InputStream转换为扫描程序有关。我有以下代码:

FileExaminer a = new FileExaminer();
FileExaminer b = new FileExaminer();
FileExaminer c = new FileExaminer();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try{
            readFile.runMethods();
        } catch (Exception e) {
            System.out.println("Error in Main");
        }
    }

public InputStream input;
public Scanner file;
AssetManager assetManager = getAssets();

public String a, b, c;  
public void runMethods() {
    openFile();
    readFile();
    closeFile();
}

public void openFile() {
    try{
        input = getAssets().open("numbers.txt");
        file = new Scanner(input);
    } catch(Exception e) {
        System.out.println("error");
        }
    }

public void readFile() {
    while (file.hasNext()) {
        a = file.next();
        b = file.next();
        c = file.next();

        System.out.printf("%s %s %s/n", a,b,c);
    }
}

public void closeFile() {
    file.close();
}

我的错误日志是这样的:

07-01 02:10:05.113: D/AndroidRuntime(2174): Shutting down VM
07-01 02:10:05.173: W/dalvikvm(2174): threadid=1: thread exiting with uncaught exception (group=0xb1cf6b20)
07-01 02:10:05.213: E/AndroidRuntime(2174): FATAL EXCEPTION: main
07-01 02:10:05.213: E/AndroidRuntime(2174): Process: com.aghgb.pokedex, PID: 2174
07-01 02:10:05.213: E/AndroidRuntime(2174): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.aghgb.pokedex/com.aghgb.pokedex.Main}: java.lang.NullPointerException
07-01 02:10:05.213: E/AndroidRuntime(2174):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at android.os.Looper.loop(Looper.java:136)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at java.lang.reflect.Method.invokeNative(Native Method)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at java.lang.reflect.Method.invoke(Method.java:515)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at dalvik.system.NativeStart.main(Native Method)
07-01 02:10:05.213: E/AndroidRuntime(2174): Caused by: java.lang.NullPointerException
07-01 02:10:05.213: E/AndroidRuntime(2174):     at android.content.ContextWrapper.getAssets(ContextWrapper.java:83)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at com.aghgb.pokedex.FileExaminer.<init>(FileExaminer.java:13)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at com.aghgb.pokedex.Main.<init>(Main.java:7)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at java.lang.Class.newInstanceImpl(Native Method)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at java.lang.Class.newInstance(Class.java:1208)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
07-01 02:10:05.213: E/AndroidRuntime(2174):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
07-01 02:10:05.213: E/AndroidRuntime(2174):     ... 11 more

提前致谢。

2 个答案:

答案 0 :(得分:1)

经过几个小时的研究和反复试验后,我想出了这个:

    try {
        file = new Scanner(getResources().openRawResource(R.raw.pokemon_data)); 
        System.out.println("File Scanned");
        // do reading, usually loop until end of file reading 
        while (file.hasNext()) {
            a = file.next();
            b = file.next();
            c = file.next();
            System.out.printf("%s %s %s\n", a, b, c);
        }
    } catch (Exception e) {
        System.out.println("Error opening/reading file");
    } finally {
        if (file != null) {
            file.close();
        }
    }

此代码用于打印已存储在列中的文本文件,只要文件位于原始文件夹中,该代码就可以正常工作。在while循环中添加更多变量将增加显示的列数。

答案 1 :(得分:0)

要读取.text文件,您可以使用以下方法:

 try {
    txtFileRead("filename.txt");
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}


public List<String> txtFileRead(String filename) throws IOException {
    List<String> readData = new ArrayList<String>();
    AssetManager assets = context.getAssets();
    BufferedReader reader = new BufferedReader(new InputStreamReader(assets.open(filename)));
    while(true) {
        String line = reader.readLine();
        if(line == null) {
            break;
        }
        lines.add(line);
    }
    return lines;
}

in txtFileRead()我正在阅读文本文件并将读取的数据添加到列表中。