简体   繁体   中英

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:

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 :)

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:/

For more information about File in Java: http://docs.oracle.com/javase/6/docs/api/java/io/File.html#File(java.lang.String)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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