Engineering Full Stack Apps with Java and JavaScript
Need to read input data from Ketboard.
Use of Scanner is a convenient and preferred approach for reading text data from keyboard. For reading data from keyboard, we can attach the PrintStream System.in which reprecents the default input device (which is usually keyboard).
Important features of Scanner:
Can get the whole line using the nextLine() method.
Can read specific tyoe values using specific methods such as nextInt().
Can parse primitive types and strings using regular expressions.
Breaks its input into tokens using a delimiter pattern, which by default is whitespace, and then get those using various next methods.
import java.io.*;
import java.util.Scanner;
public class FileCopier {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new InputStreamReader(System.in));
FileWriter fw = new FileWriter("myFile.txt");
System.out.println("Enter text (enter string 'end' on a line to end):");
String str = "";
while (!str.equals("end")) {
str = sc.nextLine();
fw.write(str + "\n");
}
fw.close();
sc.close();
}
}