简体   繁体   English

android从文本文件读取

[英]android reading from a text file

I have a java class where it reads some data from a text file using a buffered reader and returns that data as a hash map: 我有一个Java类,它使用缓冲的读取器从文本文件中读取一些数据,并将该数据作为哈希图返回:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;

public class FrequencyLoader {

     public FrequencyLoader() throws FileNotFoundException {
        }

        public HashMap<String, Double> loadUnigramFrequencies() throws FileNotFoundException, IOException {
            HashMap<String, Double> unigramFrequencies = new HashMap<String, Double>();
            String line;
            String[] splittedLine;

            BufferedReader bf = new BufferedReader(new FileReader("unigramFrequencies.txt"));

            while ((line = bf.readLine()) != null) {
                splittedLine = line.split("\\s");


                unigramFrequencies.put(splittedLine[0].trim(), Double.parseDouble(splittedLine[1].trim()));
            }

            return unigramFrequencies;
        }
}

I want to use that in my android application but when I create an instance of this class and try to execute the loadUnigramFrequencies() function in the android Activity class I am getting an error that the application has stopped unexpectedly. 我想在我的android应用程序中使用它,但是当我创建此类的实例并尝试在android Activity类中执行loadUnigramFrequencies()函数时,我收到一个错误,指出该应用程序已意外停止。 I am trying to run it on Samsung Galaxy S2. 我正在尝试在Samsung Galaxy S2上运行它。 Should the file be placed somewhere in the android project rather than on the disk? 该文件应放置在android项目中而不是磁盘上吗? if yes then where? 如果是,那么在哪里?

without a bit of logcat it is a bit trivial. 没有一点logcat,这有点琐碎。

 unigramFrequencies.put(splittedLine[0].trim(), Double.parseDouble(splittedLine[1].trim()))

here for instance could be raised a null pointer execption if splittedLine[0] or splittedLine[1] is null, or parseDouble could arise a number format execption 例如,如果splittedLine [0]或splittedLine [1]为null,或者parseDouble可能会产生数字格式执行,则此处可能会引发空指针执行

I think the error might well be there : 我认为错误很可能在那里:

BufferedReader bf = new BufferedReader(new FileReader("unigramFrequencies.txt"));

You should provide an absolute path here and first make sure that the file exists before accessing it or handle the exception. 您应在此处提供绝对路径,并在访问文件处理异常之前首先确保该文件存在。

If this file is some final asset, you should place it in your project assets folder and get a filereader from there. 如果此文件是最终资产,则应将其放在项目资产文件夹中,并从那里获取文件阅读器。

Example (from here ): 示例(从此处开始 ):

AssetFileDescriptor descriptor = getAssets().openFd("unigramFrequencies.txt");
FileReader reader = new FileReader(descriptor.getFileDescriptor());

Note that your unigramFrequencies.txt file should be present in your <project>/assets/ directory 请注意,您的unigramFrequencies.txt文件应位于您的<project> / assets /目录中

This is searching for a needle in the hay stack. 这是在干草堆中寻找针头。

I recommend you to first learn how to use debugging in Android: 我建议您首先学习如何在Android中使用调试功能:
http://www.droidnova.com/debugging-in-android-using-eclipse,541.html http://www.droidnova.com/debugging-in-android-using-eclipse,541.html

Also some exception handling wouldn't hurt: 另外,一些异常处理也不会造成伤害:
http://en.wikibooks.org/wiki/Java_Programming/Throwing_and_Catching_Exceptions http://en.wikibooks.org/wiki/Java_Programming/Throwing_and_Catching_Exceptions

The following line of code is very wrong, and it seems you don't understand file storage in android: 以下代码行是非常错误的,似乎您不理解android中的文件存储:

new FileReader("unigramFrequencies.txt")

Here it is explained: 在此说明:
http://developer.android.com/guide/topics/data/data-storage.html http://developer.android.com/guide/topics/data/data-storage.html

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

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