Question

Create a text file.

Use java.io File to input information on 10 students into a text file.

student ID, First Name, Last Name, DOB, and address.

Create a header row for your student information.

PLEASE ATTACH SNIPPING PHOTO SOURCE CODE AND FILE CONTAINING STUDENT INFORMATION! This is the most important. (See attached text file photo below.) Thank you :).

Online resource: http://t utorials.jenkov.com/java-io/file.html

HHDbJON44qf_cBVEM1MlsYTlfqWXfR3ivAf1bgEq

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please follow the code and comments for description :

CODE :

import java.io.BufferedWriter; // required imports
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class StudentInfo { // class to run the code

    public static void main(String[] args) { // driver method
        try { // try catch block of code
            File file = new File("studentData.txt"); // get the file object for the text file, provide the whole path directory if is out of the java program's class directory
            int sId[] = {123, 150, 999, 100, 225, 849, 123, 123, 150, 643}; // required variables
            String fname[] = {"Tim", "Diablo", "Tom", "Buffy", "LeRoy", "Bruce", "Tom", "Dick", "Harry", "Jane"};
            String lname[] = {"Smith", "Ho", "Doe", "Slayer", "Jackson", "Lee", "Smith", "Smith", "Ho", "Doe"};
            String dob[] = {"19960215", "19900420", "19860215", "19960314", "20001114", "19560215", "19890130", "19880615", "19870420", "19860210"};
            String addr[] = {"777 Heaven Way", "666 Hells Gate", "999 NeverMind", "456 Seven", "123 Right Here Alley", "333 Enter The Dragon", "321 Zero", "836 Pick Up Sticks", "577 Eleven", "888 Haight"};

            // if file doesnt exists, then create it
            if (!file.exists()) {
                System.out.println("Creating the File..."); // message
                file.createNewFile(); // call the method to create the file
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile()); // create the writer object for the file
            BufferedWriter bw = new BufferedWriter(fw); // buffered writer class for the file writer
            System.out.println("Writing Data to the File..."); // message
            bw.write(String.format("%-15s %-15s %-15s %-15s %-15s", "StudentID#", "First Name", "Last Name", "DOB", "Address")); // format the header
            bw.write("\n"); // new line character
            for (int i = 0; i < 10; i++) { // iterate over the index values
                int id = sId[i]; // get the respective values
                String fn = fname[i];
                String ln = lname[i];
                String birth = dob[i];
                String add = addr[i];
                bw.write(String.format("%-15s %-15s %-15s %-15s %-15s", id, fn, ln, birth, add)); // write the formatted data to file
                bw.write("\n"); // new line character
            }
            bw.close(); // close the file

            System.out.println("Completed Writing Data to the File."); // message

        } catch (IOException e) { // catch any exceptions
            e.printStackTrace();
        }
    }
}


OUTPUT :

Creating the File...
Writing Data to the File...
Completed Writing Data to the File.


ScreenShot :

Filewriter fw = new FileWriter (file .getAbsolut e File()); // create the writer object for the file BufferedWriter bw-new Bu

student Data.td DOB 19960215 19900420 19860215 19960314 20001114 19560215 19890130 19880615 19870420 19860210 Student ID# Fir

Hope this is helpful.

Add a comment
Know the answer?
Add Answer to:
Create a text file. Use java.io File to input information on 10 students into a text...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT