[TIL] 240131

Geehyun(장지현)·2024년 1월 31일

TIL

목록 보기
26/70
post-thumbnail

Today

  • Do it! 자바 완전 정복

    • 자바 IO로 파일 읽어들이기 실습
      • 영어로 작성된 txt 읽어들이기
            File file1 = new File("C:\\Users\\wkdwl\\OneDrive\\java4\\JAVA\\Temp\\infile.txt");
        try {
            InputStream fis1 = new FileInputStream(file1);
            int data;
            while ((data = fis1.read()) != -1) {
                System.out.println((char) data);
            }
            fis1.close();
        } catch(FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
    
        }
        System.out.println("\n\n");
        try {
            InputStream fis2 = new FileInputStream(file1);
            byte[] byteArr = new byte[6];
            int count1;
            while ((count1 = fis2.read(byteArr))!= -1) {
                System.out.println("byteArr : " + Arrays.toString(byteArr));
                for(int i = 0; i < count1; i++) {
                    System.out.print((char) byteArr[i]);
                }
                System.out.println();
                System.out.println("count1 :" + count1);
            }
            fis2.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println("\n\n");
        try {
            InputStream fis3 = new FileInputStream(file1);
            byte[] byteArr3 = new byte[9];
            int offset = 3; //처음 3개는 아무것도 없는 문자 나옴
            int len = 6; // 오프셋 이후 지정한 길이만큼 읽어옴
            int count2 = fis3.read(byteArr3, offset, len);
            for(int i = 0; i < offset+count2; i++) {     //"i < offset+count2" 오프셋 길이 + 우리가 읽어들인 개수를 출력해주기 위함
                System.out.println((char) byteArr3[i]);
            }
            fis3.close();           //자원을 항상 반납해야함.
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    
    • 한글로 작성된 파일 읽어들이기
    
    File kofile = new File("C:\\Users\\wkdwl\\OneDrive\\java4\\JAVA\\Temp\\kofile.txt");
    try {
            InputStream fsi1 = new FileInputStream(kofile);
            byte[] byteArr = new byte[8];
            int count1;
            while ((count1 = fsi1.read(byteArr)) != -1) {
                String str = new String(byteArr, 0, count1, Charset.forName("UTF-8"));  
                //UTF-8로 변환했지만 그래도 깨짐 따라서 "reader"로 한글을 읽어들이는게 좋음.
                System.out.println("한글 출력 : " + str);
                System.out.println(" : count1 =" + count1);
            }
            fsi1.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    • 파일 내용 작성 후 읽어보기
            File rwfile = new File("C:\\Users\\wkdwl\\OneDrive\\java4\\JAVA\\Temp\\rwfile.txt");
        try(Writer wr = new FileWriter(rwfile)) {
            wr.write("무궁화 꽃이\n".toCharArray());
            wr.write("Hello\n");
            wr.write("\n");
            wr.write("\n");
            wr.write("Java");
            wr.flush();        //메모리상 작성되었던 내용을 파일로 내림
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    
        try(Reader rd = new FileReader(rwfile)) {
            int data;
            while ((data = rd.read()) != -1) {
                System.out.print((char) data);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
  • 자바 진도 끝!!

Review

  • 자바 IO 관련해서 실습을 진행했는데 일단 선생님 코드 똑같이 작성하느라 이해를 완전히 하지는 못한 것 같다.
  • 내일 과제 (2/1)
    1. 메뉴를 콘솔로 입력받아 해당 메뉴의 html 문서를 콘솔에 출력하고, 해당 문서 내 정해진 요소를 ArrayList 또는 HashMap으로 데이터 저장하도록
      + post 메뉴의 경우 상세글 접속 후 이전/다음/목록으로 나갈 수 있도록 구성
    2. 1번에서 저장한 객체를 기준으로 정보를 출력할 수 있도록 구성
      * 반드시 Class를 사용하도록

TO DO

  • JAVA 수업진도 나간거 복습하기
    • 제네릭 / 람다식 / 컬렉션 / 자바IO
  • 블로그 개인과제를 위해 블로그 내용 최종 수정 필요
profile
블로그 이전 했습니다. 아래 블로그 아이콘(🏠) 눌러서 놀러오세요

0개의 댓글