java - CSV 阅读器不注册更改

标签 java csv gradle javafx java-8

我正在制作一个应用程序,用户可以在其中跟踪他们吃的食物 - 这样做的一个要求是让他们能够向他们的投资组合添加新食物。

为了向他们显示他们创建的食物,我从原始文件夹中加载了一个 CSV 文件,以便在 TableView 中显示。

文件的加载是通过以下方法实现的:

public static ArrayList<Meals> load(String fileName) {
    ArrayList<Meals> listings = new ArrayList<Meals>();
    try{

        URL url = MealLoader.class.getResource("/raw/"+fileName);
        CSVReader reader = new CSVReader(new FileReader(new File(url.toURI())));

        String [] line;
        //skip the first row (column headers)
        reader.readNext();
        while ((line = reader.readNext()) != null) {
            int key = convertInt(line[0]);
            String type = line[1];
            String name = line[2];
            double calories = convertDouble(line[3]);
            double carbs = convertDouble(line[4]);
            double protein = convertDouble(line[5]);
            double fat = convertDouble(line[6]);
            double saturates = convertDouble(line[7]);
            double sugar = convertDouble(line[8]);
            double fibre = convertDouble(line[9]);
            double salt = convertDouble(line[10]);
            double b1 = convertDouble(line[11]);
            double b2 = convertDouble(line[12]);
            double b3 = convertDouble(line[13]);
            double b6 = convertDouble(line[14]);
            double b9 = convertDouble(line[15]);
            double b12 = convertDouble(line[16]);
            double d = convertDouble(line[17]);
            double iron = convertDouble(line[18]);

            Meals listing = new Meals(key, type, name, calories, carbs, protein, fat, saturates, sugar, fibre, salt, b1, b2, b3, b6, b9, b12, d, iron);
            System.out.println("Created meal");
            listings.add(listing);
        }
    } catch(IOException | URISyntaxException e){
        System.out.println("Failure! Something went wrong");
        e.printStackTrace();
    }
    System.out.println("Success! Number of loaded records: " + listings.size());
    return listings;
}

用户有能力创造新食物。

当他们这样做时,文件“meals.csv”已成功更改为具有额外的新行。

在用户输入新食物的名称和营养信息后,单击“确认”按钮后立即添加此行。

但是,当您返回 tableView 时,尽管再次加载了所有餐食,并且新记录出现在 CSV 文件中,但食物却不在那里。

我让它打印出通过上述“加载”方法加载了多少餐,并且在新记录之前和之后是相同的,即使 CSV 文件已更新。

当您关闭并重新启动应用程序时,就会出现餐食。

该代码取自此应用程序的旧版本,该应用程序未使用 Gradle 或 MVC 配置,并且使用 Java 13 而不是我现在使用的 Java 8 - 它之前运行良好,并且在两个版本之间查看,唯一的区别在于此加载函数内,事实上它从“raw”文件夹获取 .csv。

文件目录:

enter image description here

旧版本的代码:

public static ArrayList<Meals> load(String fileName) {
        ArrayList<Meals> listings = new ArrayList<Meals>();
        try{
            URL url = MealLoader.class.getResource(fileName);
            CSVReader reader = new CSVReader(new FileReader(new File(url.toURI()).getAbsolutePath()));
            String [] line;
            //skip the first row (column headers)
            reader.readNext();
            while ((line = reader.readNext()) != null) {
                int key = convertInt(line[0]);

(像以前一样继续)

构建.gradle:

/*
 * This build file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * user guide available at https://docs.gradle.org/4.1/userguide/tutorial_java_projects.html
 */

 buildscript {
     repositories {
         jcenter()
     }

     dependencies {
         classpath 'org.javafxports:jfxmobile-plugin:1.3.17'
     }
 }

 plugins {
     id 'java'
     id 'application'
     //id 'org.openjfx.javafxplugin' version '0.0.8'

 }

apply plugin:'org.javafxports.jfxmobile'

 mainClassName = "src.main.java.MainMenu"

 jfxmobile {

 javafxportsVersion = '8.60.12'

 android {
 applicationPackage = 'org.javafxports.ensemble'
  compileSdkVersion = '27'
  androidSdk = 'C:/Users/Luke/AppData/Local/Android/Sdk'

  packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
    }
 }

     ios {
         forceLinkClasses = ['ensemble.**.*']
     }
 }


repositories {
jcenter()
maven {
     url 'http://nexus.gluonhq.com/nexus/content/repositories/releases/'
 }
 mavenCentral()
}

dependencies {
    // This dependency is found on compile classpath of this component and consumers.
    compile 'com.google.guava:guava:22.0'

    // Use JUnit test framework
    testCompile 'junit:junit:4.12'
    testCompile 'org.testfx:testfx-junit:4.0.15-alpha'
    testCompile 'org.hamcrest:hamcrest-all:1.3'
      compile "com.opencsv:opencsv:4.0"
    compile 'com.gluonhq:charm:5.0.0'
    compile group: 'org.jfxtras', name: 'jmetro', version: '8.6.5'

}

allprojects {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
    }
}

我是否遗漏了一些东西,这意味着这种行为有一个简单的解释?

最佳答案

这与文件的检索方式有关。

使用:

URL url = MealLoader.class.getResource("/raw/"+fileName);
CSVReader reader = new CSVReader(new FileReader(new File(url.toURI())));

不起作用,必须用替代方案替换,例如:

CSVReader reader = new CSVReader(new FileReader("./src/main/resources/raw/"+fileName));

它还有一个优点,因为它更具可读性。

但是,为什么会出现这种情况对我来说是个谜,特别是“getResource”适用于第一个项目,而不是第二个项目。

关于java - CSV 阅读器不注册更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60095563/

相关文章:

java - 用户修改的复杂决策系统 - Java

java - JAVA 条件运算符构造的哪些方面是正式描述的,哪些方面是非正式描述的

java - 如何使用 setBorder 和 JFrame 在 Java 中为矩形添加边框

gradle - gradle:在一些嵌套子项目中共享变量

android - 安装Firebase和 map 后,应对 native fatal error 和应用程序崩溃

java - 将对象的信息传递给另一个 Activity

R:解压缩大型压缩 .csv 产生 "zip file is corrupt"警告

python - 从有限的 CSV 列集读取并作为行输出到特定的 JSON 模式

csv - Prolog,读取一个csv文件并做一个谓词。找到所有

android - 如何通过gradle任务同步Android项目?