반응형
import java.io.*;
import java.sql.Struct;

class Main { //파일 읽는 예제
   public static void main(String[] args){
       try {
           FileReader in = new FileReader(args[0]);
           int c;
           String s = new String();
           while ((c = in.read()) != -1){ //파일의 끝까지 읽기
               s = s + (char) c;
               System.out.print(s);
               in.close();
           }
       }
       catch (IOException ie){
           System.out.println("예외1");
       }
       catch (Exception e){
           System.out.println("알 수 없는 오류");
       }
       finally {
           System.out.println("끝");
       }
   }


   public static void main(String[] args){ //파일 쓰기 예제
       try {
           FileWriter out = new FileWriter("C:\\Users\\a02\\Desktop\\project\\javat\\index.txt");
           out.write("0123\n");
           out.write("0123\r\n");
           out.write("ABCD\n");
           out.write("ABCD\r\n");
           out.write("가나다라");

           out.close();
       }
       catch (Exception e){
           System.out.println("파일을 지정하지 않았습니다.");
       }
   }
   public static void main(String[] args){ //바이너리 파일 읽기 아스키 코드
       try {
           FileInputStream in = new FileInputStream(args[0]);
           int c;
           String s = new String();
           while ((c = in.read()) != -1){ //파일의 끝까지 읽기
               s = s + (char) c;
               System.out.print(s);
               in.close();
           }
       }
       catch (FileNotFoundException e){
           System.out.println("파일이 존재하지 않습니다.");
       }
       catch (Exception e) {
           System.out.println("알 수 없는 오류");
       }
   }
   public static void main(String[] args){ //바이너리 파일 쓰기 예제 아스키 코드
       try {
           FileOutputStream out = new FileOutputStream("C:\\\\Users\\\\a02\\\\Desktop\\\\project\\\\javat\\\\index.dat");
           byte x; //바이너리 파일 쓸 때는 당연히 1바이트씩 써야지;
           for (x = 48; x <= 57 ; x++) { //숫자 0~9
               out.write(x);
           }
           for (x = 65;  x <= 90; x++) {
               out.write(x);
           }
           out.close();
       }
       catch (Exception e) {
           System.out.println("알 수 없는 오류");
       }
   }

   public static void main(String[] args){ //바이너리 파일을 텍스트 파일로 읽는 예제 2바이트씩 읽을때 한글 같은거
       try {
           String filename = "C:\\\\Users\\\\a02\\\\Desktop\\\\project\\\\javat\\\\index.dat";

           FileInputStream bin = new FileInputStream(filename); //바이너리 파일 읽기

           InputStreamReader tin = new InputStreamReader(bin); //바이너리 파일 객체를 인자값으로 넣고 변환

           int c;
           while ((c = tin.read()) != -1){
               System.out.print((char)c);
           }
           tin.close();
       }
       catch (Exception e) {
           System.out.println("알 수 없는 오류");
       }
   }

   public static void main(String[] args){ //바이너리 파일에 문자쓰기하는 예제 2바이트씩 쓸 때 한글같은거
       try {
           String filename = "C:\\\\Users\\\\a02\\\\Desktop\\\\project\\\\javat\\\\index.dat";

           FileOutputStream fout = new FileOutputStream(filename); //바이너리 파일 읽기

           OutputStreamWriter out = new OutputStreamWriter(fout); //바이너리 파일 객체를 인자값으로 넣고 변환

           String han = "가나다라";

           out.write(han);
           out.close();

       }
       catch (Exception e) {
           System.out.println("알 수 없는 오류");
       }
   }

    public static void main(String[] args) throws IOException { //입력과 숫자 맞추기 게임
//       int a;
//       a = System.in.read(); //그냥 읽기
//
//       InputStreamReader b = new InputStreamReader(System.in); //Reader 클래스의 오브젝트를 인수로 가짐짐
//       BufferedReader c = new BufferedReader(b); //행단위로 읽기
//        String d = c.readLine();// 문자열로 대입
        try {
            InputStreamReader key = new InputStreamReader(System.in);
            BufferedReader in = new BufferedReader(key);

            int a, b = 7;
            System.out.println("이름을 입력하세요.");
            String name = in.readLine();
            System.out.println("숫자 맞추기 ");
            String c = in.readLine();
            a = Integer.parseInt(c);

            while (a!=b){
                if ((a == b-1) || (a == b+1)){
                    System.out.println("아깝");
                }
                else if(a > b+1){
                    System.out.println("좀 더 작은 수");
                }
                else if(a < b-1){
                    System.out.println("좀 더 큰 수");
                }

                c = in.readLine();
                a = Integer.parseInt(c);
            }

            System.out.println("성공");
        }
        catch (IOException e){
            System.out.println("에러");
        }
    }
}

 

파일 쓰는거 읽는거, 

바이너리 파일 읽는거 쓰는거 구현

반응형

'잡개발' 카테고리의 다른 글

자바 UDP 소켓 양방향 통신  (0) 2022.06.03
파워쉘 기기정보 유출 악성코드  (0) 2022.05.10