24. 입출력 패키지의 인쇄 흐름, 통합 흐름

5005 단어 PrintWriter
1. PrintWriter 문자 인쇄 흐름
 
PrintStream과 매우 유사함
 
package day13;

import java.io.*;

public class PrintWriterDemo {

	public static void main(String[] args) throws IOException {
		BufferedReader br = 
				new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(new FileWriter("E:/in.txt"));
		String line = null;
		while((line=br.readLine())!=null)
		{
			if(line.equals("over"))
				break;
			pw.println(line.toUpperCase());
		}
		pw.close();
		br.close();
	}
}

 
2. SequenceInputStream 통합 흐름
 
  • SequenceInputStream은 다른 입력 흐름의 논리적 연결을 나타냅니다.
  • 입력 흐름의 질서정연한 집합부터 시작하여 첫 번째 입력 흐름부터 파일 끝까지 읽기
  • 이어서 두 번째 입력 흐름에서 읽고 마지막으로 포함된 입력 흐름의 파일 끝에 도달할 때까지 순서대로 유추한다. 

  •  
    public class SequenceInputStream extends InputStream
    {
    	//                  SequenceInputStream(           ,    s1,     s2),
    	//      SequenceInputStream      
    	public SequenceInputStream(InputStream s1,
                               InputStream s2){}
    
    	//               SequenceInputStream,               InputStream     Enumeration    。
    	//                ,      SequenceInputStream      。
    	//              ,         close       。
    	public SequenceInputStream(Enumeration<? extends InputStream> e){}
    }

     
    3. 예시
     
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.SequenceInputStream;
    import java.util.Enumeration;
    import java.util.Vector;
    
    /**
     *   :           
     */
    public class SequnceInputStreamDemo {
    
    	public static void main(String[] args) throws IOException {
    		// Vector    InputStream  
    		Vector<FileInputStream> v = new Vector<FileInputStream>();
    		v.add(new FileInputStream("e:/1.mp3"));
    		v.add(new FileInputStream("e:/2.mp3"));
    		v.add(new FileInputStream("e:/3.mp3"));
    		
    		//  Enumeration
    		Enumeration<FileInputStream> e = v.elements();
    		
    		SequenceInputStream sis = new SequenceInputStream(e);
    		FileOutputStream fos = new FileOutputStream("e:/4.mp3");
    		
    		byte[] buf = new byte[1024];
    		int num = 0;
    		while((num=sis.read(buf))!=-1)
    		{
    			fos.write(buf,0,num);
    		}
    		fos.close();
    		sis.close();
    		System.out.println("    ");
    	}
    }

     
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.SequenceInputStream;
    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.Iterator;
    
    /**
     *   :           
     * 
     *   : ArrayList  Vector   ,     Enumeration     
     */
    public class SequnceInputStreamDemo {
    
    	public static void main(String[] args) throws IOException {
    		// Vector    InputStream  
    		ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
    		al.add(new FileInputStream("e:/1.mp3"));
    		al.add(new FileInputStream("e:/2.mp3"));
    		al.add(new FileInputStream("e:/3.mp3"));
    		
    		//  Enumeration
    		final Iterator<FileInputStream> it = al.iterator();
    		Enumeration<FileInputStream> e = new Enumeration<FileInputStream>(){
    			public boolean hasMoreElements()
    			{
    				return it.hasNext();
    			}
    			public FileInputStream nextElement()
    			{
    				return it.next();
    			}
    		};
    		SequenceInputStream sis = new SequenceInputStream(e);
    		FileOutputStream fos = new FileOutputStream("e:/4.mp3");
    		
    		byte[] buf = new byte[1024];
    		int num = 0;
    		while((num=sis.read(buf))!=-1)
    		{
    			fos.write(buf,0,num);
    		}
    		fos.close();
    		sis.close();
    		System.out.println("    ");
    	}
    }

     
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /**
     *   :    
     */
    public class SequnceInputStreamDemo {
    
    	public static void main(String[] args) throws IOException {
    		FileInputStream fis = new FileInputStream("e:/4.mp3");
    		FileOutputStream fos;
    		
    		byte[] buf = new byte[1024*1024*5];
    		int num = 0;
    		int x = 5;
    		while((num=fis.read(buf))!=-1)
    		{
    			fos = new FileOutputStream("e:/"+(x++)+".mp3");
    			fos.write(buf,0,num);
    			fos.close();
    		}
    		fis.close();
    		System.out.println("    ");
    	}
    }

    좋은 웹페이지 즐겨찾기