Java/I_O

InputStream과 OutputStream

krvision 2011. 11. 30. 17:18
import java.io.*;

class InputStreamTest3 
{
	public static void main(String[] args) throws IOException
	{
		int r=0, count=0;
		while((r=System.in.read()) != -1)  {
			if( r!= 10 && r!= 13) {   //엔터키는 제외
			// System.out.println("r: " +((char)r));
			System.out.write(r);
			System.out.flush();
			count++;
			}
		}//while----------------
		System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~");
		System.out.println("총 "+count+ "바이트 읽음");
		System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~");
		System.in.close();
		System.out.close();
	}//main()-----------
}////////////////////////////////////////////////////////////////////////////////////////////
import java.io.*;

class InputStreamTest4 
{
	public static void main(String[] args) throws IOException
	{
		int r=0, count=0, total=0;
		byte[] ba=new byte[4]; //달걀판
		while((r=System.in.read(ba)) != -1)  {
			//r은 달걀갯수
			System.out.write(ba, 0 , r);
			count++; //반복문 횟수
			total += r;
		}//while-----
		System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~");
		System.out.println("총 "+total+ "바이트 읽음");
		System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~");
		System.in.close();
		System.out.close();
	}
}