텍스트 파일의 값을 이진수로 입출력 한다.
[FileInputStream 사용법]
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Main {
public static void main(String [] argv){
FileInputStream reader=null;
try {
reader = new FileInputStream("data.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte [] str = new byte[16];
try {
reader.read(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String m = new String(str);
System.out.println(m);
}
}
-----------------------------
[FileouputStream 사용법]
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String [] argv){
FileOutputStream writer = null;
try {
writer = new FileOutputStream("data.txt", true) ; // 기존 파일 내용 이어서 쓰기
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte [] m = {'R', 'u', 'n', ' ', 'a', 'w', 'a','y'};
try {
writer.write(m);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
'System Programmings > Java' 카테고리의 다른 글
[Swing] 설치 (0) | 2012.07.10 |
---|---|
[Java] 파일 입출력 [ BufferedReader ] 및 [ StringTokenizer ] 활용 (0) | 2011.07.06 |
[Java] 파일 입출력 [ FileReader / FileWriter ] (0) | 2011.07.06 |
[Java] File과 BufferedReader를 이용한 입출력 (0) | 2011.07.05 |
[Java] 부분 일치하는 문자 검색 (0) | 2011.06.08 |