简体   繁体   English

从我的jar中复制文件非常简单

[英]very simple copying a file from my jar

Ok, so I wanted to open a pdf file that I put it in my jar, so I needed to copy the file from the jar to my disk, and I did so by the following code: 好吧,所以我想打开一个pdf文件,我将它放在我的jar中,所以我需要将文件从jar复制到我的磁盘,我通过以下代码完成:

InputStream is = Jar.class.getResourceAsStream("images/lol.pdf");
OutputStream os = new FileOutputStream("753951741.pdf");
byte[] buffer = new byte[4096];
int length;
while ((length = is.read(buffer)) > 0)
    os.write(buffer, 0, length);
os.close();
is.close();

my question is:how do I control where the file is created? 我的问题是:如何控制文件的创建位置? When I execute the program It's created under C:/Users/Buba Thanks in advance :) 当我执行程序时它是在C:/ Users / Buba下创建的,在此先感谢:)

You can do like this: 你可以这样做:

File file = new File("c:/753951741.pdf");
OutputStream os = new FileOutputStream(file);

In this case the file will be created in C:/ 在这种情况下,文件将在C:/中创建

For more information about File in Java: http://docs.oracle.com/javase/6/docs/api/java/io/File.html#File(java.lang.String) 有关Java中的文件的更多信息: http//docs.oracle.com/javase/6/docs/api/java/io/File.html#File(java.lang.String)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM