zoukankan      html  css  js  c++  java
  • IO 字符流学习

    import java.awt.Frame;
    import java.io.*;
    
    
    public class filewriter {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws IOException{
    		// TODO Auto-generated method stub
    		//test1();
    		test2();
    	}
    	static void test1()throws IOException{
    		FileWriter fw = new FileWriter("F:\fd.txt");
    		fw.write("sfasf");
    		fw.flush();
    		fw.write("zzzzz");
    		fw.flush();
    		fw.close();
    		fw = new FileWriter("F:\fd.txt", true);
    		fw.write("cccc");
    		fw.flush();
    		fw.close();
    	}
    	static void test2(){
    		FileWriter fw = null;
    		try {
    			fw = new FileWriter("F:\fd2.txt");
    			fw.write("wzz");
    		} catch (IOException e) {
    			// TODO: handle exception
    			System.out.println(e.toString());
    		}
    		finally{
    			try {
    				if(fw!=null)
    					fw.close();
    			} catch (IOException e2) {
    				// TODO: handle exception
    				System.out.println(e2.toString());
    			}
    		}
    	}
    }
    
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    
    public class filereader {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args)throws IOException {
    		// TODO Auto-generated method stub
    		//test1()
    		//test2();
    		//test3();
    		//test4();
    		test5();
    	}
    	static void test1()throws IOException{
    		FileReader fr = new FileReader("F:\fd.txt");
    		int c;
    		while((c = fr.read()) != -1){
    			System.out.println((char)c);
    		}
    		fr.close();
    	}
    	static void test2()throws IOException{
    		FileReader fr = new FileReader("F:\fd.txt");
    		char[] buf = new char[3];
    		int x;
    		while((x = fr.read(buf)) != -1){
    			System.out.println(buf);
    		}
    		fr.close();
    	}
    	static void test3()throws IOException{
    		FileReader fReader = new FileReader("F:\fd.txt");
    		char[] buf = new char[2];
    		int x;
    		while((x = fReader.read(buf))!= -1){
    			System.out.print(new String(buf, 0, x));
    		}
    	}
    	static void test4()throws IOException{
    		FileWriter fw = new FileWriter("F:\fd3.txt");
    		FileReader fr = new FileReader("F:\fd2.txt");
    		int x;
    		while((x = fr.read())!=-1)
    		{
    			fw.write(x);
    		}
    		fw.close();
    		fr.close();
    	}
    	static void test5(){
    		FileWriter fw = null;
    		FileReader fr = null;
    		try {
    			fw = new FileWriter("F:\fd3.txt");
    			fr = new FileReader("F:\fd2.txt");
    			int len = 0;
    			char[] buf = new char[1024];
    			while((len = fr.read(buf)) != -1){
    				fw.write(buf, 0, len);
    			}
    		} catch (Exception e) {
    			// TODO: handle exception
    			System.out.println(e.toString());
    		}
    		finally{
    			try {
    				if(fw != null)
    					fw.close();
    			} catch (IOException e2) {
    				// TODO: handle exception
    				System.out.println(e2.toString());
    			}
    			try {
    				if(fr!=null)
    					fr.close();
    			} catch (IOException e2) {
    				// TODO: handle exception
    				System.out.println(e2.toString());
    			}
    		}
    	}
    }
    

    FileWriter FileReader 文本的续写 文本的读取 文本的拷贝

    BuffWriter Buffreader 缓冲

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    
    public class bufferwrier {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		test1();
    	}
    	static void test1(){
    		BufferedReader bfr = null;
    		BufferedWriter bfw = null;
    		try {
    			
    			bfr = new BufferedReader(new FileReader("F:\fd.txt"));
    			bfw = new BufferedWriter(new FileWriter("F:\wzz.txt"));
    			String s = null;
    			while((s = bfr.readLine())!=null){
    				bfw.write(s);
    				bfw.newLine();
    				bfw.flush();
    			}
    		} catch (IOException e) {
    			// TODO: handle exception
    			throw new RuntimeException("keng");
    		}
    		finally{
    			try {
    				if(bfr!=null)
    					bfr.close();
    			} catch (IOException e2) {
    				// TODO: handle exception
    				throw new RuntimeException("keng");
    			}
    			try {
    				if(bfw!=null)
    					bfw.close();
    			} catch (IOException e) {
    				// TODO: handle exception
    				throw new RuntimeException("keng");
    			}
    		}
    	}
    }
    

    装饰设计模式:

    当想要对已有的对象进行功能增强时。

    能够定义类,将已有对象传入。基于已有的功能。并提供加强功能。

    那么自己定义的该类称为装饰类

    装饰类一般会通过构造方法接受被装饰的对象。

    并基于被装饰的对象的功能。提供更强的功能。

    装饰模式比继承要灵活,避免了继承体系臃肿。

    并且减少了类与类之间的关系。

    装饰类由于增强已有对象,具备的功能和已有的是同样的。仅仅只是提供了更强功能。

    因此,装饰装修通常属于制度



    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    [转]Cordova + Ionic in Visual Studio
    [转]Getting Start With Node.JS Tools For Visual Studio
    TypeScript
    [转]NPOI TestFunctionRegistry.cs
    [转]Cordova android框架详解
    [转]POI : How to Create and Use User Defined Functions
    [转]用NPOI操作EXCEL--通过NPOI获得公式的返回值
    [转]Formatting the detail section to display multiple columns (水晶报表 rpt 一页多列)
    [转]Creating Mailing Labels in SQL Server Reporting Services (rdlc 数据1页 2竖排 显示)
    [转]Tetris(俄罗斯方块) in jQuery/JavaScript!
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4851491.html
Copyright © 2011-2022 走看看