• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

java.io.IOException: Permission denied

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.io.IOException: Permission denied
at java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at IOtest.FIle_1.main(FIle_1.java:25)



import java.io.File;

public class FIle_1 {

public static void main(String[] args) {

String fileName=File.separator+"hello.txt";
File f=new File(fileName);
if(!f.exists()){
try{
f.createNewFile();
}catch(Exception e){
e.printStackTrace();
}
}
else{
System.out.println("hi");
}

the file path is wrong?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most likely. What's "File.separator+" doing there?
 
moo jackie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I write another code:

and output:
java.io.IOException: No such file or directory

at java.io.UnixFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at IOtest.FIle_1.main(FIle_1.java:22)

code:
public static void main(String[] args) {
File fDir=new File(File.separator+"file_test");
File file1=new File(fDir,"file1.txt");
if(fDir.exists()){
System.out.println(fDir.getPath());

}else{
fDir.mkdir();
}
if(file1.exists()){
System.out.println(file1.getPath());
file1.delete();
}else{
try {
file1.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

}
 
Sheriff
Posts: 22819
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

You're trying to create files /hello.txt /test_file. The leading slash means you want to put them in the root of your file system. From the exception I see that you are using a Linux or UNIX file system (perhaps Mac OS even). The thing is, on Linux and UNIX systems, not every user can write to any location. Usually all users have only access to their own home folder, and the /tmp folder, with the exception of the root user who can do anything he wants on the system.

So, if you want to write to the root of the file system (which you really really really shouldn't!) you'll need to run your code as a user who has more rights. On Ubuntu you need to put sudo before the command, e.g. sudo java IOTest.File_1, and that will still fail if you're not allowed to run sudo.

And could you please UseCodeTags next time?
 
moo jackie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks.I get it.
 
ice is for people that are not already cool. Chill with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic