Buffered Streams

3 0 0
                                    

Byte streams and Character streams were unbuffered input and output. Unbuffered means volatile or the memory retains only as long as there is a power but here in unbuffered streams means that the transaction of reading and writing is all done by the operating system. And this is a no-no. So, buffered was created to facilitate such reading and writing transactions. Buffered stream is a wrapper use to convert unbuffered stream into a buffered stream.

Buffer is a memory area.

Buffer input streams is used to read data from the buffer. The FileInputStream or FileReader is called only when the buffer is empty.

Buffer output streams is used to write data to a buffer. The FileOutputStream or FileWriter is called only when the buffer is full.

Byte stream

     1. BufferedInputStream - wraps FileInputStream

     2. BufferedOutputStream - wraps FileOutputStream

Character stream

     1. BufferedReader - wraps FileReader

     2. BufferedWriter - wraps FileWriter

The program is long. So, I will just copy it on the Integrated Development Enviroment (ide).

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

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;


class YourGenericClass070222 {


    public static void main(String[] args)throws IOException{
        String yourTempDocument = "YourTemporaryFileDocument.txt";
        YourGenericClass070222.writeSomething(yourTempDocument);
        System.out.println(YourGenericClass070222.readSomething(yourTempDocument));
    }


    private static void writeSomething(String fileName) throws IOException {
        try(FileOutputStream outputStream = new FileOutputStream(fileName);
               OutputStreamWriter streamWriter = new OutputStreamWriter(outputStream);
               BufferedWriter bufferedWriter = new BufferedWriter(streamWriter
);
               Scanner scanner = new Scanner(System.in)
        ){
            String putItTogether = "";
            while (scanner.hasNext()){
                String message = scanner.next();
                String addingSpace = message.concat(" ");
                putItTogether = putItTogether.concat(addingSpace);
                if(message.equals("999")){
                    break;
                }
            }
            bufferedWriter.write(putItTogether);
        }
    }


    private static String readSomething(String fileName)throws IOException{
        try(FileInputStream inputStream = new FileInputStream(fileName);
               InputStreamReader streamReader = new InputStreamReader(inputStream);
               BufferedReader bufferedReader = new BufferedReader(streamReader);
               Scanner scanner = new Scanner(bufferedReader)

        ){
            String putItTogether = "";
            while(scanner.hasNext()){
                String message = scanner.next();
                String addingSpace = message.concat(" ");
                putItTogether = putItTogether.concat(addingSpace);
            }
            return putItTogether;
        }
    }


    private static void writeSomething2(String fileName)throws IOException{
        try(FileWriter fileWriter = new FileWriter(fileName);
               BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
               Scanner scanner = new Scanner(System.in)

        ){
            String putItTogether = "";
            while(scanner.hasNext()){
                String message = scanner.next();
                String addingSpace = message.concat(" ");
                putItTogether = putItTogether.concat(addingSpace);
                if(message.equals("999")){
                    break;
                }
            }
            bufferedWriter.write(putItTogether);
        }
    }


    private static String readSomething2(String fileName)throws IOException{
        try(FileReader fileReader = new FileReader(fileName);
               BufferedReader bufferedReader = new BufferedReader(fileReader);
              Scanner scanner = new Scanner(bufferedReader)

        ){
            String putItTogether = "";
            while(scanner.hasNext()){
                String message = scanner.next();
                String addingSpace = message.concat(" ");
                putItTogether = putItTogether.concat(addingSpace);
            }
            return putItTogether;
        }
    }


    private static void writeSomething3(String fileName)throws IOException{
        try(FileOutputStream outputStream = new FileOutputStream(fileName);
               BufferedOutputStream bufferedOutputStream = new  BufferedOutputStream(outputStream);
               Scanner scanner = new Scanner(System.in)

        ){
            String putItTogether = "";
            while(scanner.hasNext()){
                String message = scanner.next();
                String addingSpace = message.concat(" ");
                putItTogether = putItTogether.concat(addingSpace);
                if(message.equals("999")){
                    break;
                }
            }
            bufferedOutputStream.write(putItTogether.getBytes(StandardCharsets.UTF_8));
        }
    }


    private static String readSomething3(String fileName)throws IOException{
        try(FileInputStream inputStream = new FileInputStream(fileName);
               BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
               Scanner scanner = new Scanner(bufferedInputStream
)
        ){
            String putItTogether = "";
            while(scanner.hasNext()){
                String message = scanner.next();
                String addingSpace = message.concat(" ");
                putItTogether = putItTogether.concat(addingSpace);
            }
            return putItTogether;
        }
    }
}

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

Result:

Hello

World

Java World

999

Hello World Java World 999


Note:

1. A text Document will be created. And the result that is in the italic and underlined words were the input that you type and will be save in the text document by the buffered output writer stream. The one line normal words are read by the buffered input reader stream. I only used two methods in the above program which writeSomething() and readSomething(). There are two more methods for each that functions the same.

2. Flush method - is used to flush the buffer. Flushing the buffer means you declare the memory area as full even though it is not yet full so that it can be process by the buffered streams. You can manually flush the buffer as long as it is an output stream and a buffered one. Although, some buffered classes support autoflush like PrintWriter. PrintWriter activates the autoflush when a println or a format method is called.

Java ProgrammingWhere stories live. Discover now