【问题标题】:Array from a text file - using JAVA来自文本文件的数组 - 使用 JAVA
【发布时间】:2013-10-16 13:57:26
【问题描述】:

我正在尝试从 CSV 文件创建一个数组。我写了这个,但我不知道如何完成它。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

public class Java {

public static void main (String[] argv) throws IOException {

    File file = new File("1.txt");
    Scanner fileReader = new Scanner(file);
    System.out.printf(fileReader.nextLine());
    FileReader.close(); 
  }

}

【问题讨论】:

  • 问题是什么?
  • 不知道怎么写完
  • 你在 "1.txt" 中有什么。你想将文件中的每一行存储为数组中的对象吗??? .我建议使用 List 而不是数组

标签: java arrays file text


【解决方案1】:

在java中读取一个CSV文件

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import au.com.bytecode.opencsv.CSVReader;

public class CsvFileReader {
    public static void main(String[] args) {

        try {
            System.out.println("\n**** readLineByLineExample ****");
            String csvFilename = "C:/Users/hussain.a/Desktop/sample.csv";
            CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
            String[] col = null;
            while ((col = csvReader.readNext()) != null) 
            {
                System.out.println(col[0] );
                //System.out.println(col[0]);
            }
            csvReader.close();
        }
        catch(ArrayIndexOutOfBoundsException ae)
        {
            System.out.println(ae+" : error here");
        }catch (FileNotFoundException e) 
        {
            System.out.println("asd");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("");
            e.printStackTrace();
        }
    }
}

你可以从here获取相关的jar文件

【讨论】:

  • 你说得对,opencsv 可以工作,但由于某种原因,该项目似乎失去了动力。如果 OP 愿意将第三方 jars 添加到类路径中,他/她也可以考虑 Jackson:github.com/FasterXML/jackson-dataformat-csv
  • @Vidya:是的,女士,真实的故事
【解决方案2】:

您可以执行fileReader.nextLine().split(",") 之类的操作来创建一个字符串数组,该数组表示行中以逗号分隔的项目。

或者你可以使用StringTokenizer:

StringTokenizer st = new StringTokenizer(fileReader.nextLine(), ",");

然后遍历标记以从行中获取逗号分隔的项目。

【讨论】:

    【解决方案3】:
            Scanner s = null;
            List<String[]>  list  = new ArrayList<String[]>
            try {
                s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));
    
                while (s.hasNext()) {
                    String str = s.nextLine();
                    list.add(str .split(","));
                }
            } finally {
                if (s != null) {
                    s.close();
                }
            }
    
    list would have array of stings
    

    【讨论】:

      【解决方案4】:
      File f = new File(filepath)
      BufferedReader b = new Bufferedreader(f.getInputStream);
      String line = "";
      String[] myArray = new String[100];
      while((line = b.readLine())!=null)    // read file
      {
           myArray[count++] = line;   // store in an array
      }
      

      【讨论】:

      • 发布工作代码不是答案,请提供解释并指出错误。 (当然,发布不工作的代码也不是答案:))。
      • 顺便说一句,这段代码不起作用,f.getInputStream, new String[100]
      • 代码运行良好,100 不是幻数,但假设我们甚至可以使用链表,但 OP 提到了数组所以..
      猜你喜欢
      • 2014-12-25
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 2020-02-04
      • 1970-01-01
      • 2013-03-03
      • 2017-01-08
      • 1970-01-01
      相关资源
      最近更新 更多