Skip to content

Commit

Permalink
Merge pull request #6718 from ishaanvireet/patch-14
Browse files Browse the repository at this point in the history
Create mainfilehandling.java
  • Loading branch information
ossamamehmood authored Oct 30, 2023
2 parents 5581e92 + a741f87 commit 3d1afd2
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Add Code Here/Java/mainfilehandling.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.io.*;
import java.io.IOException;
import java.util.Scanner;

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

// create
try {
File fo = new File("new-file.txt");
fo.createNewFile();
} catch (IOException e) {
System.out.println(e.getMessage());
}

// write in the file
try {
FileWriter fw = new FileWriter("new-file.txt");
fw.write("सर्वधर्मान्परित्यज्य मामेकं शरणं व्रज, अहं त्वां सर्वपापेभ्यो मोक्षयिष्यामि मा शुच:");
fw.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}

// reading from a file
try (BufferedReader br = new BufferedReader(new FileReader("new-file.txt"))) {
while (br.ready()) {
System.out.println(br.readLine());
}
} catch (IOException e) {
System.out.println(e.getMessage());
}

// create
try {
File fo = new File("random.txt");
fo.createNewFile();
if(fo.delete()) {
System.out.println(fo.getName());
}
} catch (IOException e) {
System.out.println(e.getMessage());
}



}
}

0 comments on commit 3d1afd2

Please sign in to comment.