简体   繁体   English

Java用定界符读取文本文件并将其推入数组

[英]Java reading text file with delimiter and pushing into an array

I am reading a file which has a delimiter between elements. 我正在读取一个在元素之间有定界符的文件。 I am trying to put each element into a new index of an array. 我正在尝试将每个元素放入数组的新索引中。 This does not seem to work properly, everything seems to end up on one index of the array. 这似乎无法正常工作,一切似乎都结束于数组的一个索引上。 Below is a sample text file and my code. 下面是一个示例文本文件和我的代码。

Textfile 文本文件

%User  %James %Fields %Will Smith %sBel %James %Fields %Will Smith %sBel %James %Fields %Will Smith %sBel%James %Fields %Will Smith %sBel %James %Fields %Will Smith %sBel %James %Fields %Will Smith %sBel %James %Fields %Will Smith %sBel %James %Fields %Will Smith %sBel %James %Fields %Will Smith %sBel %James %Fields %Will Smith %sBel %James %Fields %Will Smith %sBel %James %Fields %Will Smith %sBel %James %Fields %Will Smith %sBel %James %Fields %Will Smith %sBel %James %Fields %Will Smith %sBel %James %Fields %Will Smith %sBel %James %Fields %Will Smith %sBel

Code

final InputStream i = getResources().openRawResource(R.raw.users);
final Scanner s = new Scanner(i);
try
{
    while (s.hasNextLine())
    {
        String d = s.nextLine();
        String test;

        test = values[1];           
        userTextArea.append(test);
        test = "";
    }
}
final InputStream i = getResources().openRawResource(R.raw.users);
final Scanner s = new Scanner(i);
try
{
     while (s.hasNextLine())
    {
        String d = s.nextLine();
        StringTokenizer st = new StringTokenizer(d,"%");
        String[] myStringArray = new String[st.countTokens()]

        int index = 0;
        while(st.hasMoreTokens()) {
            myStringArray[index] = st.nextElement();
            index ++;
        }
    }
} catch (Exception e) {
....
}

You have to use the split function. 您必须使用分割功能。

String tmp = "%hallo %world";
String [] strArray = tmp.split("%");
System.out.print(strArray[0]); //prints hallo 

An array is a container object that holds a fixed number of values stored in unique indexes (positions). 数组是一个容器对象,其中包含固定数量的存储在唯一索引(位置)中的值。

Causes 原因

everything seems to end up on one index of the array 一切似乎都以数组的一个索引结尾

It will do, for two reasons: 这样做有两个原因:

  1. The file you are reading in has no new-line characters. 您正在读取的文件没有换行符。 It will therefore assign all of the file contents to the variable " d " when you call nextLine() . 因此,当您调用nextLine()时,它将把所有文件内容分配给变量“ d ”。

      String d = s.nextLine(); 

    To clarify, at this point in your program, " d " holds your whole text file. 为了清楚起见,在您的程序中,“ d ”保存您的整个文本文件。

  2. Even if it did have new-line characters, you still wouldn't be reading anything into an array. 即使它确实包含换行符,您仍然不会将任何内容读入数组。 After assigning the string to the variable " d ". 将字符串分配给变量“ d ”后。 You are not doing anything with it. 您对此没有做任何事情。

Suggestions 意见建议

One way of doing this would be to: 一种方法是:

  • Read the file as you are, as a single line and then use the String split() function to split elements, this function returns an array so you need to assign it to a String[] array variable. 逐行读取文件,然后使用String split()函数拆分元素,此函数返回一个数组,因此您需要将其分配给String []数组变量。 Omit the while loop as you only need to read a single line. 省略while循环,因为您只需要读取一行。 After this, loop through the string array and append the values to the text area: 之后,遍历字符串数组并将值附加到文本区域:

      String d = s.nextLine(); String[] values = d.split("%"); for(String value: values) { userTextArea.append(value.trim()); } 

    When appending, don't forget to trim the string because your file contains spaces between elements. 追加时,不要忘记修剪字符串,因为文件中的元素之间包含空格。 The String trim() function will remove these. 字符串trim()函数将删除这些。

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

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