Write java program to read and write string using file.
import java.io.*;
import java.util.*;
public class App {
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
System.out.print("Enter the source file name: ");
String fileNameSource = keyBoard.nextLine();
System.out.print("Enter the destination file name: ");
String fileNameDestination = keyBoard.nextLine();
try {
BufferedReader bufferedReaderSource = new BufferedReader(new FileReader(fileNameSource));
FileWriter fileWriterDestination = new FileWriter(fileNameDestination);
String line = bufferedReaderSource.readLine();
while (line != null) {
fileWriterDestination.write(line + "\n");
line = bufferedReaderSource.readLine();
}
bufferedReaderSource.close();
fileWriterDestination.close();
} catch (IOException e) {
}
keyBoard.close();
}
}
Comments
Leave a comment