Write a program in Java to create a binary file and display its information

What Is Binary File?
A binary file is a computer file that is not a text file. The term "binary file" is often used as a term meaning "non-text file".


बाइनरी फ़ाइल क्या है? एक बाइनरी फ़ाइल एक कंप्यूटर फ़ाइल है जो एक पाठ फ़ाइल नहीं है। शब्द "बाइनरी फ़ाइल" का उपयोग अक्सर "गैर-पाठ फ़ाइल" शब्द के रूप में किया जाता है।

Reading Ordinary Text Files in Java

If you want to read an ordinary text file in your system's default encoding (usually the case most of the time for most people), use FileReader and wrap it in a BufferedReader.

In the following program, we read a file called "temp.txt" and output the file line by line on the console.

import java.io.*;

public class Test {
    public static void main(String [] args) {

        // The name of the file to open.
        String fileName = "temp.txt";

        // This will reference one line at a time
        String line = null;

        try {
            // FileReader reads text files in the default encoding.
            FileReader fileReader = 
                new FileReader(fileName);

            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader = 
                new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) != null) {
                System.out.println(line);
            }   

            // Always close files.
            bufferedReader.close();         
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open file '" + 
                fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println(
                "Error reading file '" 
                + fileName + "'");                  
            // Or we could just do this: 
            // ex.printStackTrace();
        }
    }
}
If "temp.txt" contains this:
I returned from the City about three o'clock on that 
May afternoon pretty well disgusted with life. 
I had been three months in the Old Country, and was 
fed up with it. 
If anyone had told me a year ago that I would have 
been feeling like that I should have laughed at him; 
but there was the fact. 
The weather made me liverish, 
the talk of the ordinary Englishman made me sick, 
I couldn't get enough exercise, and the amusements 
of London seemed as flat as soda-water that 
has been standing in the sun. 
'Richard Hannay,' I kept telling myself, 'you 
have got into the wrong ditch, my friend, and 
you had better climb out.'
The program outputs this:
I returned from the City about three o'clock on that
May afternoon pretty well disgusted with life.
I had been three months in the Old Country, and was
fed up with it.
If anyone had told me a year ago that I would have
been feeling like that I should have laughed at him;
but there was the fact.
The weather made me liverish,
the talk of the ordinary Englishman made me sick,
I couldn't get enough exercise, and the amusements
of London seemed as flat as soda-water that
has been standing in the sun.
'Richard Hannay,' I kept telling myself, 'you
have got into the wrong ditch, my friend, and
you had better climb out.'

Comments