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