Hello World 999

Note: 

1. The underline words is the input or the one that you type-in in the console. So, the one you type-in will be written in the text document which is done by the FileWriter class. The FileReader class together with the Scanner class will read whatever text is in the text document. 

2. You need to include a ".txt" on the file name so that it will create a text document and not a file. If it is a file, it can still be written on the file, and it can read the file. There will be no problem if it is a file but that depends on the purpose. If it is just plain words, a text document would suffice. 

Byte Stream has the this:

     FileOutputStream - writing data to a file

     FileInputStream - read input byte from a file

Character Stream has a built in bridge for this byte stream:

    OutputStreamWriter - from character to byte stream

     InputStreamReader - from byte to character stream


================

class CharacterStreamPractice{

     public static void main(String[] args) throws IOException{

           String yourFileName = "YourFileName.txt";

           CharacterStreamPractice.writeSomething(yourFileName);

           CharacterStreamPractice.writeSomething(yourFileName);

     }

     private static void writeSomething(String fileName) throws IOException{

          try(FileOutputStream outputStream = new FileOutputStream(fileName);

                 OutputStreamWriter streamWriter = new OutputStreamWriter(outputStream);

                 Scanner scanner = new Scanner(System.in)

          ){

               String putItHere = "";

              while(scanner.hasNext()){

                  String message = scanner.next();

                  String addingSpace = message.concat(" ");

                  putItHere = putItHere.concat(addingSpace);

                  if(message.equals("999")){

                      break;

                  }

             }

             streamWriter.write(putItHere);

         }

     }

     private static void readSomething(String fileName) throws IOException{

          try(FileInputStream inputStream = new FileInputStream(fileName);

                 InputStreamReader streamReader = new InputStreamReader(inputStream);

                 Scanner scanner = new Scanner(streamReader);

          ){

               String message;

               while(scanner.hasNext()){

                    message = scanner.next();

                    String addingSpace = message.concat(" ");

                    System.out.print(addingSpace);

               }

          }

     }

}

================

Result:

Practicing input and output stream

999

Practicing input and output stream 999

Note: BufferedWriter and BufferedReader is required for OutputStreamWriter and InputStreamReader respectively so that this buffered classes will wrap these stream classes. Why it needs to be wrap? It is because the stream class are used for converting byte to characters and vice versa. So, everytime there is a conversion, it will keep on calling these stream classes.

Java ProgrammingWhere stories live. Discover now