Java IO 缓冲流的读入和写出

Java IO 缓冲流的读入和写出

缓冲流也成为处理流,目的是提高程序读取和写出的性能。缓冲流也分为字节缓冲流和字符缓冲流。

1 字符缓冲流,类包含BufferedInputStream 和 BufferedOutputStream

1.1 BufferedInputStream 源码

查看源码,BufferedInputStream相对于InputStream没有新增方法,所以在使用时可以使用多态。

BufferedInputStream的demo如下,与InputStream的demo对比
/**

  • 缓冲流读取文件 jdk 7 新特性
  • @param file
  •        文件对象
    

/
public static void readFileWithJdk7(File file) {
// java 7 新特性 try-with-resource
// 不用手动释放资源,程序自动释放,因为InputStream实现了AutoCloseable接口
try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
byte[] buffer = new byte[1024];
int len = 0;
while (-1 != (len = is.read(buffer))) {
System.out.println(new String(buffer, 0, len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/
*

  • 缓冲流读取文件
  • @param file
  •        文件对象
    

/
public static void readFile(File file) {
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
int len = 0;
while (-1 != (len = is.read(buffer))) {
System.out.println(new String(buffer, 0, len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/
*

  • @param filePath
  •        文件路径
    

*/
public static void readFile(String filePath) {
readFile(new File(filePath));
}

1.2 BufferedOutputStream 源码

查看源码,BufferedOutputStream向比较与OutputStream没有增加新方法,故可以使用多态。

BufferedOutputStream的demo如下,与OutputStream的demo对比
/**

  • 缓冲流写出文件 jdk 7 新特性
  • @param file
  •        指定输出文件
    
  • @param msg
  •        输出内容
    
  • @param append
  •        是否追加,true 追加 false 不追加
    

/
public static void writeToFileJdk7(File file, String msg, boolean append) {
// java 7 新特性 try-with-resource
// 不用手动释放资源,程序自动释放,因为OutputStream实现了AutoCloseable接口
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
os.write(msg.getBytes(), 0, msg.getBytes().length);
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/
*

  • 缓冲流写出文件
  • @param file
  •        指定输出文件
    
  • @param msg
  •        输出内容
    
  • @param append
  •        是否追加,true 追加 false 不追加
    

/
public static void writeToFile(File file, String msg, boolean append) {
OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(file));
os.write(msg.getBytes(), 0, msg.getBytes().length);
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != os) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/
*
*

  • @param filePath
  •        指定输出文件路径
    
  • @param msg
  •        输出内容
    
  • @param append
  •        是否追加,true 追加 false 不追加
    

*/
public static void writeToFile(String filePath, String msg, boolean append) {
writeToFile(new File(filePath), msg, append);
}

2 字符缓冲流,类包含BufferedReader 和 BufferedWriter

2.1 BufferedReader源码

相对于Reader类,BufferedReader新增了以下方法,所以不能使用多态。

/**

  • 读取一行字符串
    */
    public String readLine() throws IOException {
    return readLine(false);
    }

BufferedReader的demo如下,与Reader的demo对比
/**
*

  • 使用BufferedReader字符缓冲流读取文件
  • 使用BufferedReader新增方法readLine(),所以不能使用多态
  • @param file
  •        文件对象
    

/
public static void readFile(File file) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String line = null;
while (null != (line = br.readLine())) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != br) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/
*
*

  • 使用BufferedReader字符缓冲流读取文件
  • 使用BufferedReader新增方法readLine(),所以不能使用多态
  • @param file
  •        文件对象
    

/
public static void readFileWithJdk7(File file) {
// java 7 新特性 try-with-resource
// 不用手动释放资源,程序自动释放,因为Reader实现了AutoCloseable接口
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line = null;
while (null != (line = reader.readLine())) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/
*
*

  • @param filePath
  •        文件路径
    

*/
public static void readFile(String filePath) {
readFile(new File(filePath));
}

2.2 BufferedWriter源码

相对于Writer类,新增以下方法,所以使用新增方法不能使用多态。

/**

  • 输出一个换行符 ‘\n’
    */
    public void newLine() throws IOException {
    write(lineSeparator);
    }

BufferedWriter的demo如下,与Writer的demo对比
/**

  • jdk 7 新特性
  • 使用BufferedWriter字符缓冲流写出文件
  • BufferedWriter 新增方法newLine(),所以不能使用多态
  • @param file
  •        指定输出文件
    
  • @param msg
  •        输出内容
    
  • @param append
  •        是否追加,true 追加 false 不追加
    

/
public static void writeToFileWithJdk7(File file, String msg, boolean append) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(msg);
writer.newLine();
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/
*
*

  • 使用BufferedWriter字符缓冲流写出文件
  • BufferedWriter 新增方法newLine(),所以不能使用多态
  • @param file
  •        指定输出文件
    
  • @param msg
  •        输出内容
    
  • @param append
  •        是否追加,true 追加 false 不追加
    

/
public static void writeToFile(File file, String msg, boolean append) {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
writer.write(msg);
writer.newLine();
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != writer) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/
*
*

  • @param filePath
  •        指定输出文件路径
    
  • @param msg
  •        输出内容
    
  • @param append
  •        是否追加,true 追加 false 不追加
    

*/
public static void writeToFile(String filePath, String msg, boolean append) {
writeToFile(new File(filePath), msg, append);
}

原文:https://blog.csdn.net/hongguo_cheng/article/details/52098930

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值