goback add

java例程练习(缓冲流)

3380 点击·0 回帖
灯火互联
楼主

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