在更新程序中引用文件(Java)

在更新程序中引用文件(Java),java,file-io,Java,File Io,我正在写一个更新程序。我有以下代码: package main; import java.io.*; import java.net.*; import java.util.*; import java.util.regex.Pattern; import java.lang.*; import static java.lang.System.out; public class UpdaterCore { public static void main(String args[]) throw

我正在写一个更新程序。我有以下代码:

package main;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.Pattern;
import java.lang.*;

import static java.lang.System.out;
public class UpdaterCore
{
public static void main(String args[]) throws IOException
{
    java.io.BufferedInputStream inv = new java.io.BufferedInputStream(new

                    java.net.URL("http://unicombox.tk/update/nv").openStream());
                    java.io.FileOutputStream fosv = new java.io.FileOutputStream("nv");
                    java.io.BufferedOutputStream boutv = new BufferedOutputStream(fosv,1024);
                    byte data[] = new byte[1024];
                    while(inv.read(data,0,1024)>=0)
                    {
                    boutv.write(data);
                    }
                    boutv.close();
                    inv.close();
                    //end version download

                    Scanner VersionReader= new Scanner(new File ("v")).useDelimiter(",");
                    int currentVersion= VersionReader.nextInt();
                    VersionReader.close();

                    Scanner NewVersionReader= new Scanner(new File ("nv")).useDelimiter(",");
                    int newVersion= NewVersionReader.nextInt();
                    NewVersionReader.close();



                    if (newVersion>currentVersion){


                    java.io.BufferedInputStream in = new java.io.BufferedInputStream(new

                                    java.net.URL("http://unicombox.tk/update/update.zip").openStream());
                    java.io.FileOutputStream fos = new java.io.FileOutputStream("update.zip");
                    java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
                    byte data1[] = new byte[1024];
                    while(in.read(data1,0,1024)>=0)
                    {
                            bout.write(data1);
                    }
                    bout.close();
                    in.close();
                    out.println("Update successfully downloaded!");

                    }

                    else{
                            out.println("You have the latest version!");
                    }








}
}
它从服务器获取新版本,然后将其与当前版本进行比较。如果新版本大于当前版本,它将下载更新

我有一个大问题。我的程序永远找不到文件“v”和“nv”! “v”和“nv”与编译后的jar在同一个文件夹中,但是我得到了一个FileNotFound。
我做错了什么?

获取当前目录(放置.jar文件的目录)的路径,如下所示:

// import java.io.*;
// import java.net.URLDecoder;
// throws java.io.UnsupportedEncodingException 

    String path = UpdaterCore.class.getProtectionDomain().getCodeSource().getLocation().getPath();
    String decodedPath = URLDecoder.decode(path, "UTF-8");
    System.out.println(decodedPath);
然后像这样创建文件实例

new File (decodedPath + File.separatorChar + "v")

您可能正在从另一个目录运行该程序,一级以上

您可以在这些java.io.File-s上使用getAbsolutePath()来找出您真正想要读取的文件路径


或者使用Marek Sebera的解决方案,也可以。

非常感谢!我会投你一票。。。但我没有足够的声誉,这是你的问题。所以你可以接受我的回答是正确的。每个答案你只能投一次赞成票。这就是它的工作方式不。我在与文件相同的目录中运行jar。不过,还是要谢谢你!顺便说一句,我使用的是基于UNIX的Mac。