0

I try to write byte data to directory I use the following code but i get this

Exception in thread "main" java.io.FileNotFoundException: C:\file (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
    at com.innvo.domain.App.main(App.java:17)

My code:

public static void main(String[] args) throws IOException {

    File dir = new File("C:\\file");
     if (dir.isDirectory())
     {
        String  x="new text string";
        File serverFile = new File(dir.getAbsolutePath());
       BufferedOutputStream stream = new BufferedOutputStream(
             new FileOutputStream(serverFile));
       System.out.println(x.getBytes());
       stream.close();
   }else {
    System.out.println("not");
  }

     }
ali ibrahim
  • 95
  • 1
  • 9
  • Looks like the user that executes the app doesn't have the right permissions to create the file. Try writing into a folder where you have such permissions. – Luiggi Mendoza Feb 03 '15 at 21:01
  • Exact duplicate of [write byte data to directory (Access is denied)](http://stackoverflow.com/questions/28306367/write-byte-data-to-directory-access-is-denied) Do not create duplicates, especially if your previous question was already a duplicate. – tnw Feb 03 '15 at 21:03
  • Ok how fix this issue? – ali ibrahim Feb 03 '15 at 21:05

1 Answers1

3

serverFile is a directory. FileOutputStream does not accept directories. You cannot write to a directory like to a file.
Use something like `

File serverFile = new File(dir,"mynewfile.txt");
Gren
  • 1,850
  • 1
  • 11
  • 16