Question: Write a String to a File using PrintStream This time build a class WriteString. This class, when instantiated, will write a string to a file

Write a String to a File using PrintStream This time build a class WriteString. This class, when instantiated, will write a string to a file by using a PrintStream object connected to a FileOutputStream Object.

This cd goes in main

WriteString ws = new WriteString(f1.txt,Hello world);

All other code goes in the constructor.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I can't get my WriteString ws = new WriteString("f1.txt","Hello World"); to work, please help. Here is what I have so far..

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.io.*; class WriteString{ public WriteString(){ try{ String filename = "7.1.txt"; File f1 = new File(filename); System.out.println("Size: " + f1.length()); if(f1.length() == 0L) System.out.println("File is Ready to Write"); else{ System.out.println("File is Not Ready to Write, File has data in it"); System.exit(0); } FileOutputStream fout = new FileOutputStream(f1); PrintStream ps = new PrintStream(fout); String s2 = "String to write to File"; ps.println(s2); } catch(IOException ie){ System.out.println("File Error: " + ie); } } public static void main(String[] args){ WriteString ws = new WriteString(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!