Code:
public class CreatingReadingWritingTextFiles {
public static void main(String[] args) throws IOException {
// creating
File f = new File("C:\\temp.txt");
f.createNewFile();
// writing
FileWriter w = new FileWriter("C:\\temp.txt");
BufferedWriter out = new BufferedWriter(w);
out.write("hello we are writing in a file");
out.newLine();
out.write("This is a new line");
out.flush();
//reading
FileReader r = new FileReader("C:\\temp.txt");
BufferedReader bfr = new BufferedReader(r);
String x ="";
//System.out.println(bfr.readLine());
//System.out.println(bfr.readLine());
//System.out.println(bfr.readLine());
while((x = bfr.readLine()) != null){
System.out.println(x);
}
}
}
this is not creating text file. it is giving me errors as
ReplyDeleteException in thread "main" java.io.IOException: Access is denied
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at readWrite.CreatingReadingWritingTextFiles.main(CreatingReadingWritingTextFiles.java:10)
Hi I think u r creating directly in C drive
DeleteTry Creating in other folders like documents or other drives in your computer
Nice Article. Very Helpful. You can follow this article also Java File handling tutorials with examples
ReplyDeletehttp://www.javaproficiency.com/2015/02/file-handling-in-java.html
Thanks for sharing nice article . It's very help visit File Handling In Java
ReplyDelete