代码
package com.test.san;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test {
/* byte bs[];
public void readFile() {

try {
// 1:构造管道
FileInputStream filein = new FileInputStream("/home/srx/suout.txt");
// 2:源内的节点流,看不见输出
// 3:
// byte bs[] = new byte[100];

try {
bs = new byte[filein.available()];

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {

filein.read(bs);
// 处理
// System.out.println("filecontentIs" + String.valueOf(bs));
System.out.println("filecontentIs" + new String(bs));
// 4:关闭
filein.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
*/
public void writeFile() {
//String s = "heihei" + "\n" + "haha";
// String s = "heihei\nhaha";
String s = "heihei \n haha";
try {
// 1构建一个管道
FileOutputStream fileout = new FileOutputStream(
"/home/srx/suout.txt");
// 2从源输出流
try {
fileout.write(s.getBytes());
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String args[]) {
Test test
= new Test();
// test.readFile();
test.writeFile();
}
}
文件的读写可以分为以下四步:

1、构建源和目的地的连接

2、从源输出流

3、目的地接收流

4、关闭连接

当读文件的时候,输出为这个文件的内容

当写文件的时候,文件的内容为:

heihei
haha

posted on 2010-09-15 10:05  snowdrop  阅读(198)  评论(0)    收藏  举报