Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

java IO example

Hi,

I was trying below java IO example

http://www.mkyong.com/java/how-to-create-a-file-in-java/


I am getting error as below

java.io.IOException: A required privilege is not held by the client
      at java.io.WinNTFileSystem.createFileExclusively(Native Method)
      at java.io.File.createNewFile(Unknown Source)
      at coreservlets.CreateFileExample.main(CreateFileExample.java:14)


Please advise on how to resolve it.

I tried one other location where i can copy paste files etc where i have all privileges to do it

C:\Users\Desktop\gpfolder\gpmy\personal\newfile.txt

but the above path is not liked by compiler

Please advise.

Any links resources ideas highly appreciated. Thanks in advance
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Quite simply you can't (usually in later versions of Windows) write to the root of C:\ for security reasons. You don't normally need to anyway. If you really do, then you could probably run your Java in an admin prompt
Avatar of gudii9

ASKER

>>>C:\Users\Desktop\gpfolder\gpmy\personal\newfile.txt



I wonder why this path also not working. For testing which location i can use. please advise
I'd need to see your code
Avatar of gudii9

ASKER

Please find my code


package com.mkyong.file;
 
import java.io.File;
import java.io.IOException;
 
public class CreateFileExample 
{
    public static void main( String[] args )
    {	
    	try {
 
	      File file = new File("C:\Users\Desktop\gpfolder\gpmy\personal\newfile.txt");
 
	      if (file.createNewFile()){
	        System.out.println("File is created!");
	      }else{
	        System.out.println("File already exists.");
	      }
 
    	} catch (IOException e) {
	      e.printStackTrace();
	}
    }
}

Open in new window

All the parent directories need to exist too. If they don't, then see
http://docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdirs()
Avatar of gudii9

ASKER

How do i modify my code to fix the issue. Please advise
File root = new File("C:/Users/Desktop/gpfolder/gpmy/personal");
if (!root.exists()) {
    root.mkdirs();
}
File f = new File(root, "newfile.txt");
boolean created = f.createNewFile();
System.out.printf("Was file %s created? %b%n", f.getAbsolutePath(), created);

Open in new window


should do it, but i've got to ask - why do you want to create an empty text file?
Avatar of gudii9

ASKER

>>>File root = new File("C:/Users/Desktop/gpfolder/gpmy/personal");



I am getting
''Invalid escape sequence" as in attachment.

Please advise on how to fix it
.

My final program looks like below


package com.mkyong.file;
 
import java.io.File;
import java.io.IOException;
 
public class CreateFileExample 
{
    public static void main( String[] args )
    {	
 
    	
    	File root =  new File("C:\Users\Desktop\gpfolder\gpmy\personal\newfile.txt");
    	if (!root.exists()) {
    	    root.mkdirs();
    	}
    	File f = new File(root, "newfile.txt");
    	boolean created = f.createNewFile();
    	System.out.printf("Was file %s created? %b%n", f.getAbsolutePath(), created);
    }
} 

Open in new window

EscapeSeq.jpg
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:)