1

I am trying to load image file from file directory. Then, I want to convert the file object to string object. Unfortunately, I keep receive this error messages. How can I resolve it?

java.io.FileNotFoundException: E:\workspace\sesaja\Images (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at test.Test1.main(Test1.java:29)

Thi is my overall code

 public class Test1 {

     public static void main(String args[]){        

    String s = System.getProperty("user.dir") + System.getProperty("file.separator")+ "Images";
    File f = new File (s);

    FileInputStream fis = null;
    String str = "";

     try {
            fis = new FileInputStream(f);
            int content;
            while ((content = fis.read()) != -1) {
                // convert to char and display it
                str += (char) content;
            }

            System.out.println("After reading file");
            System.out.println(str);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }      
    }           

} 
Belle
  • 71
  • 1
  • 2
  • 13

1 Answers1

2

Concatenate desired file name at the end of this line:

String s = System.getProperty("user.dir") +
System.getProperty("file.separator")+ "Images" + fileName;

It seems you are trying to read data from a directory, which is not logically correct.
Also using FileInputStream in order to read characters (not data) is not recommended. You may use a BufferedReader instead.
Also for getting name of files inside a directory, you may read this: Read all files in a folder

Community
  • 1
  • 1
Alireza Mohamadi
  • 511
  • 5
  • 19
  • The filename means my file image name? "Images" is the name of folder images and I am trying to read all the images from that folder, not only to read one image. Can you show me how to use Bufferedreader? – Belle May 10 '16 at 08:39