JAVA I/O
java i/o有那么多的类,平时该怎么用啊?
请举例说明!!!
推荐阅读
package myprojects.fileio;
import java.io.filereader;
public class fileio {
public fileio() {
}
public void getfilestrings() {
try {
char[] b= new char[100];
filereader fr = new filereader("c:/abc.txt");
fr.read(b) ;
system.out.println(b);
//fr.read(b, 1, 100);
} catch (exception e) {
}
}
public static void main(string args[]) {
system.out.println("starting fileio...");
fileio fileio1 = new fileio();
fileio1.getfilestrings();
}
}
我认为比较经典的一个i/o的例子,着重看关于io的代码
//: c12:iostreamdemo.java
// typical i/o stream configurations.
// {runbyhand}
// {clean: iodemo.out,data.txt,rtest.dat}
import com.bruceeckel.simpletest.*;
import java.io.*;
public class iostreamdemo {
private static test monitor = new test();
// 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.writeutf("that was pi");
out2.writedouble(1.41413);
out2.writeutf("square root of 2");
out2.close();
datainputstream in5 = new datainputstream(
new bufferedinputstream(
new fileinputstream("data.txt")));
// must use datainputstream for data:
system.out.println(in5.readdouble());
// only readutf() will recover the
// java-utf string properly:
system.out.println(in5.readutf());
// read the following double and string:
system.out.println(in5.readdouble());
system.out.println(in5.readutf());
} catch(eofexception e) {
throw new runtimeexception(e);
}
// 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();
monitor.expect("iostreamdemo.out");
}
} ///:~


讨论区