ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Console & File I/O
    개발노하우/Java 2007. 4. 30. 12:00

    import java.io.*;

    public class object {
     public static void main(String arg[])throws IOException{
      FileOutputStream fos1 = new FileOutputStream(FileDescriptor.out);
      File f = new File("aaa.txt");
      FileOutputStream fos2 = new FileOutputStream(f);
     
      byte by[] = new byte[]{'H','e','l','l','o',' ','J','A','V','A'};
       
      fos1.write(by);
      fos2.write(by);
     
      fos1.close();
      fos2.close();
     }
    }

    ====================================================================

    import java.io.*;

    public class object {
     public static void main(String arg[])throws IOException{
      FileOutputStream fos1 = new FileOutputStream(new File("bbb.txt"),false);
                                                                       // (FileDescriptor.out);
      BufferedOutputStream bos1 = new BufferedOutputStream(fos1);
      DataOutputStream dos1 = new DataOutputStream(bos1);
     
      dos1.writeInt(20); // 출력시 글자가 깨짐
      dos1.writeDouble(12.34); // 출력시 글자가 깨짐
      dos1.flush(); // 버퍼비우기 // 출력시 글자가 깨짐
     
      dos1.writeChar('A');
      dos1.writeByte('\n');
      dos1.writeByte('B');
      dos1.close();
     }
    }

    ====================================================================

    import java.io.*;

    public class object {
     public static void main(String arg[])throws IOException{
      FileInputStream fis1 = new FileInputStream(new File("aaa.txt"));
     
      while(true){
       int x = fis1.read();
       if(x == -1)
        break;
       System.out.print((char)x);
      }
      fis1.close();
     }
    }

    ====================================================================

    import java.io.*;

    public class object {
     public static void main(String arg[])throws IOException{
      File f = new File("bbb.txt");
      FileInputStream fis = new FileInputStream(f);
      BufferedInputStream bis = new BufferedInputStream(fis);
      DataInputStream dis = new DataInputStream(bis);
     
      int a = dis.readInt();
      double b = dis.readDouble();
      char c = dis.readChar();
      byte d = dis.readByte();
      byte e = dis.readByte();
      dis.close();
     
      System.out.println("a = "+a);
      System.out.println("b = "+b);
      System.out.println("c = "+c);
      System.out.println("d = "+d);
      System.out.println("e = "+(char)e);
     }
    }

    ====================================================================

    import java.io.*;

    public class object {
     public static void main(String arg[])throws IOException{
      File f = new File("bbb.txt");
      FileInputStream fis = new FileInputStream(f);
      BufferedInputStream bis = new BufferedInputStream(fis);
      DataInputStream dis = new DataInputStream(bis);
     
      while(true){
       int x = dis.read();
       if(x == -1)
        break;
       System.out.print(x);
      }
      dis.close();
     }
    }

    ====================================================================
    // 출력시 글자가 깨지지 않는다.
    import java.io.*;

    public class object {
     public static void main(String arg[])throws IOException{
      OutputStreamWriter osw = new OutputStreamWriter(System.out);
      BufferedWriter bw = new BufferedWriter(osw);
      PrintWriter pw = new PrintWriter(bw);
     
      pw.println(10);
      pw.println("test 안뇽하세요~");
     
      pw.close();
     
     }
    }

    ====================================================================
    // 파일 생성과 출력
    import java.io.*;

    public class object {
     public static void main(String arg[])throws IOException{
      File f = new File("ccc.txt");
      FileWriter fw = new FileWriter(f);
      BufferedWriter bw = new BufferedWriter(fw);
      PrintWriter pw = new PrintWriter(bw);
     
      pw.println(10);
      pw.println("안뇽하세요~");
      pw.close();
     
     }
    }

Designed by Tistory.