阅读文本文件,并在阵列填充数据。 [英] Read Text file and fill the data in Array.

查看:109
本文介绍了阅读文本文件,并在阵列填充数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林pretty确保我只是问一个愚蠢的问题,但是,突出部分说,在有上有此数据的txt文件

Im pretty sure im just asking a silly question but, jut say in have a txt file that has this data on it

UniPub; 112 Binara圣; ACT结果
MooseHeads; 54科恩圣; ACT结果
立方; 24莫森圣; ACT

UniPub;112 Binara St;ACT
MooseHeads;54 Cohen St;ACT
Cube;24 Mawson St;ACT

病态使用这个code读它在我的应用程序:

Ill read it in my application using this code:

package au.edu.canberra.g30813706;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.os.Environment;


public class FileReader extends Activity{{

    ArrayList<String> sInfo = new ArrayList<String>();

    String txtName = "AccomodationTxt.txt";
    File root = Environment.getExternalStorageDirectory();
    File path = new File(root, "CanberraTourism/" + txtName);

    try {

        BufferedReader br = new BufferedReader (
                            new InputStreamReader(
                            new FileInputStream(path)));
        String line;
        String[] saLineElements;
        while ((line = br.readLine()) != null)
        {
            //The information is split into segments and stored into the array
            saLineElements = line.split(";");
            for (int i = 0; i < saLineElements.length; i++) 
                  sInfo.add(saLineElements[i]);
            //sInfo.addAll(Arrays.asList(saLineElements[0], saLineElements[1], saLineElements[3]));     
        }
         br.close();


    } 
    catch (FileNotFoundException e) {

        System.err.println("FileNotFoundException: " + e.getMessage());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }}
}

我将如何区分线阵列?

How would i differentiate the lines in the Array?

例如

我要显示一个页面上的名称​​的文本,所以只有

I would want to display the text of the names on one page, so only

UniPub结果
  MooseHeads结果
  立方

UniPub
MooseHeads
Cube

推荐答案

有关添加字符串数组到ArrayList,而不是单个元素是什么。结果
像这样的:

What about adding the String arrays to the ArrayList instead of the individual elements.
Like this:

ArrayList<String[]> sInfo = new ArrayList<String[]>();
String line;
String[] saLineElements;
while ((line = br.readLine()) != null)
{
    //The information is split into segments and stored into the array
    saLineElements = line.split(";");
        sInfo.add(saLineElements);
}

然后,你可以遍历sInfo并使用第一个元素sInfo每个数组中的

Then you could loop through sInfo and use the first element in each array in sInfo.

这篇关于阅读文本文件,并在阵列填充数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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