访问构建目录中的文件时出现文件路径异常

发布于 2024-11-08 02:17:50 字数 3553 浏览 0 评论 0原文

我正在创建一个 Java 类来检查文件是否存在:

package filedemo;
import java.io.File;

public class FileReadDemo {
    public static void main(String[] args){
        File f = new File("data/"+"hello.xml");

        System.out.println("file name="+f.getName());
        System.out.println("file path="+f.getPath());
        System.out.println("file canon path="+f.getCanonicalPath());
        System.out.println("file abs path="+f.getAbsolutePath());
        System.out.println("file parent="+f.getParent());
        System.out.println("file length="+f.length());
        System.out.println("is file="+f.isFile());
        System.out.println("file exists:"+f.exists());
    }
}

我的应用程序的文件夹结构是:

C:\code\java\javalearn\
                      |
                      |--src\filedemo\FileReadDemo.java
                      |--resources\hello.xml
                      |--lib
                      |--build

我创建了一个构建文件,如下所示。

<project name="filereaddemo" >
    <property file="local.properties" />
    <property name="dir.build" value="build"/>
    <property name="dir.classes" value="${dir.build}/classes"/>
    <property name="dir.data" value="${dir.classes}/data"/>
    <property name="dir.src" value="src"/>
    <property name="dir.lib" value="lib"/>
    <property name="dir.resources" value="resources"/>
    <property name="packagename" value="filedemo"/>
    <property name="mainclass" value="FileReadDemo"/>
    <path id="clientclasspath">
        <pathelement location="${dir.classes}/"/>
        <pathelement location="${dir.lib}"/>
    </path>

    <target name="makedirs">
        <mkdir dir="${dir.classes}"/>
        <mkdir dir="${dir.data}"/>
    </target>

    <target name="clean" description="Remove all generated files.">
        <delete dir="${dir.build}"/>
    </target>

    <target name="compile" depends="clean,makedirs" description="Compile all source code">
        <copy file="${dir.resources}/hello.xml" todir="${dir.classes}/data">
        </copy>
        <javac srcdir="${dir.src}" destdir="${dir.classes}"  verbose="yes">
            <classpath refid="clientclasspath"/>
            <include name="**/${mainclass}.java"/>
        </javac>
    </target>

    <target name="run" depends="compile">
        <java fork="true" classname="${packagename}.${mainclass}">
            <arg value="-verbose"/>

            <classpath refid="clientclasspath"/>
        </java>
    </target>
</project>

执行编译目标时,资源文件夹中的 XML 文件将复制到 build/classes/data 文件夹。 生成的构建文件夹具有以下结构:

build---classes---data---hello.xml
              |
              |---filedemo----FileReadDemo.class

下面是运行目标输出。

run:
     [java] file name=hello.xml
     [java] file path=data\hello.xml
     [java] file canon path=C:\code\java\javalearn\data\hello.xml
     [java] file abs path=C:\code\java\javalearn\data\hello.xml
     [java] file parent=data
     [java] file length=0
     [java] is file=false
     [java] file exists:false

我期望绝对路径是

C:\code\java\javalearn\build\classes\data\hello.xml

但在这里我得到了

C:\code\java\javalearn\data\hello.xml

为什么会发生这种情况?我需要做什么才能让 Java 类使用以下命令打开 File 对象 C:\code\java\javalearn\build\classes\data\hello.xml 路径?

I am creating a Java class to check if a file exists:

package filedemo;
import java.io.File;

public class FileReadDemo {
    public static void main(String[] args){
        File f = new File("data/"+"hello.xml");

        System.out.println("file name="+f.getName());
        System.out.println("file path="+f.getPath());
        System.out.println("file canon path="+f.getCanonicalPath());
        System.out.println("file abs path="+f.getAbsolutePath());
        System.out.println("file parent="+f.getParent());
        System.out.println("file length="+f.length());
        System.out.println("is file="+f.isFile());
        System.out.println("file exists:"+f.exists());
    }
}

