Writing text output to a file:
In previous versions of Java writing text to a file was a little complicated; the newest version of Java makes the whole process easier. The PrintWriter class provides methods for writing text and primitives to a named file. The constructor:
PrintWriter(String);
creates a file into which data can be written. For example, the code:
PrintWriter pw = new PrintWriter("Hello.txt");
will create a PrintWriter object that will write data to the text file Hello.txt. For example, the code:
pw.println("The first line");
pw.println("The second line");
will print two lines to the file. The only other operation that is needed with such a PrintWriter object is to close the file down by means of the close method so, for example, the code:
pw.close();
will close the file Hello.txt down.
The following class demonstrates the use of text output to a disk file. It generates a multiplication table and writes it to the file.
import java.io.*;
public class MultiplicationTable
{
PrintWriter out;
final static int MAX_LINES = 10;
public void openFile (String fileName) throws FileNotFoundException
{
out = new PrintWriter(fileName);
}
public void printMultiplicationTable ()
{
out.println("Multiplication Table");
for (int i = 1; i <= MAX_LINES; i++)
{
out.print("line" + i + ": ");
for (int j = 1; j <= MAX_LINES; j++)
{
out.print("\t");
out.print(i * j); //overloaded print()
}
out.println();
}
}
public void closeFile ()
{
out.close();
}
} // end class MultiplicationTable
The MultiplicationTable class can be used as follows:
import java.io.*;
public class TestMultiplicationTable
{
public static void main (String []args) throws FileNotFoundException
{
MultiplicationTable table = new MultiplicationTable(); table.openFile("table.txt"); table.printMultiplicationTable();
table.closeFile();
} // end main
} // end class TestMultiplicationTable
Note the use of the closeFile method, which invokes the close method of the PrintWriter class. This is known as fiushing the buffer. This is essential to ensure that all the data is written to the file and does not remain unprocessed in the buffer - without this the file may be incomplete or even empty.
It can be done explicitly by invoking the fiush method, for example, as follows:
out.print("line" + i + ": ");
out.flush();
It can also be done by using the println method.
The constructor for PrintWriter(String) within the method openFile will throw a FileNotFoundException if there is a problem either creating or accessing the file that is represented by the string argument.
The class PrintWriter is unusual among the stream classes in that its methods do not throw exceptions of type IOException when an error occurs. Instead, PrintWriter has a method called checkError, which returns a boolean value to indicate whether a problem has occurred when executing one of the methods of a given PrintWriter object. For example, using an extract from the previous example, we could check for an error like this:
out.print("line" + i + ": ");
if (out.checkError())
{
System.err.println("Faulty file output");
out.close();
System.exit(-1);
}
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Writing text output to a file homework and assignments? Live Writing text output to a file experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Writing text output to a file homework help, java assignment help and Writing text output to a file projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours