zoukankan      html  css  js  c++  java
  • Java 字节流实现文件读写操作(InputStream-OutputStream)

    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();
    	}
    }
    
  • 相关阅读:
    php上传文件大小修改
    flex布局
    restful
    mysql之windows忘记密码
    mysql常用命令
    比较级浅析1
    一般副词的位子
    still讲解
    英语学习法
    as的如下用法
  • 原文地址:https://www.cnblogs.com/pengyan5945/p/5218373.html
Copyright © 2011-2022 走看看