如何使用 JAVA 读取 CSV 文件

我有一个问题,但我还没有找到任何解决方案。以下问题:我必须读取一个 CSV 文件,该文件必须如下所示:


First Name,Second Name,Age,


Lucas,Miller,17,


Bob,Jefferson,55,


Andrew,Washington,31,

作业是用JAVA读取这个CSV文件并显示如下:


First Name: Lucas


Second Name: Miller


Age: 17

属性名称并不总是相同,因此也可能是:


Street,Number,Postal Code,ID,


Schoolstreet,93,20000,364236492,

(“,”必须替换为“;”)


此外,文件地址并不总是相同。我已经有了视图等。我只需要模型。


感谢您的帮助。:))


我在 Controller 中已经有一个 FileChooser 类,它返回一个 URI。


神不在的星期二
浏览 91回答 2
2回答

撒科打诨

如果您的 CSV 文件始终包含指示表列名称的标题行,那么只需捕获该行并将其拆分,以便将这些列名称放入字符串数组(或集合,或其他)中。该数组的长度决定了每个记录数据行预期可用的数据量。一旦你有了列名,事情就变得相对容易了。如何获取 CSV 文件路径及其格式类型显然取决于您,但以下是如何执行手头任务的一般概念:public static void readCsvToConsole(String csvFilePath, String csvDelimiter) {&nbsp; &nbsp; String line;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // To hold each valid data line.&nbsp; &nbsp; String[] columnNames = new String[0];&nbsp; &nbsp;// To hold Header names.&nbsp; &nbsp; int dataLineCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Count the file lines.&nbsp; &nbsp; StringBuilder sb = new StringBuilder(); // Used to build the output String.&nbsp; &nbsp; String ls = System.lineSeparator();&nbsp; &nbsp; &nbsp;// Use System Line Seperator for output.&nbsp; &nbsp; // 'Try With Resources' to auto-close the reader&nbsp; &nbsp; try (BufferedReader br = new BufferedReader(new FileReader(csvFilePath))) {&nbsp; &nbsp; &nbsp; &nbsp; while ((line = br.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Skip Blank Lines (if any).&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (line.trim().equals("")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataLineCount++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Deal with the Header Line. Line 1 in most CSV files is the Header Line.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dataLineCount == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* The Regular Expression used in the String#split()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;method handles any delimiter/spacing situation.*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; columnNames = line.split("\\s{0,}" + csvDelimiter + "\\s{0,}");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp;// Don't process this line anymore. Continue loop.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Split the file data line into its respective columnar slot.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] lineParts = line.split("\\s{0,}" + csvDelimiter + "\\s{0,}");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* Iterate through the Column Names and buld a String&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;using the column names and its' respective data along&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;with a line break after each Column/Data line.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < columnNames.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(columnNames[i]).append(": ").append(lineParts[i]).append(ls);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Display the data record in Console.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(sb.toString());&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* Clear the StringBuilder object to prepare for&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a new string creation.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.delete(0, sb.capacity());&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Trap these Exceptions&nbsp; &nbsp; catch (FileNotFoundException ex) {&nbsp; &nbsp; &nbsp; &nbsp; System.err.println(ex.getMessage());&nbsp; &nbsp; }&nbsp; &nbsp; catch (IOException ex) {&nbsp; &nbsp; &nbsp; &nbsp; System.err.println(ex.getMessage());&nbsp; &nbsp; }}使用这种方法,您可以拥有 1 到数千个列,这并不重要(并不是说您在任何给定记录中都会有数千个数据列,但是嘿....您永远不知道...哈哈)。并使用此方法:// Read CSV To Console Window.readCsvToConsole("test.csv", ",");

慕侠2389804

如果始终有 3 个属性,我将读取 csv 的第一行并在具有三个字段的对象中设置值:attribute1、attribute2 和 attribute3。我将创建另一个类来保存三个值并读取之后的所有行,每次创建一个新实例并在数组列表中读取它们。要打印,我只需每次将属性类中的值与每组值一起打印即可。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java