0

The error I am getting:

java.io.FileNotFoundException: /Users/coryallen/Desktop/input.txt (Operation not permitted)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:112)
at java.base/java.io.FileReader.<init>(FileReader.java:60)
at Main.main(Main.java:41)

Here is my code

List<String> fileContents = new ArrayList<>();
    try {
        FileReader fReader = new FileReader(fileName);
        BufferedReader fBuff = new BufferedReader(fReader);
        while((fBuff.readLine()).contains("name")) {
            System.out.println(fBuff.readLine());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

I tried a few different methods for reading this file and I cannot figure out why this is happening. The extensions I have used are .rtf and .txt, thinking maybe it was an issue with the file itself. The file just contains the following:

name John
weapon Sword 
madmaxx01
  • 61
  • 6
  • mac or windows OS? – VeKe Jul 19 '21 at 14:23
  • 3
    Seems like its file access issue – VeKe Jul 19 '21 at 14:24
  • 1
    looks like the jvm process gets started by a user that does not have access to read the file. if you are on a unix system can you try "sudo chmod 777 /Users/coryallen/Desktop/input.txt" to see if the program runs when the ffile is accessible by all users and groups? – meaningqo Jul 19 '21 at 14:26
  • Hi this is mac @VeKe – madmaxx01 Jul 19 '21 at 15:18
  • I set it to have all users and groups have read/write access and Im still getting that error. – madmaxx01 Jul 19 '21 at 15:18
  • I've came across something similar when using some hardened linux distributions. MAC also has policies so it might be it. After a search I found this: https://stackoverflow.com/questions/41747473/java-io-filenotfoundexception-operation-not-permitted-error-with-keytool-i which is policy related, can you give it a try and see if it helps? – pabrantes Jul 19 '21 at 16:34
  • Hi @pabrantes - you are right. This actually is a MAC-specific issue. Weirdly when I opened the file in IntelliJ, suddenly there were no issues. – madmaxx01 Jul 20 '21 at 19:02

1 Answers1

1

The file permissions need to be changed.

When I ran the code on my system it didn't throw an exception until I changed the file permissions to block access. The error message was different though ("Access is denied"), though that is probably not very consequential here.

Alan
  • 711
  • 4
  • 14