상세 컨텐츠

본문 제목

[입출력] 전화번호부 로딩, 저장, 어레이리스트

자바

by esoesmio 2023. 4. 11. 21:51

본문

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class z23 {
    int i = 0;
    HashMap<String,String> person = new HashMap<>();

    public static void main(String[] args) {
        z23 z = new z23();
        Scanner sc = new Scanner(System.in);

///첨부터 불러오기
        try {
            BufferedReader br = new BufferedReader(new FileReader("/Users/eunsukkim/Desktop/person.txt"));
            BufferedReader br2 = new BufferedReader(new FileReader("/Users/eunsukkim/Desktop/number.txt"));

            while(true){
                String line = br.readLine();
                String line2 = br2.readLine();

                if(line==null)break;
                z.person.put(line, line2);

            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        String filepath = "/Users/eunsukkim/Desktop/person.txt";
String filepath2 = "/Users/eunsukkim/Desktop/number.txt";

        while(true) {

            System.out.println("번호를 선택하시오");
            System.out.println("1. 저장, 2.출력하기 3.어레이리스트보기");
            int a = sc.nextInt();
            if(a==1) {
                String returnstring = "";
                String returnstring2 = "";
                for (int i = 0; i < 3; i++) {
                    System.out.println("사람의 이름과 번호");
                    String g = sc.next();
                    String v = sc.next();
                    z.person.put(g,v);
                    returnstring += g + "\n";
                    returnstring2 += v + "\n";

                }

                write(filepath,filepath2, returnstring, returnstring2);
            }

            else if(a==2){
            System.out.println(read(filepath,filepath2,z));}
            else if (a==3){

                System.out.println(z.person);
            }
        }

    }

    public static void write(String filepath, String filepath2,String text,String text2) {

        try {
            InputStream is = new FileInputStream(filepath);
            Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
            char[] ass = new char[255];
            if(reader.read(ass)!=-1){
                try {
                    OutputStream os = new FileOutputStream(filepath, true);
                    Writer writer = new OutputStreamWriter(os, StandardCharsets.UTF_8);
                    writer.write(text);
                    writer.flush();
                    writer.close();
                    System.out.println("저장이 되었습니다.");

                    OutputStream os2 = new FileOutputStream(filepath2, true);
                    Writer writer2 = new OutputStreamWriter(os2, StandardCharsets.UTF_8);
                    writer2.write(text2);
                    writer2.flush();
                    writer2.close();
                    System.out.println("저장이 되었습니다.");


                } catch (IOException e) {
                    System.out.println("저장이 안됐어요" + e.getMessage());
                }

            }else{
                try {
                    OutputStream os = new FileOutputStream(filepath);
                    Writer writer = new OutputStreamWriter(os, StandardCharsets.UTF_8);
                    writer.write(text);
                    writer.flush();
                    writer.close();
                    System.out.println("저장이 되었습니다.");


                    OutputStream os2 = new FileOutputStream(filepath2);
                    Writer writer2 = new OutputStreamWriter(os2, StandardCharsets.UTF_8);
                    writer2.write(text2);
                    writer2.flush();
                    writer2.close();
                    System.out.println("저장이 되었습니다.");


                } catch (IOException e) {
                    System.out.println("저장이 안됐어요" + e.getMessage());
                }

            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }








    }

    public static String read(String filepath,String filepath2, z23 z) {
        String returnstring = "";
        try {


            BufferedReader br = new BufferedReader(new FileReader(filepath));
            BufferedReader br2 = new BufferedReader(new FileReader(filepath2));

            while(true){
                String line = br.readLine();
                String line2 = br2.readLine();
                if(line==null)break;
                returnstring+= line+ "\n"+line2 + "\n";
            }


        } catch (IOException e) {
            throw new RuntimeException(e);
        }
return returnstring;

    }



}



//    public static String read(String path) {
//        String z = "";
//        try {
//
//            InputStream os = new FileInputStream(path);
//            Reader reader = new InputStreamReader(os, StandardCharsets.UTF_8);
//
//
//            char[] arr = new char[1024];
//
//            int c;
//            while ((c=reader.read(arr))!= -1) {
//                z += String.valueOf(arr,0,c);
//
//            }
//
//            reader.close();
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//        return z;

//이것도 알아둬야됨

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

[오라클] 오라클 명령어 간단 정리  (0) 2023.04.12
[sql] 이해 안되는 any  (0) 2023.04.12
[오라클 쿼리 연습]연습연습  (0) 2023.04.11
[인터페이스함수]다시한번 해보자  (0) 2023.04.11
[stream] 플랫맵 예시  (0) 2023.04.11

관련글 더보기

댓글 영역