How to Read file from a shared location Windows? (Java)

前端 未结 3 1649
隐瞒了意图╮
隐瞒了意图╮ 2020-12-06 13:40

Is there a way to read a file from a network shared location on windows?

Let\'s say for instance I\'ve this simple code that reads a text file called rea

相关标签:
3条回答
  • 2020-12-06 14:03

    In your bat file %~dp0 expands to the location of the bat file. You need that in your classpath so that java can find the class, though I don't know if it will choke on UNC path. For example:

    @echo off
    echo %~dp0
    

    would output

    \\host\share\dir
    

    EDIT: %dp0 will not work if there are spaces. This is what you need in your bat file:

    @echo off
    set p=%~dps0
    echo %p%
    java -classpath %p%\jarname classname
    pause
    
    0 讨论(0)
  • 2020-12-06 14:10

    When using a file path in Java, make sure to escape all \ correctly when giving the full path name.

    For example, if the file is on PC with IP (10.10.10.123) on a Shared folder called Addons then the full path will be:

    File f = new File ("\\\\10.10.10.123\\Addons\\readme.txt");
    

    Other than the full path, your code is throwing a ClassNotFound because you JAVA-CLASSPATH is not set properly.

    0 讨论(0)
  • 2020-12-06 14:15

    Two ways of doing.

    1) Map the shared path to a local drive.

    2) Else hard code the server path in new File('') as mentioned by Medopal.

    Something like new File("").getAbsolutePath() might help me get the base Folder when executed on a local system. Likewise there is no such way to programmatically find out the working base when executed on a shared location.

    0 讨论(0)
提交回复
热议问题