Pages

Saturday, 7 January 2017

Chapter 17 Exercise 18, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

17.18 (View bits)
Write the following method that displays the bit
representation for the last byte in an integer:
public static String getBits(int value)
Write a program that prompts the user to enter a
file name, reads bytes from the file, and displays
each byte’s binary representation.


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Scanner;

public class Exercise_18 {


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

        System.out.print(
                "Bit viewer\n" +
                        "Enter a filename: ");
        File file = new File(new Scanner(System.in).nextLine());

        String bits = "";
        try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file))) {

            int r;
            while ((r = in.read()) != -1)
                bits += " " + getBits(r);
        }
        System.out.println(bits);
    }

    public static String getBits(int value) {

        String byteString = "";

        for (int i = 7; i >= 0; i--)
            byteString += ((value >> i) & 1);

        return byteString;
    }
}

No comments :

Post a Comment