Need in java Part 1 -- File Input Streams For part 1, you will write a program t
ID: 3777211 • Letter: N
Question
Need in 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:usersUSERdocuments. 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.
TShirt text: String -size: String -gift: boolean +getText0: String +setText (String): void +getSize 0: String +setSize0: String +isGifto: boolean tsetGift(bor oolean voidExplanation / Answer
import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class Util{ /** * Read bytes from a File into a byte[]. * * @param file The File to read. * @return A byte[] containing the contents of the File. * @throws IOException Thrown if the File is too long to read or couldn't be * read fully. */ public static byte[] readBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); // Get the size of the file long length = file.length(); // You cannot create an array using a long type. // It needs to be an int type. // Before converting to an int type, check // to ensure that file is not larger than Integer.MAX_VALUE. if (length > Integer.MAX_VALUE) { throw new IOException("Could not completely read file " + file.getName() + " as it is too long (" + length + " bytes, max supported " + Integer.MAX_VALUE + ")"); } // Create the byte array to hold the data byte[] bytes = new byte[(int)length]; // Read in the bytes int offset = 0; int numRead = 0; while (offset = 0) { offset += numRead; } // Ensure all the bytes have been read in if (offsetRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.