Java 字节流实现文件读写操作(InputStream-OutputStream)
2014-07-25 10:36
507 查看
Java 字节流实现文件读写操作(InputStream-OutputStream)
备注:字节流比字符流底层,但是效率底下。
字符流地址:http://pengyan5945.iteye.com/blog/1092354
备注:字节流比字符流底层,但是效率底下。
字符流地址: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(); } }
相关文章推荐
- Java 字节流实现文件读写操作(InputStream-OutputStream)
- Java 字符流实现文件读写操作(FileReader-FileWriter)
- Java——FileInputStream&FileOutputStream字节流实现文件复制
- Java实现CSV格式文件的读写(操作API)
- JAVA中inputStream/OutputStream字节输入流、输出流读写文件
- java中文件的读写操作工具类的实现
- Java文件读写数据流大全(InputStream、OutputStream、FileInpuStream)
- Java文件读写数据流大全(InputStream、OutputStream、FileInpuStream)
- 用JAVA源代码实现文件的读写操作.
- 自己总结的Java实现文件的读写操作
- Java文件读写操作具体代码实现
- java 字符流实现文件读写操作(FileReader-FileWriter)
- Java 字符流实现文件读写操作(FileReader-FileWriter)
- java写文件读写操作(IO流,字节流)
- java文件读写操作-字节流
- Java IO操作——字节流(OutputStream、InputStream)和字符流(Writer、Reader)
- Java IO操作——字节流(OutputStream、InputStream)和字符流(Writer、Reader)
- Java基础之IO流,以字节流的方式操作读写文件FileOutputStream和FileInputStream的使用
- Java文件读写数据流大全(InputStream、OutputStream、FileInpuStream)
- java开发之读写txt文件操作的实现