The document shows Java code that writes a line of text and numeric data to an output file. It imports FileOutputStream and OutputStream classes, declares a string variable containing sample text and numeric data, creates a FileOutputStream object to write to an output file, converts the string to bytes, writes the bytes to the output stream, prints a message that data was written, and closes the output stream, catching any exceptions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
38 views1 page
Java Programming Week 1
The document shows Java code that writes a line of text and numeric data to an output file. It imports FileOutputStream and OutputStream classes, declares a string variable containing sample text and numeric data, creates a FileOutputStream object to write to an output file, converts the string to bytes, writes the bytes to the output stream, prints a message that data was written, and closes the output stream, catching any exceptions.
public static void main(String args[]) { String data = "This is a line of text inside the file. 123 sample"; try { OutputStream out = new FileOutputStream("output.txt"); // Converts the string into bytes byte[] dataBytes = data.getBytes(); // Writes data to the output stream out.write(dataBytes); System.out.println("Data is written to the file."); // Closes the output stream out.close(); } catch (Exception e) { e.getStackTrace(); } } }