您的位置:首页 > 编程语言 > Java开发

java例程练习(缓冲流)

2012-05-02 16:59 288 查看
import java.io.*;

public class Test {
public static void main(String[] args) {

try {
FileInputStream fis = new FileInputStream("C:/java/Test.java");
BufferedInputStream bis = new BufferedInputStream(fis);

int c = 0;
System.out.println(bis.read());
System.out.println(bis.read());

bis.mark(100);
for(int i = 0; i <= 10 && (c = bis.read()) != -1; i++) {
System.out.print(c+ " ");
}

System.out.println();

bis.reset();
for(int i = 0; i <= 10 &&(c = bis.read()) != -1; i++) {
System.out.print(c + " ");
}
bis.close();
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
} catch (IOException e) {
System.out.println("读取异常");
}
}
}
import java.io.*;

public class TTest {
public static void main(String[] args) {
try {
BufferedWriter bw =
new BufferedWriter(new FileWriter("C:/java/Test_Bak.txt"));

BufferedReader br =
new BufferedReader(new FileReader("C:/java/Test.java"));

String s = null;
for(int i = 0; i <= 100; i++) {
s = String.valueOf(Math.random());
bw.write(s);
bw.newLine();
}
bw.flush();

while((s = br.readLine()) != null) {
System.out.println(s);
}
bw.close();
br.close();

} catch (FileNotFoundException e) {
System.out.println("文件不存在");
} catch (IOException e) {
System.out.println("读取异常");
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: