FileOutput类---载自《java软件开发》Russel Winder & Graham Roberts
import java.io.*;
/**
* A simple output class to write values to a file of characters.
* If any file errors occur, methods in this class will display
* an error message and terminate the program.
*
* @version 1.1 1999.09.10
* @author Graham Roberts
* @author Russel Winder
*
*/
public class FileOutput {
/**
* Instance variables to store file name.
*/
private String filename = "";
/**
* Instance variables to store stream.
*/
private BufferedWriter writer = null;
/**
* Construct <CODE>FileOutput</CODE> object given a file name.
*/
public FileOutput(final String name) {
filename = name;
try {
writer = new BufferedWriter(new FileWriter(filename));
}
catch(IOException e) {
error("Can't open file: " + filename);
}
}
/**
* Construct <CODE>FileOutput</CODE> object given a
* <CODE>File</CODE> object..
*/
public FileOutput(final File file) {
filename = file.getName();
try {
writer = new BufferedWriter(new FileWriter(filename));
}
catch(IOException e) {
error("Can't open file: " + filename);
}
}
/**
* Close the file when finished
*/
public final synchronized void close() {
try {
writer.close();
}
catch(IOException e) {
error("Can't close file: " + filename);
}
}
/**
* Write an <CODE>int</CODE> value to a file.
*/
public final synchronized void writeInteger(final int i) {
try {
writer.write(Integer.toString(i));
}
catch(IOException e) {
error("writeInteger failed for file: " + filename);
}
}
/**
* Write an <CODE>long</CODE> value to a file.
*/
public final synchronized void writeLong(final long l) {
try {
writer.write(Long.toString(l));
}
catch(IOException e) {
error("writeLong failed for file: " + filename);
}
}
/**
* Write an <CODE>double</CODE> value to a file.
*/
public final synchronized void writeDouble(final double d) {
try {
writer.write(Double.toString(d));
}
catch(IOException e) {
error("writeDouble failed for file: " + filename);
}
}
/**
* Write an <CODE>float</CODE> value to a file.
*/
public final synchronized void writeFlaot(final float f) {
try {
writer.write(Float.toString(f));
}
catch(IOException e) {
error("writeFloat failed for file: " + filename);
}
}
/**
* Write an <CODE>char</CODE> value to a file.
*/
public final synchronized void writeCharacter(final char c) {
try {
writer.write(c);
}
catch(IOException e) {
error("writeCharacter failed for file: " + filename);
}
}
/**
* Write an <CODE>string</CODE> value to a file.
*/
public final synchronized void writeString(final String s) {
try {
writer.write(s);
}
catch(IOException e) {
error("writeString failed for file: " + filename);
}
}
/**
* Write a newline to a file.
*/
public final synchronized void writeNewline() {
try {
writer.write('\n');
}
catch(IOException e) {
error("writeNewline failed for file: " + filename);
}
}
/**
* Deal with a file error
*/
private void error(String msg) {
System.err.println(msg);
System.err.println("Unable to continue executing program.");
System.exit(0);
}
}
/**
* A simple output class to write values to a file of characters.
* If any file errors occur, methods in this class will display
* an error message and terminate the program.
*
* @version 1.1 1999.09.10
* @author Graham Roberts
* @author Russel Winder
*
*/
public class FileOutput {
/**
* Instance variables to store file name.
*/
private String filename = "";
/**
* Instance variables to store stream.
*/
private BufferedWriter writer = null;
/**
* Construct <CODE>FileOutput</CODE> object given a file name.
*/
public FileOutput(final String name) {
filename = name;
try {
writer = new BufferedWriter(new FileWriter(filename));
}
catch(IOException e) {
error("Can't open file: " + filename);
}
}
/**
* Construct <CODE>FileOutput</CODE> object given a
* <CODE>File</CODE> object..
*/
public FileOutput(final File file) {
filename = file.getName();
try {
writer = new BufferedWriter(new FileWriter(filename));
}
catch(IOException e) {
error("Can't open file: " + filename);
}
}
/**
* Close the file when finished
*/
public final synchronized void close() {
try {
writer.close();
}
catch(IOException e) {
error("Can't close file: " + filename);
}
}
/**
* Write an <CODE>int</CODE> value to a file.
*/
public final synchronized void writeInteger(final int i) {
try {
writer.write(Integer.toString(i));
}
catch(IOException e) {
error("writeInteger failed for file: " + filename);
}
}
/**
* Write an <CODE>long</CODE> value to a file.
*/
public final synchronized void writeLong(final long l) {
try {
writer.write(Long.toString(l));
}
catch(IOException e) {
error("writeLong failed for file: " + filename);
}
}
/**
* Write an <CODE>double</CODE> value to a file.
*/
public final synchronized void writeDouble(final double d) {
try {
writer.write(Double.toString(d));
}
catch(IOException e) {
error("writeDouble failed for file: " + filename);
}
}
/**
* Write an <CODE>float</CODE> value to a file.
*/
public final synchronized void writeFlaot(final float f) {
try {
writer.write(Float.toString(f));
}
catch(IOException e) {
error("writeFloat failed for file: " + filename);
}
}
/**
* Write an <CODE>char</CODE> value to a file.
*/
public final synchronized void writeCharacter(final char c) {
try {
writer.write(c);
}
catch(IOException e) {
error("writeCharacter failed for file: " + filename);
}
}
/**
* Write an <CODE>string</CODE> value to a file.
*/
public final synchronized void writeString(final String s) {
try {
writer.write(s);
}
catch(IOException e) {
error("writeString failed for file: " + filename);
}
}
/**
* Write a newline to a file.
*/
public final synchronized void writeNewline() {
try {
writer.write('\n');
}
catch(IOException e) {
error("writeNewline failed for file: " + filename);
}
}
/**
* Deal with a file error
*/
private void error(String msg) {
System.err.println(msg);
System.err.println("Unable to continue executing program.");
System.exit(0);
}
}