Hi guys, what's the correct way to receive input from the console and then write it into a file using Java 8? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Hi guys, what's the correct way to receive input from the console and then write it into a file using Java 8?

17th Apr 2019, 10:58 AM
David Afonso
David Afonso - avatar
4 Answers
+ 10
Your welcome
17th Apr 2019, 7:43 PM
Javier Felipe Toribio
Javier Felipe Toribio - avatar
+ 9
You must ensure each write io operation handles exceptions. It's common a "finally" at last line to ensure that file is closed, with or without exceptions. Simple code for writting String data to a text file: import java.io.FileWriter; import java.io.IOException; public class Main {     public static void main(String[] args) {         String myString = "This is\na test";         FileWriter fw = null;         try {             fw =new FileWriter("miOutputFile.txt");             fw.write(myString + "\n");         } catch(IOException e){             e.printStackTrace();         // Ensures file is closed after writting         // with or without exceptions         } finally{             if (fw != null) {                 try {                     fw.close();                 // Handle exceptions when closing file                 } catch (IOException e) {                     e.printStackTrace();                 }             }         }     } }
17th Apr 2019, 2:48 PM
Javier Felipe Toribio
Javier Felipe Toribio - avatar
+ 2
So far I'm receiving the input that way. I'm not sure if it's the best practice using Java 8, and how to write the input into a file using Java 8: /** * Class to get input from the console */ import java.util.Scanner; public class Input { // Variable to check if the user's input is "q" for quiting the program private String input; // Variable to hold the whole user's input private static String text = ""; // Constructor public Input() { // Create a Scanner to be able to read input from the user's keyboard Scanner scanner = new Scanner(System.in); // Ask user to write some input from the console to later write it in a file System.out.println("\nShare your thoughts with me:\n"); // While the user has something to say and write while (true) { // Read each line of input from the keyboard input = scanner.nextLine(); // If the user wants to end the program by writing down the letter "q" if ("q".equals(input)) { System.out.println("\nProgram ended!"); break; } // If not, add the current input to the string variable that holds the whole text text += input + "\n"; } // When the user ends the while loop writing down the "q" letter on the keyboard // Return the whole input string the users wrote System.out.println("\nYou said: \n" + "\n" + text); // Close the scanner scanner.close(); } // Getter to retrieve the user's input public static String getText() { return text; } }
17th Apr 2019, 11:20 AM
David Afonso
David Afonso - avatar
+ 1
Thanks Javier 👍🏼
17th Apr 2019, 6:39 PM
David Afonso
David Afonso - avatar