ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA] 파일 읽고쓰기, BufferedWriter 파라미터로 넘기기
    자바 2023. 4. 29. 11:36

    main에서 BufferedWriter를 열고 닫기 전 메서드에서 파일 내용을 가져다 쓴다면 제대로 저장이 될까? 

     

     

    public static void main(String[] args) throws IOException {		
    		
    	Scanner sc = new Scanner(System.in);
    		
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd(HH-mm-ss)");
            String newFileName = formatter.format(calendar.getTime()).toString();
    
            File file = new File("C:\\resultTxt\\"+newFileName+".txt");
            String newFilePath = "C:\\resultTxt\\"+newFileName+".txt";
    
            BufferedWriter bf = new BufferedWriter(new FileWriter(newFilePath, true));
    
            System.out.println("\n\n========================================\n");
            System.out.println("fileOpenTest 콘솔을 시작합니다.\n");
            System.out.println("========================================\n");
            
            bf.write("\n\n========================================\n");
            bf.write("fileOpenTest 콘솔을 시작합니다.\n");
            bf.write("========================================\n");
            
            System.out.println("내용을 입력하세요.");
            String input = sc.nextLine();
            bf.write(input);
            bf.write("\n\n----------------------------------------\n");
            
            
            interceptContent(newFilePath);
            
            System.out.println(" 1번째 메서드 끝 ");
            System.out.println(" ===================== ");
            bf.write(" 1번째 메서드 끝 \n");
            bf.write(" ===================== \n");
            
            interceptContent2(newFilePath);
            
            System.out.println(" 2번째 메서드 끝 ");
            System.out.println(" ===================== ");
            bf.write(" 2번째 메서드 끝 \n");
            bf.write(" ===================== \n");
    
            bf.write("\n\n\n\n\n----------------------------------------\n");
            bf.write(newFileName+"\n");
            bf.write("fileOpenTest 콘솔 종료합니다.\n\n");
            
            bf.flush();
            bf.close();
            
    }
    public static void interceptContent(String newFilePath) throws IOException {	
    	Scanner sc = new Scanner(System.in);
    	BufferedWriter bf = new BufferedWriter(new FileWriter(newFilePath, true));
            
    	bf.write("\n\n========================================\n");
            bf.write("interceptContent-1 메서드 시작합니다.\n");
            bf.write("========================================\n");
    		
            System.out.println("내용을 입력하세요.(1/2)");
            String input = sc.nextLine();
            bf.write("내용을 입력하세요.(1/2)\n");
            bf.write(input+"\n");
            
            System.out.println("내용을 입력하세요.(2/2)");
            input = sc.nextLine();
            bf.write("내용을 입력하세요.(2/2)\n");
            bf.write(input+"\n");
    		
            bf.write("\n\n\n----------------------------------------\n");
            bf.write("interceptContent-1 메서드 종료합니다.\n\n");	
            
            bf.flush();
            bf.close();		
    }
    	
    public static void interceptContent2(String newFilePath) throws IOException {
    		
    	Scanner sc = new Scanner(System.in);
    	BufferedWriter bf = new BufferedWriter(new FileWriter(newFilePath, true));
            
    	bf.write("\n\n========================================\n");
            bf.write("interceptContent-2 메서드 시작합니다.\n");
            bf.write("========================================\n");
    
            System.out.println("내용을 입력하세요.(1/2)");
            String input = sc.nextLine();
            bf.write("내용을 입력하세요.(1/2)"+"\n");
            bf.write(input+"\n");
            
            System.out.println("내용을 입력하세요.(2/2)");
            input = sc.nextLine();
            bf.write("내용을 입력하세요.(2/2)"+"\n");
            bf.write(input+"\n");
    		
            bf.write("\n\n\n----------------------------------------\n");
            bf.write("interceptContent-2 메서드 종료합니다.\n\n");	
            
            bf.flush();
            bf.close();
    }

    - 저장은 되는데 순서대로 저장되지는 않는다

    - 콘솔을 시작합니다 -> 메서드1 시작종료 -> 메서드2 시작종료 이렇게 되어야 한다

     

     


     

     

    public static void main(String[] args) throws IOException {		
    		
    	Scanner sc = new Scanner(System.in);
    		
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyMMdd(HH-mm-ss)");
            String newFileName = formatter.format(calendar.getTime()).toString();
    
            File file = new File("C:\\resultTxt\\"+newFileName+".txt");
            String newFilePath = "C:\\resultTxt\\"+newFileName+".txt";
    
            BufferedWriter bf = new BufferedWriter(new FileWriter(newFilePath, true));
    
            System.out.println("\n\n========================================\n");
            System.out.println("fileOpenTest 콘솔을 시작합니다.\n");
            System.out.println("========================================\n");
            
            bf.write("\n\n========================================\n");
            bf.write("fileOpenTest 콘솔을 시작합니다.\n");
            bf.write("========================================\n");
            
            System.out.println("내용을 입력하세요.");
            String input = sc.nextLine();
            bf.write(input);
            bf.write("\n\n----------------------------------------\n");
            
            interceptContent3(newFilePath, bf);
            
            System.out.println(" 3번째 메서드 끝 ");
            System.out.println(" ===================== ");
            bf.write(" 3번째 메서드 끝 \n");
            bf.write(" ===================== \n");
    
    
            bf.write("\n\n\n\n\n----------------------------------------\n");
            bf.write(newFileName+"\n");
            bf.write("fileOpenTest 콘솔 종료합니다.\n\n");
            
            bf.flush();
            bf.close();
    		
    }
    public static void interceptContent3(String newFilePath, BufferedWriter bf) throws IOException {
    		
    	Scanner sc = new Scanner(System.in);
            
    	bf.write("\n\n========================================\n");
            bf.write("interceptContent-3 메서드 시작합니다.\n");
            bf.write("========================================\n");
    		
            System.out.println("내용을 입력하세요.(1/2)");
            String input = sc.nextLine();
            bf.write("내용을 입력하세요.(1/2)\n");
            bf.write(input+"\n");
            
            System.out.println("내용을 입력하세요.(2/2)");
            input = sc.nextLine();
            bf.write("내용을 입력하세요.(2/2)\n");
            bf.write(input+"\n");
    		
            bf.write("\n\n\n----------------------------------------\n");
            bf.write("interceptContent-3 메서드 종료합니다.\n\n");		
    		
    }

    - BufferedWriter를 메서드 파라미터로 넘기면 순서대로 저장된다

     

    java.io.IOException: Stream closed

    - 파일에 저장은 제대로 되는데 오류가 발생했었다

    > interceptContent3 메서드에서 bufferedwriter 열지 않고 사용했는데 마지막에 close 하고 main에서 다시 한번 close 하니까 발생한 오류였다

    728x90

    '자바' 카테고리의 다른 글

    [JAVA] 예외처리  (0) 2023.05.07
    [JAVA] path 클래스  (0) 2023.05.06
    [JAVA] try-catch-finally 구문  (0) 2023.04.28
    [JAVA] Static 키워드  (0) 2023.04.27
    [JAVA] openAI API 이용한 chatGPT, 자바로 채팅 기능 구현하기 - V2  (0) 2023.04.24
Designed by Tistory.