1)
package bookpractice0410;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class FileTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = 1;
try (FileWriter f = new FileWriter("짱구의 일기.txt")) {
while (num == 1) {
System.out.print("문자열입력");
String st = sc.nextLine();
f.write(st + "\n");
System.out.println("1.계속 0 그만");
num = sc.nextInt();
st = sc.nextLine();
}
} catch (IOException e) {
e.printStackTrace();
}
try(FileReader r = new FileReader("짱구의 일기.txt")) {
char[] buff = new char[100];
r.read(buff);
for (int i = 0; i < buff.length; i++) {
System.out.print(buff[i]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2)나혼자 연습
package bookpractice0410;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class FilePracticeMyself {
public static void main(String[] args) {
try (FileWriter f = new FileWriter("ting.txt")){
f.write("흥행흥행1" +"\n");
f.write("흥행흥행2" +"\n");
} catch (IOException e) {
e.printStackTrace();
}
try (FileReader rr = new FileReader("ting.txt")){
ArrayList<Character> ac = new ArrayList<>();
int a;
while((a=rr.read()) !=-1) {
ac.add((char)a);
}
for(int i=0; i<ac.size(); i++) {
System.out.print(ac.get(i));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
3)스캐너로 입력
package bookpractice0410;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
class Diary {
int num;
String date;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
public class fileTest2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Diary d = new Diary();
while (true) {
System.out.print("1.일기쓰기 2.일기보기 0.종료");
int num = sc.nextInt();
sc.nextLine();
if (num == 1) {
System.out.print("일기제목은?");
String name = sc.nextLine();
try (FileWriter f = new FileWriter(name + ".txt")) {
System.out.println("내용입력하세요");
while (true) {
f.write(name + "\n");
name = sc.nextLine();
if (name.equals("짱구")) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
} else if (num == 2) {
System.out.print("조회할 파일명 입력");
String name = sc.nextLine();
try (FileReader r = new FileReader(name + ".txt")){
ArrayList<Character> ac = new ArrayList<>();
int c;
while((c=r.read()) !=-1 ) {
ac.add((char)c);
}
for(Character a : ac) {
System.out.print(a);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
} else if (num == 0) {
System.out.println("종료한다 오버");
break;
}
}
}
}
'수업시간 JAVA > 이론' 카테고리의 다른 글
lombok /@Data/AllArgsConstructor/NoArgsConstructor (0) | 2023.04.12 |
---|---|
FileOutputStream ->바이트 단위 /ObjectOutputStream/FileOutputStream/ObjectOutputStream (0) | 2023.04.11 |
Interface (0) | 2023.04.09 |
상속 (0) | 2023.04.08 |
substring/indexof/ lastindexof /split 만들어보기 (0) | 2023.04.08 |