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 
  • mac or windows OS? – VedantK Jul 19 '21 at 14:23
  • 4
    Seems like its file access issue – VedantK 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 –  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. –  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. –  Jul 20 '21 at 19:02

2 Answers2

2

Like this answer suggests, if you are on Mac and running it by java cli drag the the cli to the Full Disk Access under Security and privacy, and allow it.

If you are running the .jar via Jar Launcher at /System/Library/CoreServices/Jar Launcher.app drag that in instead.

6rchid
  • 1,074
  • 14
  • 19
  • 1
    This does work for me. I update my Mac os to Ventura 13.4 (22F66), and the code that was working before just suddenly not working. After adding my ide (SpringToolSuite) into Privacy and Security , it works! – Gary Liao Jun 02 '23 at 06:13
0

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
  • 716
  • 5
  • 15