Java File Handling

File Handling inwards Java

Computer understands things inwards damage of Files, User cry for of persuasion Drives, Folders, Files as well as Databases are unlike things but Computer cry for of persuasion everything is file only.

Computer cry for of persuasion - Drive is file, Folder is file, File is file as well as Database too file.

Using File Class (Pre-defined class) nosotros tin grip Computer files.

Examples:

1) Create a Folder

File fileObject = novel File("C:/Users/gcreddy/Desktop/gcreddy");   
fileObject.mkdir();

2) Check the beingness of gcreddy folder

File fileObject = novel File("C:/Users/gcreddy/Desktop/gcreddy");   
boolean a = fileObject.exists();

if ( a == true){
System.out.println("Folder Exists");
}
else{
System.out.println("Folder Not Exists");
}
---------------------------------------------
3) Delete a Folder

File fileObject = novel File("C:/Users/gcreddy/Desktop/gcreddy");   
fileObject.delete();
-----------------------------
4) Create a Text File

public static void main(String[] args) throws IOException{
File fileObject = novel File("C:/Users/gcreddy/Desktop/Selenium.txt");   
fileObject.createNewFile();
}
-------------------------------------
5) Delete a Text File

public static void main(String[] args) throws IOException{
File fileObject = novel File("C:/Users/gcreddy/Desktop/UFT.xlsx");   
fileObject.delete();
}
-----------------------------------------
6) Read a Text File

public static void main(String[] args) throws IOException{
String line;
FileReader file = novel FileReader("C:/Users/gcreddy/Desktop/UFT.txt");
BufferedReader br= novel BufferedReader(file);

while ((line = br.readLine()) !=null){
System.out.println(line);   
}
br.close();
file.close();
}
------------------------------------------------
7) Write information to a Text file
 

public static void main(String[] args) throws IOException{
FileWriter file = novel FileWriter ("C:/Users/gcreddy/Desktop/UFT.txt");
BufferedWriter bw = novel BufferedWriter(file);
String information ="Welcome to Java";
bw.write(data);
bw.close();
}
----------------------------------------------------
8) Read a Text File as well as write to about other file.

public static void main(String[] args) throws IOException {
String line;
FileReader file1 = novel FileReader("C:/Users/gcreddy/Desktop/UFT.txt");
FileWriter file2 = novel FileWriter("C:/Users/gcreddy/Desktop/Selenium.txt");

BufferedReader br = novel BufferedReader(file1);
BufferedWriter bw = novel BufferedWriter(file2);

while ((line =br.readLine()) != null){
bw.write(line);
bw.newLine();
}
bw.close();
}
}
------------------------------------------------------

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post