Java 字节流实现文件读写操作(InputStream-OutputStream)

备注:字节流比字符流底层,但是效率底下。

字符流地址:http://pengyan5945.iteye.com/blog/1092354

package com.frank.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; /**
* author:pengyan
* date:Jun 15, 2011
* file:InputOutputStreamTest.java
*/
public class InputOutputStreamTest{
File f=new File("F:\\abc.txt");
public static void main(String[] args) throws Exception{
InputOutputStreamTest test=new InputOutputStreamTest();
test.outputFile("Java字节流读写文件测试!");
test.inputFile();
}
private void inputFile() throws IOException{
//reate FileInputStream with file
FileInputStream fis=new FileInputStream(f);
//in order to receive the value of this stream read every time
int temp=0;
//the all content of this stream read
String str="";
while((temp=fis.read())!=-1){
//if not end,the total content add the value of the stream read this time
str+=(char)temp;
}
//create a new String object with the correct encode
System.out.println("文件内容:"+new String(str.getBytes("ISO-8859-1")));
}
private void outputFile(String content) throws IOException {
if (f.exists()==false) {
f.createNewFile();//create file if not exist
}
//create FileOutputStream with file
FileOutputStream fos=new FileOutputStream(f);
//output file
fos.write(content.getBytes());
//flush this stream
fos.flush();
//close this stream
fos.close();
}
}

最新文章

  1. IOS客户端UIwebview下web页面闪屏问题
  2. MySQL中函数CONCAT及GROUP_CONCAT
  3. loading动画效果记录
  4. QT的安装和配置及helloqt程序的编写时遇到的问题
  5. C语言编程心得
  6. [纯小白学习OpenCV系列]官方例程00:世界观与方法论
  7. 序列化在Netty中的使用
  8. tomcat 一个服务 多端口网站
  9. HDU 4932 贪心
  10. openstack iptables
  11. iOS KVO & KVC
  12. JQuery学习(3)
  13. HDU 4668 Finding string (解析字符串 + KMP)
  14. log4cxx第三篇----使用多个logger
  15. 通过 PHP 连接China Azure Blob 存储
  16. 安卓开发学习笔记(五):史上最简单且华丽地实现Android Stutio当中Webview控件https/http协议的方法
  17. (一)Java工程化--Maven基础
  18. 字符串排序简单的工具类,数组转list,list转数组
  19. Visual Studio 开发(一):安装配置Visual Studio Code
  20. Ubuntu环境下mysql常见的操作

热门文章

  1. 一款基于jQuery/CSS3实现拼图效果的相册
  2. Android概览
  3. CentOS7 安装Docker报错
  4. javascript函数中的实例对象、类对象、局部变量(局部函数)
  5. 如何使用nodejs发邮件
  6. 完整HttpHelper类
  7. 升级树莓派archlinux系统到新sd卡
  8. nginx +lua +redis 构建自动缓存系统
  9. Mac 下抓包工具 Charles 修改特定请求
  10. leetcode 题解 Add Two Numbers(两个单链表求和)