import java.io.*; import java.net.*; public class Downloader { public static void main(String[] args) throws FileNotFoundException { Downloader dowloader = new Downloader(); dowloader.downloadFiles("http://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded&a", "C:\\Users\\IP44", 1); } public static void downloadFiles(String strURL, String strPath, int buffSize) { try { URL connection = new URL(strURL); HttpURLConnection urlconn; urlconn = (HttpURLConnection) connection.openConnection(); urlconn.setRequestMethod("GET"); urlconn.connect(); InputStream in = null; in = urlconn.getInputStream(); OutputStream writer = new FileOutputStream(strPath); byte buffer[] = new byte[buffSize]; int c = in.read(buffer); while (c > 0) { writer.write(buffer, 0, c); c = in.read(buffer); } writer.flush(); writer.close(); in.close(); } catch (Exception e) { System.out.println(e); } } } 

Good day. I want to download a picture using the code taken from here . The following error occurs: java.io.FileNotFoundException C: \ Users \ IP44. I have given myself full permissions in the properties of the java folder, and also specified different directories. What can be done?

  • Is the file called IP44 ? and without expansion? - Alexey Shimansky
  • No, this is a directory. Should I give the name of the file I am downloading? - Kojer Defor 5:56
  • Of course. If, say, you have 100500 files in your directory, then how does the program need to know which file to work with? We need to make it clear specifically what we need to work with - Alexey Shimansky
  • Yes thank you. I threw an empty jpg into the directory, and the downloaded images overwritten it. How do you think, how can you implement downloading without overwriting? - Kojer Defor

1 answer 1

The java.io.FileNotFoundException error indicates that there is no file on the disk in which to write, or no rights to the directory. Judging by your code - the file is not assigned. That is, at least you need to create it in the directory, and add its type in the arguments of the method

 downloadFiles("http://imagehost.ru/icon.jng", "C:\\Users\\IP44\\test.jpg", 1); 

What if you need to download all sorts of pictures and manually create these files too stressful?

First: FileOutputStream can take an argument of type File as an input parameter.

Accordingly, we can already write something like this:

 File file = new File(strPath + "test.jpg"); OutputStream writer = new FileOutputStream(file); 

where test.jpg is a file name that can be generated randomly or as your heart desires.

but this? if it changes my memory, it will cause an error, again, if the file has not been previously created on the disk:

Secondly: File has a createNewFile method that creates a file on disk. That is, we check if there is a file on the disk, if not, then create it. It looks like this:

 if(!file.exists()) { //Создаем его. file.createNewFile(); } 

As a result, the downloadFiles method should be instead of the line

 OutputStream writer = new FileOutputStream(strPath); 

to write

 String myRandomName = "test.jpg"; // Генерируйте любое удобное имя вместо хардкорного File file = new File(strPath + myRandomName); if(!file.exists()) { //Создаем его. file.createNewFile(); } OutputStream writer = new FileOutputStream(file); 

Should work.


UPD : say in the comments that the error with new File will be only if the file cannot be opened for writing, createNewFile not necessary to call createNewFile .

  • Many thanks for the detailed answer. - Kojer Defor
  • "this will cause an error, again, if the file is not previously created on the disk" - an error will occur only if the file cannot be opened for writing, createNewFile not necessary to call createNewFile - zRrr
  • @zRrr exactly? umm I will add below ... - Alexey Shimansky
  • @Kojer Defor - they say, createNewFile is not necessary)) - Alexey Shimansky
  • @ Alexey Shimansky I checked. createNewFile can be used if you want to check the return value instead of catching an exception. Even with the seven there is a Files.newOutputStream with the ability to specify the parameters for opening a file. - zRrr