To study and implement File IO in Java 4 Add the following method to your

To study and implement file io in java 4 add the

This preview shows page 23 - 28 out of 73 pages.

To study and implement File I/O in Java 4. Add the following method to your FileTest class: Compile it. Add "throws" statements as necessary. Modify your main to run the consoleRead method, and recompile. Run your program. What does it do? CODE: package filetext; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class FileText { public void fileWrite() throws IOException { File dstFile = new File("outputfile.txt"); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(dstFile))); out.println("Arlene 19 3.8"); out.println("Bill 22 3.5"); out.println("Marilyn 15 3.9"); out.println("Bryan 35 1.1"); out.println("Buzz 6 4.0"); 23
Image of page 23
Lab2 To study and implement File I/O in Java out.close(); } public void consoleRead() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("What is your first name? "); String first = in.readLine(); System.out.print("What is your last name? "); String last = in.readLine(); System.out.println("Your name is " + last + ", " + first + "."); } public static void main(String[] args) throws IOException { FileText fileTest = new FileText(); fileTest.fileWrite(); fileTest.consoleRead(); } 5. Add the following method to FileTest. public void fileRead() throws IOException { File srcFile = new File("outputfile.txt"); BufferedReader in = new BufferedReader(new FileReader(srcFile)); String text = in.readLine(); System.out.println(text); in.close(); } Run the method. What do you see? Modify this method to print out the names, ages, and gpas of the five people you stored back in Task 1. 24
Image of page 24
Lab2 To study and implement File I/O in Java CODE: package filetext; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class FileText { public void fileWrite() throws IOException { File dstFile = new File("outputfile.txt"); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(dstFile))); out.println("Arlene 19 3.8"); out.println("Bill 22 3.5"); out.println("Marilyn 15 3.9"); out.println("Bryan 35 1.1"); out.println("Buzz 6 4.0"); out.close(); } 25
Image of page 25
Lab2 To study and implement File I/O in Java public void consoleRead() throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("What is your first name? "); String first = in.readLine(); System.out.print("What is your last name? "); String last = in.readLine(); System.out.println("Your name is " + last + ", " + first + "."); } public void fileRead() throws IOException { File srcFile = new File("outputfile.txt"); BufferedReader in = new BufferedReader(new FileReader(srcFile)); String text = in.readLine(); while (text != null) { System.out.println(text); // read next line text = in.readLine(); } in.close(); } public static void main(String[] args) throws IOException { // TODO code application logic here 26
Image of page 26
Lab2 To study and implement File I/O in Java FileText fileTest = new FileText(); fileTest.fileWrite(); fileTest.consoleRead(); fileTest.fileRead(); } } 27
Image of page 27
Image of page 28

You've reached the end of your free preview.

Want to read all 73 pages?

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors