ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 자바 인프런 - 입출력 API
    자바/인프런 인강 2018. 2. 15. 18:53

    -FileInputStream 사용법


    import java.io.InputStream;

    import java.io.FileInputStream;


    public class InputOutput {

    public static void main(String[] args) {

      inputStream inputs = null;

    try {

    Iinputs = new FileInputStream("C:\\Users\\hyk\\Desktop\\testinout.txt");

    while(true) {

    int i = inputs.read();

    System.out.println("입력 받은 데이터:"+i);

    if(i == -1) break;

    }

    } catch (Exception e) {

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

    } finally {

    try {

    if (inputs != null) inputs.close();

    } catch (IOException e2) {

    System.out.prinln(ex.getMassage());

    }

        }


        }

    }


    -FileOutputStream 사용법


    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.OutputStream;


    public class InputOutput {

    public static void main(String[] args) {

    OutputStream outputs = null;

    try {

    outputs = new FileOutputStream("C:\\Users\\hyk\\Desktop\\testout.txt");

    String str1 = "안녕하세요";

    byte[] bs = str1.getBytes();

    outputs.write(bs);

    } catch (Exception e) {

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

    } finally {

    try {

    if(outputs != null) outputs.close();

    } catch (IOException e2) {

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

    }

    }

    }

    }


    -DataInput/OutputStream 사용법, 파일복사하기

    import java.io.DataInputStream;

    import java.io.DataOutputStream;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.InputStream;

    import java.io.OutputStream;


    public class InputOutput {

    public static void main(String[] args) {

    InputStream inputs = null;

    DataInputStream Dis = null;

    OutputStream outputs = null;

    DataOutputStream Dos = null;


    try {

    inputs = new FileInputStream("경로");

    Dis = new DataInputStream(inputs);

    String str = Dis.readUTF();

    outputs = new FileOutputStream("경로");

    Dos = new DataOutputStream(outputs);

    Dos.writeUTF(str);;

    } catch (Exception e) {

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

    } finally {

    if(Dos != null) {

    try {

    Dos.close();

    } catch (Exception e2) {

    }

    if(outputs != null) {

    try {

    outputs.close();

    } catch (Exception e2) {

    }

    }

    }