package IO;


import java.io.FileInputStream;

import java.io.FileOutputStream;


public class ByteMain {


public static void main(String[] args) { // TODO Auto-generated method stub

// 파일에 바이트 단위로 기록할 수 있는 스트림을 생성


// try {

// // true를 쓰면 이어붙이기가 됩니다.

// FileOutputStream fos = new FileOutputStream("test.txt", true);

//

// byte [] b ="Hello".getBytes();

//

// fos.write(b);

//

//

// //계속 예외처리하라고 하면 Exception으로 바꿔주기

// fos.close();

// } catch (Exception e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// }


// FileOutputStream fos = new FileOutputStream("test.txt", true);


FileOutputStream의 매개변수 자리에 true 를 사용하면 

test 파일 안에 문자가 리셋되는 것이 아니라 

계속 추가되어 문자가 쓰여진다.


/***************************************************************************************************/

// try {

//

// FileInputStream fis = new FileInputStream("test.txt");

// // 1개 읽기

// int r = fis.read();

// System.out.println(r);

// fis.close();

// } catch (Exception e) {

// System.out.println(e.getMessage());

// }



//텍스트 파일 생성 

/***************************************************************************************************/

try {


FileInputStream fis = new FileInputStream("test.txt");

byte[] b = new byte[fis.available()];

fis.read(b);


// String(byte[] bytes)

// Constructs a new String by decoding the specified array of bytes using the

// platform's default charset.


//new String 생성자를 생성해서 

// byte의 숫자를 문자로 형변환 해준다. 

//String 클래스의 생성자중 byte 배열을 Charset으로 변화해주는 생성자를 쓰면 쉽게 숫자를 문자로 바꾸어 

읽어 올 수 있다.  

System.out.println(new String(b));


// for(byte x : b ) {

// System.out.println(x);

// }

// System.out.println(b);

fis.close();

} catch (Exception e) {

System.out.println(e.getMessage());

}

}


}









+ Recent posts