Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use the corresponding classes for reading from a file to readthese values back i

ID: 3614594 • Letter: U

Question

Use the corresponding classes for reading from a file to readthese values back in to variables and print them toSystem.out.

Most classes in java.io for reading files do sosequentially. That is, one must read the first byte, andthen the second, and then the third. It's not possible to skipahead to the last 128 bytes without reading all the interveningbytes. You'll have seen this to be true for theScanner class. The class RandomAccessFiledoes support such skipping ahead, in which you are seekinga particular byte position.

Use the RandomAccessFile class to read inthe ID3 tags from this MP3 file. Methods that you will find useful includelength, seek, and the readmethod which takes a byte array argument. If youcreate an array of 30 bytes and pass it as an argument toread, it will fill those 30 bytes with data from thefile. You can then turn this array into something printable usingthe String constructor that takes a byte arrayargument. Read in and print each of the six fields on its ownline.

The last field can be used by music software as an index into anarray of established genres. (You do not need todo perform this lookup in your own code.) What's the genre for theprovided MP3 file?

Explanation / Answer

package lab08; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; public class Checkpoint1_1_MP3 {     public static void main(String[] args) throwsFileNotFoundException, IOException     {         File data = newFile("C:/Users/Alex/Desktop/java/song.mp3");         RandomAccessFile tagReader =new RandomAccessFile(data, "r");                 byte[] title =newbyte[30];         byte[] artist =newbyte[30];         byte[] album =newbyte[30];         byte[] year =new byte[4];         byte[] comment =newbyte[30];         byte genre;                tagReader.seek(tagReader.length() - 125);                 tagReader.read(title);         tagReader.read(artist);         tagReader.read(album);         tagReader.read(year);         tagReader.read(comment);         genre =tagReader.readByte();         String sTitle = newString(title);         String sArtist = newString(artist);         String sAlbum = newString(album);         String sYear = newString(year);         String sComment = newString(comment);                System.out.println(sTitle);        System.out.println(sArtist);        System.out.println(sAlbum);        System.out.println(sYear);        System.out.println(sComment);        System.out.println(genre);                 tagReader.close();             } } package lab08; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Checkpoint_1_Variable {     public static void main(String[] args) throwsFileNotFoundException, IOException     {         DataOutputStream out = newDataOutputStream(newFileOutputStream("C:/Users/Alex/Desktop/data.bin"));         out.writeDouble(3.14159);         out.writeInt(500);         out.writeBoolean(false);         out.close();                 DataInputStream in = newDataInputStream(newFileInputStream("C:/Users/Alex/Desktop/data.bin"));                 Double x =in.readDouble();         int y = in.readInt();         boolean z =in.readBoolean();         System.out.println(x + ", " +y + ", " + z);         in.close();                     } }