1

JARファイルのリソースにアクセスする方法を見てきましたか?テキストファイルをjarからjarの外部のファイルにコピーするにはどうすればよいですか?と他の多くの質問が実際に答えを得ることができませんでした。私がやろうとしているのres/CDC.txtは、jarファイル内のファイルの内容をjarファイルのどこかにコピーすることです。今、私のコンピューターでは動作しますが、別のコンピューターで試してみると、が表示されますFileNotFoundException。それで、なぜそれが私のもので機能するのかを理解しました。.;D:\myname\Java\JavaFilesすべてのJavaファイルがパッケージ内にある場所にCLASSPATHを設定しています。「JavaFiles」ディレクトリには「res/CDC.txt」もあります。したがって、アプリケーションを起動すると、最初にmyapp.jarが配置されている現在のディレクトリで「res / CDC.txt」がチェックされ、次に「JavaFiles」がチェックされて検出されます。他のコンピュータにはありません。それで、

public final class CT
{

//Other fields
private static CT ct;
private NTSystem nts;
private File f1;
private File f6;
private PrintWriter pw1;
private BufferedReader br1;
//Other fields

public static void main(String[] args)
{
    try
    {
        showMessage("Executing program...");
        ct = new CT();
        ct.init();
        ct.create();
        ct.insertData();
        //Other code
        showMessage("Program executed!");
    }
    catch(Exception e)
    {
        showMessage("An exception occured! Program closed.");
        e.printStackTrace();
        System.exit(0);
    }
}

private void init()
    throws IOException
{
    //Other initialization
    nts = new NTSystem();
    f1 = new File("C:\\Users\\" + nts.getName() + "\\blahblah");
    f6 = new File("res\\CDC.txt");
    br1 = new BufferedReader(new FileReader(f6));
    //Other initialization
    showMessage("Initialized");
}

private void create()
    throws IOException
{
    //Makes sure file/dir exists, etc

    pw1 = new PrintWriter(new BufferedWriter(new FileWriter(f1)), true);
    //Other Stuff

    showMessage("Created");
}

private void insertData()
    throws IOException
{
    String line = br1.readLine();
    while(line != null)
    {
        pw1.println(line);
        line = br1.readLine();
    }

    //Other stuff
    showMessage("Data inserted");
}

private static void showMessage(String msg)
{
    System.out.println(msg);
}

}

に変更しました

public final class CT
{

//Other fields
private static CT ct;
private NTSystem nts;
private byte[] buffer;
private File f1;
private URL url1;
private FileOutputStream fos1;
private InputStream is1;
//Other fields

public static void main(String[] args)
{
    try
    {
        showMessage("Executing program...");
        ct = new CT();
        ct.init();
        ct.create();
        ct.insertData();
        //Other code
        showMessage("Program executed!");
    }
    catch(Exception e)
    {
        showMessage("An exception occured! Program closed.");
        e.printStackTrace();
        System.exit(0);
    }
}

private void init()
    throws IOException
{
    //Other initialization
    nts = new NTSystem();
    buffer = new byte[4096];
    f1 = new File("C:\\Users\\" + nts.getName() + "\\blahblah");
    url1 = getClass().getClassLoader.getResource("res\\CDC.txt"); //Also tried url1 = ct.getClass().getClassLoader.getResource("res\\CDC.txt"); or url1 = this.getClass().getClassLoader.getResource("res\\CDC.txt"); or url1 = CT.getClass().getClassLoader.getResource("res\\CDC.txt");
    is1 = url1.openStream();
    //Other initialization
    showMessage("Initialized");
}

private void create()
    throws IOException
{
    //Makes sure file/dir exists, etc

    pw1 = new PrintWriter(new BufferedWriter(new FileWriter(f1)), true);
    //Other Stuff

    showMessage("Created");
}

private void insertData()
    throws IOException
{
    int read = is1.read(buffer);
    while(line != null)
    {
        fos1.write(buffer, 0, read);
        read = is1.read(buffer);
    }

    //Other stuff
    showMessage("Data inserted");
}

private static void showMessage(String msg)
{
    System.out.println(msg);
}

}

そして今回はいつもNullPointerException。では、jar内にあるフォルダーとファイルを読み取る方法は?

ありがとう

4

1 に答える 1

0

getSystemResourceAsStream()を使用して、jar内のファイルからコンテンツを読み取る必要があります。

もちろん、これは、ファイルが実際に他のユーザーのコンピューターのクラスパス上にあることを前提としています。

于 2012-08-16T15:20:45.797 に答える