Java Part 1 File Input Streams For part 1 you will write a
Java Part 1 -- File Input Streams
For part 1, you will write a program that will use a FileInputStream to read bytes from a file, and display both a hex version of the byte, and it\'s corresponding ASCII code (if it is a printable character).
Details
This program should be able to read any file the user selects. To make this easy for the user to select a file, we\'re going to use the JFileChooser dialog. This is from the Swing framework (precursor to JavaFX). You can use the following code to bring up a dialog box that will allow you to navigate to a file on your computer:
This code snippet brings up a File Selection dialog box to starts in the USER-HOME directory. On windows, this will be c:\\users\\USER\\documents. The if-statement checks to see if a file was selected, and if so, then calls a method to show the contents of the file. This file can any type of file -- text or non text.
As mentioned earlier, you will use a FileInputStream to read bytes from the selected file. Your code will read 10 bytes at a time, and display the hexadecimal (remember the number systems from COP1000?) representation of the byte, and the ASCII character, if it is a letter or digit. If it is not a letter or a digit, then just print a period \".\". You should display the 10 bytes in hex first, then the ASCII charatter. For readability, put spacing between the hex and the ASCII. A example of printing the first 4 lines of a file is shown below:
There are many ways to get the hex representation of a number, and our text shows you one way with a wrapper class (Integer.toHexString). You can use this if you want, but there\'s a simpler way -- use the format specifier \'x\'. For example, you can do something like this: String.format(\"%02x \", some_byte). This will take a byte, and provide a 2 digit hex representation of it. The \'0\' in the format specifier states that if the hex value is one digit, then left pad it with a \'0\'. So, a byte value of 15 will be 0F, not F.
Another technique that will help is using the methods in Character wrapper class. Use the isLetterOrDigit() method to determine if the character is printable. Note that this method takes a char as input, so your byte has to be type cast to a char.
Part 2 Object Input/Output Streams
For this exercise, you will use JavaFX to create a small order form GUI for T-Shirt. The information entered into the form must be \'preserved\', that is, after you save it, and restart the app, the form should display with the information entered previously. To do this, you will save the information in an entity object, and serialize it. When the app starts, if will de-serialize the object and use it to populate the form.
Details
The first step is to create the entity object. An entity object is just a plain old java class -- nothing fancy. It has some attributes, and the appropriate getters/setters. Here, we have a TShirt class with 3 attributes: size, text, and gift. We want this object to be serializable, so make sure it implements the required interface.
Next, use your new JavaFX knowledge to create the following GUI. The radio buttons should be grouped so only one is selected at any one time.
When the save button is selected, you will read the data from the form and call the setter methods on your entity object. Then, you will serialize your tshirt entity object it to the current directory.
When your JavaFX app starts, it needs to deserialize the TShirt entity object and call the getter methods to set the form controls. Keep in mind that the entity object may not exist ( which is certainly true the first time). In other words, make sure that you have de-serialized a object before you use it to populate the GUI controls.
Solution
Please find below the solution for question#1:
package chegg;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.swing.JFileChooser;
public class Solution1 {
public static void showContents(File file){
try {
FileInputStream fis=new FileInputStream(file);
//long flength=file.length();
//long offset=0;
while(true){
byte b[]=new byte[10];
int tmp=0;
tmp=fis.read(b, 0, 10);
if(tmp==-1)
break;
for(byte mybyte:b){
Integer i=(int)mybyte;
System.out.print(Integer.toHexString(i));
}
System.out.print(\" \");
for(byte mybyte:b){
char ch=(char)mybyte;
if(Character.isDigit(ch)||Character.isLetter(ch)){
System.out.print(ch);
}
else
System.out.print(\".\");
}
System.out.println();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
System.out.println(\"File contents: \" + file.getAbsolutePath());
System.out.println();
showContents(file);
}
}
}

