来自文本文件的数组-使用JAVA

来自文本文件的数组-使用JAVA,java,arrays,file,text,Java,Arrays,File,Text,我正在尝试从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

我正在尝试从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(); 
  }

}

您可以执行类似于
fileReader.nextLine().split(“,”)
的操作来创建表示行中逗号分隔项的字符串数组

或者您可以使用:


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

读取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();
        }
    }
}
您可以从

Scanner s=null获取相关的jar文件;
列表=新的ArrayList
试一试{
s=新的扫描仪(新的BufferedReader(新的文件阅读器(“xanadu.txt”));
而(s.hasNext()){
字符串str=s.nextLine();
列表.添加(str.split(“,”);
}
}最后{
如果(s!=null){
s、 close();
}
}
列表中会有一个stings数组

问题是什么?我不知道如何完成它“1.txt”中有什么内容。是否要将文件中的每一行存储为数组中的对象。我建议使用列表而不是数组发布工作代码不是答案,请提供解释并指出错误。(当然,发布一个不工作的代码也不是答案:)。顺便说一句,这个代码不工作,f.getInputStream,新字符串[100]代码工作正常,100不是神奇的数字,但假设我们甚至可以使用链表,但OP提到的数组如此..你是对的opencsv可以工作,但由于某种原因,该项目似乎失去了动力。如果OP很舒服地将第三方JAR添加到类路径中,他/她也可以考虑杰克逊:@ VIDY:是的,女士,真实的故事。
StringTokenizer st = new StringTokenizer(fileReader.nextLine(), ",");
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();
        }
    }
}
        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