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;
//이것도 알아둬야됨
댓글 영역