The folder structure for my application is:

C:\code\java\javalearn\
                      |
                      |--src\filedemo\FileReadDemo.java
                      |--resources\hello.xml
                      |--lib
                      |--build

I created a buildfile as below.

<project name="filereaddemo" >
    <property file="local.properties" />
    <property name="dir.build" value="build"/>
    <property name="dir.classes" value="${dir.build}/classes"/>
    <property name="dir.data" value="${dir.classes}/data"/>
    <property name="dir.src" value="src"/>
    <property name="dir.lib" value="lib"/>
    <property name="dir.resources" value="resources"/>
    <property name="packagename" value="filedemo"/>
    <property name="mainclass" value="FileReadDemo"/>
    <path id="clientclasspath">
        <pathelement location="${dir.classes}/"/>
        <pathelement location="${dir.lib}"/>
    </path>

    <target name="makedirs">
        <mkdir dir="${dir.classes}"/>
        <mkdir dir="${dir.data}"/>
    </target>

    <target name="clean" description="Remove all generated files.">
        <delete dir="${dir.build}"/>
    </target>

    <target name="compile" depends="clean,makedirs" description="Compile all source code">
        <copy file="${dir.resources}/hello.xml" todir="${dir.classes}/data">
        </copy>
        <javac srcdir="${dir.src}" destdir="${dir.classes}"  verbose="yes">
            <classpath refid="clientclasspath"/>
            <include name="**/${mainclass}.java"/>
        </javac>
    </target>

    <target name="run" depends="compile">
        <java fork="true" classname="${packagename}.${mainclass}">
            <arg value="-verbose"/>

            <classpath refid="clientclasspath"/>
        </java>
    </target>
</project>

When compile target is executed, an XML file from resources folder is copied to build/classes/data folder.
The resulting build folder has this structure:

build---classes---data---hello.xml
              |
              |---filedemo----FileReadDemo.class

Below is the run target output.

run:
     [java] file name=hello.xml
     [java] file path=data\hello.xml
     [java] file canon path=C:\code\java\javalearn\data\hello.xml
     [java] file abs path=C:\code\java\javalearn\data\hello.xml
     [java] file parent=data
     [java] file length=0
     [java] is file=false
     [java] file exists:false

I expected the absolute path to be

C:\code\java\javalearn\build\classes\data\hello.xml

But here I am getting

C:\code\java\javalearn\data\hello.xml

Why does this happen? What do I have to do to get the Java class to open the File object using the
C:\code\java\javalearn\build\classes\data\hello.xml path?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声静候 2024-11-15 02:17:50

当您执行 run 目标时,您的脚本将在目录 C:\code\java\javalearn\ 中运行,

因为该目录是脚本的当前工作目录,并且脚本在不更改目录的情况下执行 java,因此它也是当前的工作目录。 java 的工作目录。

类路径只是告诉 java 在哪里查找类文件。它不会更改当前工作目录。因此,当您的项目显示“new File(data/hello.xml)”时,java 会在路径“./data/hello.xml”处查找文件,并翻译“.”。在目录“C:\code\java\javalearn”的路径中。这 ”。”每当您不从根目录启动路径时就暗示了这一点。

您需要在执行 java 之前更改目录、移动数据文件夹或更改代码中的字符串以指向正确的目录。

When you execute the run target, your script is being run in the directory C:\code\java\javalearn\

Since that directory is the current working directory for the script, and the script executes java without changing directories, it is also the current working directory for java.

The classpath simply tells java where to look for your class files. It does not change the current working directory. Hence, when your project says "new File(data/hello.xml)", java looks for the file at path "./data/hello.xml", and translates the "." in that path to the directory "C:\code\java\javalearn". The "." is implied anytime you do not start a path from the root directory.

You either need to change the directory before executing java, move your data folder, or change the String in your code to point to the correct directory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文