File I/O Lab

Part I

Write a Binary File Viewer that can display the contents of a file in binary. Your program will feature

To test your program: Use Notepad to save the characters ABC into a file. Use your binary file viewer to read the file.

  1. What to do you see?
Use your program to read and display the contents of this (binary.dat) file. Can you determine what was written to the file? Use the links at the bottom of the page to check your response later.

If you are so inclined, you can start with the File Viewer program (the program also uses the File Manager class) and modify it to suit your purposes. However, it may be easier to simply write your own program from scratch. Alternatively, modify ByteViewer.

Part II Analyzing the Contents of a Bitmapped Image

Add a menu option to your Binary File Viewer to open a bitmap image and replace the green in the rgb values for those green intensities greater than 127 with black and save this file with the same file name and extension with "Modified" appended to the name. This code may be useful:

public static void download(File inFile, File outFile) throws IOException {
    InputStream in = new FileInputStream(inFile);
    FileOutputStream out = new FileOutputStream(outFile);
    byte[] b = new byte[1024];    //consider making the byte array the same size as the file
    int len;
    while((len = in.read(b)) != -1) {  //the read method is overloaded so its behavior varies by the input parameter
        out.write(b, 0, len);             //modify those bytes that represent green with intensity > 127 before writing the byte array
    }
    out.close();
}

public static void download(URL url, File file) throws IOException {
    InputStream in = url.openStream();
    FileOutputStream out = new FileOutputStream(file);
    byte[] b = new byte[1024];
    int len;
    while((len = in.read(b)) != -1) {
        out.write(b, 0, len);
    }
    out.close();
}

A discussion of the bitmap format can be seen here. After reading the discussion, view the contents of the following files to help determine the file layout: red.bmp, green.bmp, blue.bmp. Save these files, right click and view properties to determine more about the format of these files. The base URL for these files is
http://torvalds.bearlabs.uncw.edu/~tompkinsj/221/Fall04/Lab12b/

Answer the seven questions in the body of your email with your zipped project folder attached:

  1. What is the distribution of the 36 bytes in binary.dat?
  2. What is the hexadecimal value of the size of the green.bmp file? This value is in the 4 bytes following the file type, BM (0x42 0x4d).
  3. What is the hexadecimal value of the offset from the beginning of the file to the start of the image data? Offset is the last 4 bytes of the 14 byte Header.
  4. What is the decimal value of offset?
  5. What are the width and height of the green.bmp image? Found in the second and third four byte groups of the 40 byte Information Header.
  6. Test your Bitmap modification option on green.bmp, blue.bmp, and red.bmp. How do greenModified.bmp, blueModified.bmp, and redModified.bmp differ from the original files when viewed in Paint or Office Picture Manager?



    This is what was written to the file (view as html), and here is the program that produced both data files.

    Based on Dr. Narayan's Lab 5 , Modified by Jack Tompkins