javafx getstylesheet().add(path)不允许我从maven项目的根目录读取

nhjlsmyf  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(227)

这是我的项目结构

DejaVuu
    -- src/main/java
    ---- views 
    ------ basetheme.css
    -- src/test/java
    -- src
    -- basetheme.css
    -- tempfile.css

目前我正在尝试读取我的css文件与

view.getStylesheets().add("basetheme.css");

但这行不通。但是,当我使用

view.getStylesheets().add("views/basetheme.css");

我需要basetheme.css在根目录下,因为那是我在更新后重写文件的地方。换句话说,读写位置应该相同。
下面是我调用以写入文件的代码:

public String writeToCSS(String css) throws IOException {
        String path_name = "basetheme.css"; // I need this to match read in for getStyleSheet().add
        File temp = new File(path_name); 
        temp.delete();
        temp.createNewFile();
        BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
        bw.write("some css");
        bw.close();

        String temp_url = temp.toURI().toString();
        return temp_url; 
    }

现在当你往里看的时候 writeToCSS 设置如下

String path_name = "basetheme.css";

以上文字很管用。这是项目的根。问题是现在我无法从这个位置将样式表读入javafx。
话虽如此,我还是试着把文件写到

String path_name = "/home/ming/eclipse-workspace/DejaVuu/views/basetheme.css";

但是,我在执行此操作时收到一个错误

java.io.IOException: No such file or directory
    java.io.UnixFileSystem.createFileExclusively(Native Method)
    java.io.File.createNewFile(File.java:1012)

可以 File 不用于写入mavenprojects中的包?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题