<입출력(IO)>
Input : 입력
-> 외부에서 내부로 값이 들어오는 것
Output : 출력
-> 내부에서 외부로 값이 나가는 것
바이트 기반 스트림
파일출력
FileOutputStream 변수명 = null;
try {
변수명 = new FileOutputStream("파일경로");
String 변수명2 = "입력내용";
for(int i = 0; i < 변수명2.length(); i++) {
변수명.write(변수명2.charAt(i));
}
} catch(IOException e) {
e.printStackTrace();
} finally {
try{
변수명.close();
} catch (IOException e) {
e.printStrackTrace();
}
}
파일 입력
FileInputStream 변수명 = null;
try {
변수명 = new FileInputStream("test1.txt");
while(true) {
int 변수명 2 = 변수명.read();
if(변수명2 == -1)
break;
System.out.print((char)변수명2);
} catch(IOException e) {
e.printStackTrace();
} finally {
try{
변수명.close();
} catch (IOException e) {
e.printStrackTrace();
}
}
문자기반 스트림
파일출력
FileWriter 변수명 = null;
try {
변수명 = new FileWriter("test1.txt");
String 변수명2 = "입력내용";
변수명.write(변수명2);
} catch(IOException e) {
e.printStackTrace();
} finally {
try{
변수명.close();
} catch (IOException e) {
e.printStrackTrace();
}
}
파일입력
FileReader 변수명 = null;
try {
변수명 = new FileReader("test1.txt");
while(true) {
int 변수명2 = 변수명.read();
if(변수명2 == -1)
break;
System.out.print((char)변수명2);
} catch(IOException e) {
e.printStackTrace();
} finally {
try{
변수명.close();
} catch (IOException e) {
e.printStrackTrace();
}
}