zoukankan      html  css  js  c++  java
  • Java 字符流实现文件读写操作(FileReader-FileWriter)

    Java 字符流实现文件读写操作(FileReader-FileWriter)

    备注:字符流效率高,但是没有字节流底层

    字节流地址:http://pengyan5945.iteye.com/blog/1092120

    package com.frank.io;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Reader;
    import java.io.Writer;
    
    /** 
     * author:pengyan  
     * date:Jun 15, 2011  
     * file:WriterReaderTest.java 
     */ 
    public class WriterReaderTest {
    
    	File f=new File("E:\abc.txt");
    	
    	public static void main(String[] args) throws IOException{
    		WriterReaderTest test=new WriterReaderTest();
    		test.writeFile("Java字符流读写文件测试!");
    		test.readFile();
    	}
    	private void readFile()  throws IOException{
    		//reate BufferedReader with file  
    		Reader r=new BufferedReader(new FileReader(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=r.read())!=-1) {
               //if not end,the total content add the value of the stream read this time  
    			str+=(char)temp;
    		}
    		//show the content of the file
    		System.out.println("文件内容:"+str);
    	}
    	private void writeFile(String content) throws IOException {
    		if (f.exists()==false) {
    			f.createNewFile();//create file if not exist 
    		}
    		//create FileWriter with file 
    		Writer w=new FileWriter(f);
    		//write file
    		w.write(content);
    		//flush this stream  
    		w.flush();
    		//close this stream  
    		w.close();
    	}
    
    }
    
  • 相关阅读:
    spark shuffle过程分析
    Android实现网络多线程断点续传下载
    幻世(OurDream)TM 2D图形引擎开通捐赠渠道
    MDA模型定义及扩展
    STL在迭代的过程中,删除指定的元素
    c# POST和GET方式通过server地址提交数据
    Python爬虫抓取csdn博客
    Word Ladder II
    HDU 4183 Pahom on Water(最大流SAP)
    poj1011 Sticks
  • 原文地址:https://www.cnblogs.com/pengyan5945/p/5218372.html
Copyright © 2011-2022 走看看