How do I copy a text file from a jar into a file outside of the jar?

前端 未结 1 466
心在旅途
心在旅途 2021-01-03 08:07

Let\'s say I have a file called test.txt within the package \"com.test.io\" within my jar.

How would I go about writing a class which retrieves this text file and th

相关标签:
1条回答
  • 2021-01-03 09:13

    Assuming said jar is on your classpath:

    URL url = getClassLoader().getResource("com/test/io/test.txt");
    FileOutputStream output = new FileOutputStream("test.txt");
    InputStream input = url.openStream();
    byte [] buffer = new byte[4096];
    int bytesRead = input.read(buffer);
    while (bytesRead != -1) {
        output.write(buffer, 0, bytesRead);
        bytesRead = input.read(buffer);
    }
    output.close();
    input.close();
    
    0 讨论(0)
提交回复
热议问题