-
210820_FileWriter+scannerJAVA 2021. 8. 20. 10:40
//스캐너를 이용해서 콘솔창에서 입력하면 메모장에 저장됨
FileWriter fw;
static final String FILENAME="메모장 저장 위치\\저장할이름.txt"; //상수는 대문자로
Scanner sc;
//생성자 불러오기
public FileWriterEx03() {
sc=new Scanner(System.in); //생성과 동시에 사용 가능해짐
}
//데이터 입력 메서드
public void dataWrite() { //static은 생성 안 해주고 할 때 사용
try {
fw=new FileWriter(FILENAME, true); //추가모드 생성
System.out.println("이름 입력");
String name=sc.nextLine();
System.out.println("주소 입력");
String addr=sc.nextLine();
System.out.println("핸드폰번호 입력");
String hp=sc.nextLine();
System.out.println("***파일에 저장됨***");
//파일 쓰기
fw.write("이름명: "+name+"\n");
fw.write("주소: "+addr+"\n");
fw.write("연락처: "+hp+"\n--------------\n");
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FileWriterEx03 fw3=new FileWriterEx03();
fw3.dataWrite();
}
}
//결과
이름 입력
요다
주소 입력
서울시
핸드폰번호 입력
010101010
***파일에 저장됨***
'JAVA' 카테고리의 다른 글
210820_Hashset+Iterator (0) 2021.08.20 210820_filereader+filewriter+scanner (0) 2021.08.20 210819_date format (0) 2021.08.19 210819_filereader+bufferreader+split+tokenizer (0) 2021.08.19 210819_split+tokenizer (0) 2021.08.19