DR wrote:
>new FileWriter(fileName).write(yourString);
>Seems pretty simple to me.
It "seems pretty simple" because it's a common beginner pitfall:
http://www.hut.fi/~tratilai/tij2/Chap11.htm (Bruce Eckel's Thinking in Java ch11)
File output
First, a FileWriter is created to connect to the file. You'll virtually always want to buffer the output by wrapping it in a BufferedWriter (try removing this wrapping to see the impact on the performance-buffering tends to dramatically increase performance of I/O operations). Then for the formatting it's turned into a PrintWriter. The data file created this way is readable as an ordinary text file.
----------------------------------------------------------------
http://servlet.java.sun.com/javaone/javaone2000/pdfs/TS-1632.pdf (Sun's Designing for Performance on the JavaTM Platform)
Common Pitfalls
* Unbuffered I/O
-Standard readers/writers operate byte-at-a-time
-Wrap in buffered equivalents-
Benefit: fewer expensive I/O calls
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter("out")));
----------------------------------------------------------------
Here is the *correct* way to do Java I/O:
http://www.hut.fi/~tratilai/tij2/Chap11.htm (Bruce Eckel Thinking in Java ch11)
//: c11:IOStreamDemo.java
// Typical I/O stream configurations.
import java.io.*;
public class IOStreamDemo {
// Throw exceptions to console:
public static void main(String[] args)
throws IOException {
// 1. Reading input by lines:
BufferedReader in =
new BufferedReader(
new FileReader("IOStreamDemo.java"));
String s, s2 = new String();
while((s = in.readLine())!= null)
s2 += s + "\n";
in.close();
// 1b. Reading standard input:
BufferedReader stdin =
new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter a line:");
System.out.println(stdin.readLine());
// 2. Input from memory
StringReader in2 = new StringReader(s2);
int c;
while((c = in2.read()) != -1)
System.out.print((char)c);
// 3. Formatted memory input
try {
DataInputStream in3 =
new DataInputStream(
new ByteArrayInputStream(s2.getBytes()));
while(true)
System.out.print((char)in3.readByte());
} catch(EOFException e) {
System.err.println("End of stream");
}
// 4. File output
try {
BufferedReader in4 =
new BufferedReader(
new StringReader(s2));
PrintWriter out1 =
new PrintWriter(
new BufferedWriter(
new FileWriter("IODemo.out")));
int lineCount = 1;
while((s = in4.readLine()) != null )
out1.println(lineCount++ + ": " + s);
out1.close();
} catch(EOFException e) {
System.err.println("End of stream");
}
// 5. Storing & recovering data
try {
DataOutputStream out2 =
new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("Data.txt")));
out2.writeDouble(3.14159);
out2.writeCharswriteUTF("That was pi\n");
out2.writeBytes("That was pi\n");
out2.writeDouble(1.41413);
out2.writeUTF("Square root of 2");
out2.close();
DataInputStream in5 =
new DataInputStream(
new BufferedInputStream(
new FileInputStream("Data.txt")));
BufferedReader in5br =
new BufferedReader(
new InputStreamReader(in5));
// Must use DataInputStream for data:
System.out.println(in5.readDouble());
// Only readUTF() will recover the
// Java-UTF String properly:
// Can now use the "proper" readLine():
System.out.println(in5br.readLinereadUTF());
// But the line comes out funny.
// The one created with writeBytes is OK:
System.out.println(in5br.readLine());
// Read the following double and String:
System.out.println(in5.readDouble());
System.out.println(in5.readUTF());
} catch(EOFException e) {
System.err.println("End of stream");
}
// 6. Reading/writing random access files
RandomAccessFile rf =
new RandomAccessFile("rtest.dat", "rw");
for(int i = 0; i < 10; i++)
rf.writeDouble(i*1.414);
rf.close();
rf =
new RandomAccessFile("rtest.dat", "rw");
rf.seek(5*8);
rf.writeDouble(47.0001);
rf.close();
rf =
new RandomAccessFile("rtest.dat", "r");
for(int i = 0; i < 10; i++)
System.out.println(
"Value " + i + ": " +
rf.readDouble());
rf.close();
}
} ///:~
-----Original Message-----
From: David Rosenstrauch [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, August 21, 2002 3:32 PM
To: JDJList
Subject: [jdjlist] RE: Java IO Question
At 02:37 PM 8/21/2002 -0700, you wrote:
>This is exactly what I am talking about. Writing to file using Java IO is
>like using a supercomputer to add 1 + 1. Java is a full-featured language
>for the enterprise. However, this means that every little thing now
>requires a "sledgehammer" approach to solve it.
>
>Although some people (possibly those bitten by radioactive spiders?) will
>froth at the mouth from this thought, I want to state once again: I
>am NOT trying to solve the world's hunger problems or create peace in the
>Middle East. I am just trying to write a String to a file. That is
>all. 99% of all the file-writing code just wants to do that and nothing
>else. Yet this "very simple" functionality is not at all that simple in
>Java. In fact it is awkward for almost everyone who does not deal with
>this every day.
new FileWriter(fileName).write(yourString);
Seems pretty simple to me.
DR
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm
http://www.sys-con.com/java/list.cfm
