Java Reference
In-Depth Information
byte byteData;
while ((byteData = (byte) fin.read()) != -1) {
System.out.print((char) byteData);
}
}
catch (FileNotFoundException e) {
FileUtil.printFileNotFoundMsg(dataSourceFile);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
STRANGE fits of passion have I known:
And I will dare to tell,
But in the lover's ear alone,
What once to me befell.
Writing Data to a File Using an Output Stream
In this section, I will show you how to write a stanza from the poem Lucy by William Wordsworth to a file named
luci2.txt . The stanza is as follows:
When she I loved look'd every day
Fresh as a rose in June,
I to her cottage bent my way,
Beneath an evening moon.
The following steps are needed to write to the file:
Identify the data sink, which is the file to which the data will be written.
Create an output stream using the file.
Write the data to the file using the output stream.
Flush the output stream.
Close the output stream.
Identifying the Data Sink
Your data sink could be simply the file path as a string or a File object representing the pathname of the file. Let's
assume that the luci2.txt file is in the current working directory.
// The data sink
String destFile = "luci2.txt";
 
Search WWH ::




Custom Search