java中缓冲读写buffer基础程序

来源:互联网 发布:mac好用的office软件 编辑:程序博客网 时间:2024/05/18 18:19

package com.buffered;

import java.io.*;

public class TestBufferedIO {

public static void main(String[] args) {

String path = "d:/SQLHelp.java";

TestBufferedIO t = new TestBufferedIO();

try {

// t.read2(path);

//t.read3();

//PrintStream ps = System.out;

PrintStream ps = new PrintStream(new File("d:/SQLHelp1.java")); t.list(path, ps);

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 测试字节流的处理流 * * @param path * @throws IOException */ public void read1(String path) throws IOException { File f = new File(path); InputStream is = new FileInputStream(f); BufferedInputStream bis = new BufferedInputStream(is); byte[] bs = new byte[20]; int flag = -1; while ((flag = bis.read(bs)) != -1) { String msg = new String(bs); System.out.print(msg); } bis.close(); is.close(); } /** * 测试打印流 * * @param f * @param fs




public static void list(String f, PrintStream fs) throws IOException { try { BufferedReader br = new BufferedReader(new FileReader(f)); String s = null; while ((s = br.readLine()) != null) { fs.println(s); } br.close(); } catch (IOException e) { } } fs.println("无法读取文件"); /** * 测试字符流的处理流 * * @param path * @throws IOException */ public void read2(String path) throws IOException { File f = new File(path); Reader reader = new FileReader(f); BufferedReader br = new BufferedReader(reader); String msg = null; while ((msg = br.readLine()) != null) { System.out.println(msg); } br.close(); reader.close(); } /** * 接收键盘输入 */ public void read3() throws IOException { System.out.println("用户请输入内容:"); InputStream is = System.in; BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String msg = null; while ((msg = reader.readLine()) != null) { System.out.println(msg); } reader.close();


} }

public static void list(String f, PrintStream fs) throws IOException { try { BufferedReader br = new BufferedReader(new FileReader(f)); String s = null; while ((s = br.readLine()) != null) { fs.println(s); } br.close(); } catch (IOException e) { } } fs.println("无法读取文件"); /** * 测试字符流的处理流 * * @param path * @throws IOException */ public void read2(String path) throws IOException { File f = new File(path); Reader reader = new FileReader(f); BufferedReader br = new BufferedReader(reader); String msg = null; while ((msg = br.readLine()) != null) { System.out.println(msg); } br.close(); reader.close(); } /** * 接收键盘输入 */ public void read3() throws IOException { System.out.println("用户请输入内容:"); InputStream is = System.in; BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String msg = null; while ((msg = reader.readLine()) != null) { System.out.println(msg); } reader.close();


} }


对于学习有困难不知道如何提升自己可以加扣:578024144 进行交流得到帮助,获取学习资料
0 0
原创粉丝点击