开发者

File path anomaly accessing a file in build directory

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
    开发者_如何学Python [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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