|
JNISpice version 2.0.0 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectspice.basic.TextWriter
public class TextWriter
Class TextWriter provides a very simple interface that supports writing lines of text to files.
import java.io.*; import spice.basic.*; // // Create a new text file, write to it, then // read back what was written. The name of the // file is supplied on the command line. // class TextIOEx1 { // // Load the JNISpice shared library. // static { System.loadLibrary( "JNISpice" ); } public static void main ( String[] args ) throws SpiceException { // // Create a small text file. // final int NLINES = 10; String fname = args[0]; TextWriter tw = new TextWriter( fname ); for ( int i = 0; i < NLINES; i++ ) { String line = "This is line " + i + " of the text file."; tw.writeLine( line ); } // // Close the TextWriter. // tw.close(); // // Now read the text file and print each line as we // read it. // TextReader tr = new TextReader( fname ); String line = tr.getLine(); while( line != null ) { System.out.println( line ); line = tr.getLine(); } // // Close the TextReader. // tr.close(); } }
The program can be executed using the command
java TextWriterEx1 ex1.txt
When run on a PC/Linux/java 1.6.0_14/gcc platform, the output from this program was a file named "ex1.txt" containing the text:
This is line 0 of the text file. This is line 1 of the text file. This is line 2 of the text file. This is line 3 of the text file. This is line 4 of the text file. This is line 5 of the text file. This is line 6 of the text file. This is line 7 of the text file. This is line 8 of the text file. This is line 9 of the text file.
Version 1.0.0 21-DEC-2009 (NJB)
Constructor Summary | |
---|---|
TextWriter(java.lang.String filename)
Create a TextWriter instance associated with a specified file. |
Method Summary | |
---|---|
void |
close()
Close this TestWriter instance. |
void |
writeLine(java.lang.String line)
Write a string to the file associated with this TextWriter instance. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public TextWriter(java.lang.String filename) throws SpiceException
SpiceException
Method Detail |
---|
public void writeLine(java.lang.String line) throws SpiceException
The caller is responsible for calling close()
after all
lines have been written to the file.
SpiceException
public void close()
|
JNISpice version 2.0.0 |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |