我以前在网上看到过类似的例子:你看看吧
这是一个读写文件的程序,用了两种方式一种是低层流另一种是高层流
import java.io.FileWriter;
import java.io.InputStream;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.FileReader;
public class Untitled1 {
public static void writeFileChar() throws Exception {
FileWriter f = new FileWriter("c:\\aa.txt");
InputStream is = System.in;
int c = is.read();
while (((char) c) != 'x') {
f.write(c);
c = is.read();
}
f.close();
is.close();
}
public static void writeFileString() throws Exception {
FileWriter f = new FileWriter("c:\\aa.txt");
BufferedWriter bwr=new BufferedWriter(f);
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String c = bf.readLine();
while (!c.equals("stop")) {
bwr.write(c+"\r\n");
c = bf.readLine();
}
bwr.close();
f.close();
bf.close();
}
public static void readFileChar() throws Exception {
FileReader f = new FileReader("c:\\aa.txt");
int c = f.read();
while (c!=-1) {
System.out.print((char)c);
c=f.read();
}
f.close();
}
public static void readFileString() throws Exception {
BufferedReader bf = new BufferedReader(new FileReader("c:\\aa.txt"));
String c = bf.readLine();
while (c!=null)
{
System.out.println(c);
c=bf.readLine();
}
bf.close();
}
public static void main(String[] args) throws Exception {
readFileString();
}
} |