如何读取文本文件并将其存储在类向量中-Android

像素士兵

我想将文本文件读入我的android程序,并将内容存储在类的向量中。文本文件内容的示例如下所示:

Latitude  Longitude  Radioactivity
56.0349  -3.34267    8690000
56.0328  -3.342      867289
56.0328  -3.342      867289
56.0348  -3.34242    404430
56.0348  -3.34247    295287
56.0338  -3.34122    221830
56.0346  -3.34242    193347
56.0337  -3.34118    182304
56.0342  -3.34141    155572
56.0337  -3.34173    145229
56.0347  -3.34239    125143

我想将这些值存储在矢量(或数组中,因为列表的长度是有限的),以便可以在for循环中访问列表以将用户当前位置与点列表进行比较(例如地理围栏,但我有一个点数据库)。

我已经用c ++做到了,但是我以前没有用java编程过,这是我的第一个android应用。下面是我的C ++代码。我的问题是,如何为我的Android应用程序在Java中做同样的事情?

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <iomanip>

using namespace std;

struct radioactivityData
{
double lat;
double lon;
int radioactivity;
};

int main()
{
std::ifstream dataFile;
dataFile.open("combinedorderedData.txt");

std::string tmpLine;
std::vector<radioactivityData> radioactivityTable;

while(std::getline(dataFile, tmpLine))
{
    std::stringstream inputLine(tmpLine);

    radioactivityData rad;
    if(!(inputLine >> rad.lat >> rad.lon >> rad.radioactivity))
    {
        // ... error parsing input. Report the error
        // or handle it in some other way.

        continue;   // keep going!
    }
    radioactivityTable.push_back(rad);
}
Opster Elasticsearch专家

这是一种逐行读取文件的通用方法:

private void processFile(Context context, String fileName) {
    BufferedReader br;

    File file = new File(context.getExternalFilesDir(null) + "/" + FILE_DIR, fileName);
    try {
        FileReader fr = new FileReader(file);
        br = new BufferedReader(fr);
    } catch (FileNotFoundException e) {
        Log.e("couldn't read from external file");
        return;
    }

    try {
        String line;
        while ((line = br.readLine()) != null) {
            // here you put your code
            processLine(line);
        }
    } catch (IOException e) {
        Log.e("couldn't process line");
    } finally {
        try {
            if (br != null) {
                br.close();
            }
        } catch (IOException e) {
            Log.e("Failed to close BufferedReader");
        }
    }
}

假设您有一种方法可以从行字符串创建所需的RadioactivityData对象:

private ArrayList<RadioactivityData> mRadioactivityList = new ArrayList<RadioactivityData>();

private void processLine(String line) {

    RadioactivityData radioactivityData = new RadioactivityData(line);
    mRadioactivityList.add(radioactivityData);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从文本文件读取并存储到android中的数组

如何在Android中读取文本文件?

如何从Android Studio中的资产读取文本文件?

如何从文本文件中读取数据并将其加载到向量中?

如何拆分文本文件中的字符串(从原始文件夹检索),然后将其存储到android中两个单独的变量中?

如何使用空格android java读取文本文件

Android-如何从网址读取文本文件?

从Android Studio Java中的文本文件读取

在Android中逐行读取文本文件

如何从文本文件中读取数据并将其存储在C语言变量中?

如何在python 3.3.3中读取文本文件并将其存储在变量中?

如何使用C#读取此文本文件并将其存储在列表中

Android读取文本文件太慢

Delphi FMX(Android)-如何在zip流中读取文本文件而不进行提取?

如何在Android中阅读文本文件?

如何在ListView Android中打开文本文件

Xamarin Android,从内部存储读取/下载简单文本文件

从文本文件中读取数据并将其存储在python中的数组中

在android中写入文本文件

Android中的文本文件限制?

从文本文件中读取行并将其存储在数组中

从文本文件中读取网格并将其存储在二维数组中?

从文本文件中读取行并将其存储在多维数组php中

从文本文件中读取值并将其存储到列表中

如何使用 node.js 读取名词文本文件并将其存储在变量中

在Python中逐行读取文本文件并将其存储为struct作为图形坐标

从文本文件读取数据并将其存储在对象数组中-NodeJS

从文本文件读取整数并将其存储到数组中

使用JQuery读取文本文件并将其存储在数据库中