티스토리 뷰

dev/java

[16] 1바이트 입력

altvirus 2008. 9. 27. 20:21
## 1바이트 입력 1 ##
import java.io.*;

public class _2_3 {
	public static void main(String[] args)  throws IOException{
		
		// 콘솔모드
		FileInputStream fis1 = new FileInputStream(FileDescriptor.in);
				
		System.out.print("문자는? ");
		char ch = (char)fis1.read(); // 입력받은 값(문자 1개)의 아스키 값 int -> char
		
		System.in.read(); // 엔터키 처리...
		
		System.out.print("문자열은?");
		byte[] by = new byte[1024];
		fis1.read(by); // read()의 매개변수로 배열을 적으면, 문자 하나하나가 각각 배열로 입력.
		
		System.out.println("ch = " + ch);
		System.out.println("by = " + new String(by).trim());
		fis1.close();
		

		// 파일모드
		FileInputStream fis2 = new FileInputStream(new File("aaa.txt"));
		while(true){
			int x = fis2.read(); // 1바이트씩 계속 읽는다.
			if(x==-1) break; // 파일의 끝을 만나면 -1이 된다.
			System.out.print((char)x);
		}
		
		fis2.close();
		
	}
}


## 1바이트 입력 2 ##
import java.io.*;

public class _2_4 {
	public static void main(String[] args) throws IOException{
		// 콘솔로부터 입력받는 것은 scanner 클래스로....
		
		FileInputStream fis = new FileInputStream(new File("bbb.txt"));
		BufferedInputStream bis = new BufferedInputStream(fis, 1024);
		DataInputStream dis = new DataInputStream(bis);
		
		
		System.out.println(dis.readInt());
		System.out.println(dis.readDouble());
		System.out.println(dis.readChar());
		System.out.println(dis.readByte());
		
		dis.close();
		
	}
}

'dev > java' 카테고리의 다른 글

[15] Thread 관련 이것저것  (0) 2008.09.27
[15] Thread 만들기 기본..  (0) 2008.09.27
[16] 1바이트 출력  (0) 2008.09.27
Inner Class  (0) 2008.09.27
4대 중첩 클래스  (0) 2008.09.27
공지사항