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
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
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
0
gudii9Author Commented:
>>>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
0
CEHJCommented:
I'd need to see your code
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

gudii9Author Commented:
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

0
CEHJCommented:
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()
0
gudii9Author Commented:
How do i modify my code to fix the issue. Please advise
0
CEHJCommented:
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?
0
gudii9Author Commented:
>>>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
0
CEHJCommented:
Because you didn't use the code i posted. Your slashes need to be escaped if you want (unnecessary) backslashes
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.