Reputation: 517
This is probably a silly question, but I'm pretty new to Java and I can't figure it out.
Basically, I'm trying to download some files from a website and I want to save them to a particular folder (rather than the default of the same folder that my Java file is located in). How can I do this?
I've been using FileReader
, BufferedReader
, BufferedInputStream
, and FileOutputStream classes
.
Thanks :)
Upvotes: 13
Views: 109723
Reputation: 425328
Java is pretty friendly with IO. Try something like this:
File file = new File("/some/absolute/path/myfile.ext");
OutputStream out = new FileOutputStream(file);
// Write your data
out.close();
Notes:
/
, it will be relative to your "current" directoryBufferedWriter
easier: BufferedWriter writer = new BufferedWriter(new FileWriter(file));
. It has newLine()
and write(String)
methodsUpvotes: 19
Reputation: 162851
When you insantiate your FileOutputStream
you can pass an absolute path to the constructor. Like this:
FileOutputStream os = new FileOutputStream("/path/to/file.txt");
Upvotes: 3