import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//read file
System.out.println("Enter the file path to read:");
String filePathToRead = sc.nextLine();
File file = new File(filePathToRead);
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
System.out.println("File doesn't exist");
} catch (IOException e) {
System.out.println("Failed to read file");
}
//read file
//write to file
System.out.println("Enter the file path to write:");
String filePathToWrite = sc.nextLine();
File writeFile = new File(filePathToWrite);
String line = "Hello, test!";
try (BufferedWriter writer = new BufferedWriter(new FileWriter(writeFile))) {
writer.write(line);
} catch (IOException e) {
System.out.println("Failed to write to a file");
}
//write to file
sc.close();
}
}
Comments
Leave a comment