Blog Introductions
In this blog contains tutorials about HTML Tutorials, Python Tutorials, CSS Tutorials, How to, PHP Tutorials, Java Tutorials, C++ Tutorials, Tutorials, Examples, Source code,Learning,Tips and Software development services. ava Files create file and delete file and open file read file | Java Tutorials Bestitworriors
Java Files create file and delete file and open file read file
Java Files create file and delete file and open file read file | Java Tutorials Bestitworriors - File handling is very important part of any application.Java has serval method for creating,reading,updating and deleting files.We can use createNewFile() to create new file in java .This method return true if file created and return false if file already exists.
Some Key Points of Java Files
- File handling is very important part of any application
- We can create , read , write , update and delete files
- First import java.io.files
- Then crate object of specific file-File myObj = new File("filename.txt");
- Now you can perform write ,read or update or delete function
- Import scanner also for scane the files import java.util.Scanner;
Code
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFile {
public static void main(String[] args) {
try {
File myObj = new File("monthfile.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
0 Comments