灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2585回复:0

java例程练习(缓冲流)

楼主#
更多 发布于:2012-09-08 09:42

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("读取异常");
        }
    }
}
[java]
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("读取异常");
        }
        
    }
}







喜欢0 评分0
游客

返回顶部