0

I try to write a text to a file and read this text later. When I use FileWriter I become a NullPointerException? Is that a permission problem or ...? I also try the PrintWriter but I see the same Exception . This my code:

FileWriter fw = new FileWriter(new File("file.file"));
fw.write("XYZ");

best regards londi

Londi
  • 3
  • 3

2 Answers2

0

I guess your problem is that you use a relative file path, but that the origin of the relative path is not the one you think.

First of all, try to use an absolute path, that would be, on linux-like machines something like /home/me/myCode/myfile.txt or on windows something like c:/some/path/myfile.txt

Another thing you can do, in order to know what happens is print the origin.

File origin = new File("."); System.out.println(origin.getAbsolutePath());

Once you know where the origin is, you can see what you need in order to get to your file.

Hope it will help.

  • hi and thanks for your answer. Your Code works, but it does not create the file. When I use the createNewFile() methode, I become a IOException: EACCESS open failed (Permission denied) – Londi Oct 03 '14 at 19:06
  • check the folder in which you want to create the file, has your user the right to create new files ? – Francois Laroche Oct 05 '14 at 18:56
0

Sounds like a permission issue. On iOS your application lives within a security sandbox, so you cannot just randomly read and write files anywhere you want. You could either use File.createTempFile to create a temp file somewhere hidden you your sandbox where nothing else can see it, or use the native api to determine where to dump your files. The following example will give you a file reference to the Documents Directory folder:

        NSArray nsa = NSFileManager.defaultManager().URLsForDirectory$inDomains$(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask);
        NSURL nsu = (NSURL)nsa.getFirst();
        String snsu = nsu.getAbsoluteString() + "MyNewDocument.pdf";
        File newFile = new File(new URI(snsu));
Christo Smal
  • 615
  • 5
  • 16