Java - 将 txt 文件的部分添加到数组

标签 java arrays string csv

我已经导入了一个 .csv 数据库文件,其中列出了程序的用户,以及以下形式的其他信息:UserName、Password、PropertyName、EstimatedValue。

我已经想出了如何获取用户名,但它只会读取数据库中的最后一个用户名,而不会读取其他用户名。将不胜感激。

import java.util.*;
import java.io.*;

public class readCSV
{
    String[] userData;

    public void checkLogin() throws IOException
    {
        try
        {
            File file = new File("C:/Users/Sean/Documents/Programming assigment/Users.csv");
            BufferedReader bufRdr  = new BufferedReader(new FileReader(file));      
            String lineRead = bufRdr.readLine();
            while(lineRead != null)
            {
                this.userData = lineRead.split(",");
                lineRead = bufRdr.readLine();
            }
            bufRdr.close();
        }
        catch(Exception er){
            System.out.print(er); 
            System.exit(0);
        }
    }


}

最佳答案

违规行是这样的:

this.userData = lineRead.split(",");

您应该将其放入某个集合中,例如一个列表

final List<String[]> userData = new LinkedList<String[]> ();

try
    {
        File file = new File("C:/Users/Sean/Documents/Programming assigment/Users.csv");
        BufferedReader bufRdr  = new BufferedReader(new FileReader(file));      
        String lineRead = bufRdr.readLine();
        while(lineRead != null)
        {
            this.userData.add (lineRead.split(","));
        }
        bufRdr.close();
    }
    catch(Exception er){
        System.out.print(er); 
        System.exit(0);
    }

关于Java - 将 txt 文件的部分添加到数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13516709/

相关文章:

java - 如果列表中有字符串(在编译时给出): Is HashSet the fastest solution?

java - 在大字符串中删除字符串模板直到 '\n' - Java

java - Android Gradle 项目中包含 Apache HttpComponents 的问题

c - 限制输入字符串字符

c++ - 如何将数组大小传递给 C++ 中的函数

string - Haskell:将输入字符串直接转换为列表

java - 序列化 HttpSession 对象

java - 异常 : java. io.UTFDataFormatException:服务器/客户端

arrays - 多维数组如何在PowerShell中工作?

string - 从golang中的字节数组解压gzip