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

Java - Help reading code! This code is suppose to read code from a file and save

ID: 640379 • Letter: J

Question

Java - Help reading code! This code is suppose to read code from a file and save it in an array, then output it to a different file but I have no idea what does what can someone please explain this to me and place comments??

import java.io.*;
import java.util.*;
import java.lang.String;

class Student implements Serializable
{
   String fname,lname,userid;
   double gpa;
   @Override
   public String toString() {
   return new StringBuffer("")
   .append(this.fname)
   .append(" ")
   .append(this.lname)
   .append(" ")
   .append(this.userid)
.append(" ")
   .append(this.gpa).toString();
   }
}

public class studentInfo{
private Scanner x; //Creates scanner
private Student stud[];

private void openFile(String filename){ //Sets scanner to file
try{
x= new Scanner(new File(filename));
stud = new Student[20];
}catch(Exception e){

System.out.println("File not found!");

}
}
public void readFile()
{
int i = 0;
try{
FileOutputStream out = new FileOutputStream("deans.txt");
ObjectOutputStream oout = new ObjectOutputStream(out);
PrintStream fileStream = new PrintStream(new FileOutputStream("deans.txt", true));

while(x.hasNext()) //Reads file until it reaches the end
{
stud[i] = new Student();
stud[i].fname = new String(x.next());
stud[i].lname = x.next();
stud[i].userid = x.next();
stud[i].gpa = x.nextDouble();
if(stud[i].gpa > 3.5)
{
   fileStream.println(stud[i].toString());
   System.out.printf("%s %s %s %s ", stud[i].fname,stud[i].lname,stud[i].userid,stud[i].gpa);
}
i++;
}
}
catch(Exception ex)
{
   ex.printStackTrace();
}
}
public void closeFile()
{
x.close();
}
public static void main(String[] args)
{
studentInfo Tests = new studentInfo();

Tests.openFile(args[0]);
Tests.readFile();
Tests.closeFile();
}
}


The contents of the file that is suppose to be read:

John Smith jjs81111 3.2
Mary Jones maj82222 2.9
Susan Black sbb83333 4.0
Jeff Cooper jbc94444 2.5
Joe Green mjg95555 1.6
Sally Gray smg06666 3.6
Bob Redford bar07777 2.2
Mary Blackford mjb18888 1.8
Tom Robinhood tar19999 3.8

Explanation / Answer

import java.io.*;
class CopyFile {
public static void main(String args[])
throws IOException
{
int i;
FileInputStream fin;
FileOutputStream fout;
try {
// open input file
try {
fin = new FileInputStream(args[0]);
} catch(FileNotFoundException e) {
System.out.println("Input File Not Found");
return;
}
// open output file
try {
fout = new FileOutputStream(args[1]);
} catch(FileNotFoundException e) {
System.out.println("Error Opening Output File");
return;
}
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Usage: CopyFile From To");
return;
}
// Copy File
try {
do {
i = fin.read();
if(i != -1) fout.write(i);
} while(i != -1);
} catch(IOException e) {
System.out.println("File Error");
}
fin.close();
fout.close();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote