File Handling In Java
package com.company;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class tuts {
public static void main(String[] args) {
/*
// Creating new File
File myFile = new File("aryan111.txt");
try {
myFile.createNewFile();
} catch (IOException e) {
System.out.println("Unable to create this file");
}
// Code to write to a file
try {
FileWriter fileWriter = new FileWriter("aryan111.txt");
fileWriter.write("This is our first file from this Java Course\nOk now Bye");
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
// Reading A File
File myFile = new File("aryan111.txt");
try {
Scanner sc = new Scanner(myFile);
while(sc.hasNextLine()){
String line = sc.nextLine();
System.out.println(line);
}
sc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
*/
File myFile = new File("aryan111.txt");
if (myFile.delete()){
System.out.println("I have deleted: "+ myFile.getName());
}
else{
System.out.println("Some problem occurred while deleting this file");
}
}
}
Comments
Post a Comment