gudii9
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.cr eateFileEx clusively( Native Method)
at java.io.File.createNewFile (Unknown Source)
at coreservlets.CreateFileExa mple.main( CreateFile Example.ja va: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\perso nal\newfil e.txt
but the above path is not liked by compiler
Please advise.
Any links resources ideas highly appreciated. Thanks in advance
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.cr
at java.io.File.createNewFile
at coreservlets.CreateFileExa
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\
but the above path is not liked by compiler
Please advise.
Any links resources ideas highly appreciated. Thanks in advance
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
ASKER
>>>C:\Users\Desktop\gpfold er\gpmy\pe rsonal\new file.txt
I wonder why this path also not working. For testing which location i can use. please advise
I wonder why this path also not working. For testing which location i can use. please advise
I'd need to see your code
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();
}
}
}
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()
http://docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdirs()
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);
should do it, but i've got to ask - why do you want to create an empty text file?
ASKER
>>>File root = new File("C:/Users/Desktop/gpf older/gpmy /personal" );
I am getting
''Invalid escape sequence" as in attachment.
Please advise on how to fix it
.
My final program looks like below
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);
}
}
EscapeSeq.jpg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:)