1

I have a byte array, i want to create an image file(bmp file) of byte array. I create an images folder in src (my path is src/images/test.bmp). my code is in below, in

OutputStream stream = new FileOutputStream(file);

i get error. what is my problem? How can i solve this?

public static void saveImage() {
    String s="........................";
    byte[] dataCustImg = Base64.decode(s.getBytes());

    File file = new File("/images/test.bmp");
    if (file.exists()) {
        file.delete();
    }
    file = new File("/images/test.bmp");
    file.mkdirs();
    try {
        OutputStream stream = new FileOutputStream(file);

        stream.write(dataCustImg);
        stream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

Error:

java.io.FileNotFoundException: \images\test.bmp (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Fahim
  • 384
  • 5
  • 20
  • I can solve my problem with this http://stackoverflow.com/questions/6142901/how-to-create-a-file-in-a-directory-in-java link. but now i want to know how can solve my code with @EJP answer. I mean how create file and assign to fileoutputStream parameter ? – Fahim May 23 '16 at 06:22

4 Answers4

2
File file = new File("/images/test.bmp");

OK.

if (file.exists()) {
    file.delete();
}

Redundant. Remove. new FileOutputStream() will create a new file.

file = new File("/images/test.bmp");

Redundant. Remove. It already is a File with this name.

file.mkdirs();

The problem is here. Change to

file.getParentFile().mkdirs();

You are creating a directory called "/images/test.bmp", rather than just ensuring that "/images" exists. This will cause new FileOutputStream() to fail with an access permission, as you can't overwrite a directory with a file.

try {
    OutputStream stream = new FileOutputStream(file);

Now carry on. Note that you will first have to delete the directory "/images/test.bmp", manually.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks for reply, but when i have images folder in src path, now i want to save test.bmp in images folder, i should create file with File file=new File("/images/"), this is true? if yes how can save in test.bmp?how can set this in fileoutputstream? Sorry if my English is bad – Fahim May 23 '16 at 05:57
  • please answer to my questions. Thanks a lot – Fahim May 23 '16 at 06:20
  • You don't need to do anything other that what I've stated above. I didn't say anything about `new File("/images")`. I don't know where you got that from, or what part of my answer you don't understand. – user207421 May 24 '16 at 05:34
0

Here when you are calling mkdir then it is creating test.bmp as a directory not as a file, SO you have to first create directory and then you can create file. see the below code.

        File dir = new File("/images/");
        dir.mkdirs();
        file = new File("/images/test.bmp");
        file.createNewFile();
Naveen Ramawat
  • 1,425
  • 1
  • 15
  • 27
0

The reason for the exception is that you actually create a directory with the path /images/test.bmp

file = new File("/images/test.bmp");
file.mkdirs();

and later you want to open a file

OutputStream stream = new FileOutputStream(file);

If you want to ensure that the directory /images exist before you create the file you should use

File dir = new File("/images/");
dir.mkdirs();

The explicit delete before writing to the file isn't necessary, as the file would be overwritten by default.

Find a small working snippet below.

// create the directory if not exist
File dir = new File("/images/");
dir.mkdirs();
// create a new file or overwrite an existing one
File file = new File("/images/test.bmp");
try (OutputStream os = new FileOutputStream(file)) {
    os.write((int) System.currentTimeMillis());
}
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
-1
public static void saveImage() {
    String s="........................";
    byte[] dataCustImg = Base64.decode(s.getBytes());

    File file = new File("/images/test.bmp");
    if(!file.getParentFile().exists()) {
      file.getParentFile().mkdirs();
    }
    if(!file.exists()) {
      try {
        file.createNewFile();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    // because stream.write(dataCustImg) will overwrite the file if the file has already existed.
    try {
        OutputStream stream = new FileOutputStream(file);

        stream.write(dataCustImg);
        stream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
kgym
  • 29
  • 3
  • @Fahim, I have edit the code above and tested it. It works in my computer. Suggest to use File.separator instead of "/" and "\". – kgym May 24 '16 at 05:24
  • The `exists()/createNewFile()` block is a complete and utter waste of time. The operating system has to do exactly that when `new FileOutputStream()` is called. You're forcing it to do it all twice. The existence check on the parent directory is pointless and wasteful as well, as `mkdirs()` already does it inside the OS. – user207421 May 24 '16 at 05:35
  • @EJP Now, I see. Thank you! – kgym May 24 '16 at 05:40