0

I am trying to write some message to text file. The text file is in the server path. I am able to read content from that file. But i am unable to write content to that file. I am getting FileNotFoundException: \wastServer\apps\LogPath\message.txt (Access Denied).

Note: File has a read and write permissions.

But where i am doing wrong. Please find my code below.

Code:

    String FilePath = "\\\\wastServer\\apps\\LogPath\\message.txt";

    try {
        File fo = new File(FilePath);
        FileWriter fw=new FileWriter(fo); 
        BufferedWriter bw=new BufferedWriter(fw); 
        bw.write("Hello World"); 
        bw.flush(); 
        bw.close(); 
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Please help me on this?

Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79
Ranganath
  • 1
  • 1
  • What platform is this running on? – Karthik T Jan 30 '13 at 04:48
  • @theunlucky: how do you know this to be a fact? To Ranganath: please print the entire stack trace. – Hovercraft Full Of Eels Jan 30 '13 at 04:50
  • 1
    Is the file open by another process? You `try-catch` is dangerous as it's not actually attempting to close the file should a write file (even though the file was opened) – MadProgrammer Jan 30 '13 at 04:52
  • [Reference](http://stackoverflow.com/questions/4281143/javaio-filenotfoundexception-access-is-denied) – thar45 Jan 30 '13 at 04:59
  • Java platform. Sample java class to write some content into that file. File is on WAS logs server path. – Ranganath Jan 30 '13 at 11:43
  • java.io.FileNotFoundException: \\wastServer\apps\LogPath\message.txt (Access is denied.) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.(FileOutputStream.java:190) at java.io.FileOutputStream.(FileOutputStream.java:142) at java.io.FileWriter.(FileWriter.java:84) at FileCheck.main(FileCheck.java:15) – Ranganath Jan 30 '13 at 11:58

3 Answers3

1

Please check whether you can access the apps and LogPath directory.

Type these on Run (Windows Key + R)

\\\\wastServer\\apps\\

\\\\wastServer\\apps\\LogPath\\

And see whether you can access those directories from the machine and user you are executing the above code.

shazin
  • 21,379
  • 3
  • 54
  • 71
0

You don't have write access to the share, one of the directories, or the file itself. Possibly the file is already open.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

After this line

 File fo = new File(FilePath);

try to print the absolute path

System.out.println( fo.getAbsolutePath() );

And then check whether the file exists in that location, instead of directly checking at

\\\\wastServer\\apps\\LogPath\\message.txt

So , you will know, where the compiler is searching for the file.

madhairsilence
  • 3,787
  • 2
  • 35
  • 76
  • It's already an absolute path. The file doesn't have to exist in that location. The compiler isn't searching for the file. Nobody is searching for the file. His program is destructively opening the file for output. Not an answer. – user207421 Jan 30 '13 at 07:35