Search This Blog

Wednesday, August 12, 2020

File Handling?

 Creating Fine On Disk??

Program:

import java.io.File;

import java.io.IOException;


public class MainClass {


public static void main(String[] args) {


File file = new File("readfile.txt");

try {

file.createNewFile();

System.out.println("File Created");

} catch (IOException e) {

e.printStackTrace();

}

}

}


Program For Create Directory:

import java.io.File;
import java.io.IOException;

public class CreataDirectory {
public static void main(String[] args) {

// File directory=new File("test");
// directory.mkdir();
// System.out.println("Direcroty is created....");

File directory = new File("package/org/goomo"); // for Ubuntu or Mac
// File directory = new File("package\\org\\goomo"); // for windows
directory.mkdirs();
System.out.println("Direcroty is created....");
File file = new File("package/org/goomo/test.txt");
System.out.println("File created");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Program for write onto File:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteIntoFile {

public static void main(String[] args) {
File directory = new File("package/org/goomo");
directory.mkdirs();
System.out.println("Direcroty is created....");
directory = new File("package/org/goomo/test.txt");
System.out.println("File created");
try {
directory.createNewFile();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error Occured");
}
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(directory,true))) {

bufferedWriter.write("Pankaj");
bufferedWriter.newLine();
bufferedWriter.write("Jatan Veer");
bufferedWriter.close();
System.out.println("Writing data to file is done ....");
} catch (IOException e) {
             System.out.println("Error occured duing writing onto file...");
}
}
}


Program for Reading file with BufferedReader

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class WriteIntoFile {

public static void main(String[] args) {
File directory = new File("package/org/goomo");
directory.mkdirs();
System.out.println("Direcroty is created....");
directory = new File("package/org/goomo/test.txt");
System.out.println("File created");
try {
directory.createNewFile();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Error Occured");
}
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(directory, true))) {

bufferedWriter.write("Pankaj kumar");
bufferedWriter.newLine();
bufferedWriter.write("Jatan Veer");
bufferedWriter.newLine();
bufferedWriter.close();
System.out.println("Writing data to file is done ....");
} catch (IOException e) {
System.out.println("Error occured duing writing onto file...");
}
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader("package/org/goomo/test.txt"));
String line;
System.out.println("=================================");
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Read file using scanner class:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadFileWithScanner {
public static void main(String[] args) {

try {
Scanner scanner = new Scanner(new File("test.txt"));
String line;
while (scanner.hasNext()) {
line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("Error occured during reading file...");
}

}
}


Delete file Program

import java.io.File;


public class DeleteFile {

public static void main(String[] args) {


File file = new File("goomo/test.txt");

if (file.delete()) {

System.out.println("Deleted successfully.....");

} else {

System.out.println("Error occued while delet file....");

}

}

}


No comments:

Post a Comment