Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将CSV文件转换为列表<;列表<;字符串>&燃气轮机;_Java_List_Csv - Fatal编程技术网

Java 将CSV文件转换为列表<;列表<;字符串>&燃气轮机;

Java 将CSV文件转换为列表<;列表<;字符串>&燃气轮机;,java,list,csv,Java,List,Csv,我的本地服务器中有一个CSV文件,我想读取该文件并将其转换为列表。我已经完成了与服务器的连接部分,现在我想读取这个csv文件并转换它 有谁能给我举一个这种转换的例子,因为大多数例子都说明了如何将它转换成多维数组?这是一个非常简单的例子,说明了您要查找的内容,ArrayList LinesRays是每行的项目列表 public class FileReader { public static void main(String args[]) { ArrayList<List&

我的本地服务器中有一个CSV文件,我想读取该文件并将其转换为
列表
。我已经完成了与服务器的连接部分,现在我想读取这个csv文件并转换它


有谁能给我举一个这种转换的例子,因为大多数例子都说明了如何将它转换成多维数组?

这是一个非常简单的例子,说明了您要查找的内容,
ArrayList LinesRays
是每行的项目列表

public class FileReader
{
  public static void main(String args[])
  {
    ArrayList<List<String>> linesArrays = new ArrayList<List<String>>();

    FileInputStream fileInputStream = null;
    BufferedReader bufferedReader = null;
    try
    {
      fileInputStream = new FileInputStream("d:\\test.csv");
      bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));

      String line = bufferedReader.readLine();
      while (line != null)
      {
        line = bufferedReader.readLine();
        if (line != null)
        {
          List<String> items = Arrays.asList(line.split(","));
          linesArrays.add(items);
        }
      }
      for (List<String> stringList : linesArrays)
      {
        System.out.println("items :" + stringList.size());
      }
    }
    catch (FileNotFoundException fileNotFoundException)
    {
      //todo Deal with exception
      fileNotFoundException.printStackTrace();
    }
    catch (IOException iOException)
    {
      //todo Deal with exception
      iOException.printStackTrace();

    }
    finally
    {
      try
      {
        if (bufferedReader != null)
        {
          bufferedReader.close();
        }
        if (fileInputStream != null)
        {
          fileInputStream.close();
        }
      }
      catch (IOException ex)
      {
        // not much you can do about this one
      }
    }
  }

}
公共类文件读取器
{
公共静态void main(字符串参数[])
{
ArrayList LinesArray=新的ArrayList();
FileInputStream FileInputStream=null;
BufferedReader BufferedReader=null;
尝试
{
fileInputStream=新的fileInputStream(“d:\\test.csv”);
bufferedReader=新的bufferedReader(新的InputStreamReader(fileInputStream));
String line=bufferedReader.readLine();
while(行!=null)
{
line=bufferedReader.readLine();
如果(行!=null)
{
列表项=数组.asList(第.split行(“,”);
linesArrays.add(项目);
}
}
对于(列表字符串列表:LinesArray)
{
System.out.println(“items:+stringList.size());
}
}
捕获(FileNotFoundException FileNotFoundException)
{
//处理异常的todo
fileNotFoundException.printStackTrace();
}
捕获(IOException IOException)
{
//处理异常的todo
iOException.printStackTrace();
}
最后
{
尝试
{
if(bufferedReader!=null)
{
bufferedReader.close();
}
if(fileInputStream!=null)
{
fileInputStream.close();
}
}
捕获(IOEX异常)
{
//这件事你无能为力
}
}
}
}

对于每个原始数据,将逗号分隔的字符串作为列表,然后将所有原始数据作为列表。您可以使用Jackson处理CSV文件。链接的答案是关于逗号分隔的字符串,而这个问题是关于不同的CSV文件(CSV文件具有在值中使用逗号的机制)。这个答案出现在谷歌上,所以我在这里发布了一条警告,尽管这个答案已经关闭