I/O 란
입력(input)과 출력(Output)을 뜻한다. 컴퓨터가 입력하는것은 Input 이며, 컴퓨터가 출력하는것을 Output이라고 한다. 대표적으로 system.out.println();
구문이 output이다.
InputStream 사용법
read()메서드 사용법
package sutdy.java.ex20_InputOutput;
import java.io.FileInputStream;
import java.io.InputStream;
public class InputClass {
public static void main(String[] args) {
try {
InputStream is = new FileInputStream("D:\\jain.txt");
while (true) {
int i = is.read();
System.out.println("데이터 : " + i );
if (i == -1) {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
OutputStream 사용법
package sutdy.java.ex20_InputOutput;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class OutputStreamClass {
public static void main(String[] args) {
try {
OutputStream os = new FileOutputStream("D:\\jaOut.txt");
String str = "너무 어렵다";
byte[] bs = str.getBytes();
os.write(bs);
} catch (Exception e) {
e.getStackTrace();
}
}
}
package sutdy.java.ex20_InputOutput;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class OutputStreamClass {
public static void main(String[] args) {
OutputStream os = null;
try {
os = new FileOutputStream("D:\\jaOut.txt");
String str = "너무 어렵다";
byte[] bs = str.getBytes();
os.write(bs);
} catch (Exception e) {
e.getStackTrace();
} finally {
try {
if (os != null) {
os.close(); //연결을 끊는 부분
}
}catch(Exception e) {
}
}
}
}