API - String
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Test01 {
public static void main (String[] args) {
// 문자열 저장
File target = new File("files", "string1.txt");
OutputStream out = null;
String text = "어둠만이 나의 전부였던 동안\r\n"
+ "숨이 벅차도록 달려왔잖아\r\n"
+ "Never say “time's up”\r\n"
+ "경계의 끝자락\r\n"
+ "내 끝은 아니니까\r\n"
+ "울타리 밖에 일렁이는 무언가\r\n"
+ "그 아무도 모르는 별일지 몰라\r\n"
+ "I wanna wanna be there\r\n"
+ "I'm gonna gonna be there\r\n"
+ "벅찬 맘으로 이 궤도를 벗어나\r\n"
+ "Let's go!\r\n"
+ "새로운 길의 탐험가\r\n"
+ "Beyond the road\r\n"
+ "껍질을 깨뜨려버리자\r\n"
+ "두려움은 이제 거둬\r\n"
+ "오로지 나를 믿어\r\n"
+ "지금이 바로 time to fly\r\n"
+ "두 눈 앞의 끝, 사뿐 넘어가\r\n"
+ "한계 밖의 trip, 짜릿하잖아\r\n"
+ "녹이 슨 심장에 쉼 없이 피는 꿈\r\n"
+ "무모하대도 믿어 난";
try {
out = new FileOutputStream(target);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte[] data = text.getBytes();
try {
out.write(data);
} catch (IOException e) {
e.printStackTrace();
}
try {
if(out !=null) {
out.close();
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
저장된 파일
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
public class Test02 {
public static void main(String[] args) {
//문자열 전용 Stream을 이용하면 편하게 입출력도 가능하다
File target = new File("files","string2.txt");
Writer out = null;
PrintWriter printer = null;
try {
out = new FileWriter(target);
printer = new PrintWriter(target);
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
// try {
// out.write("자바\n");
//
// } catch (IOException e) {
// e.printStackTrace();
// }
printer.append("자바\n");
printer.append("자바");
// 기존 파일 뒤에 붙임
printer.println("자바");
printer.println("자바");
printer.println("자바");
printer.println("자바");
printer.println("자바");
try {
if(out != null) {
out.close();
}
if(printer != null) {
printer.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
저장된 파일
'Backend > JAVA' 카테고리의 다른 글
JAVA 코딩 API (Application Programming Interface) - Math, System, Date, Scanner (1) | 2023.03.03 |
---|---|
JAVA 코딩 API (Application Programming Interface) - File (1) | 2023.03.02 |
JAVA 코딩 API (Application Programming Interface) - Thread (0) | 2023.03.02 |
JAVA 객체지향 프로그래밍 OOP, Class 상속 - 부모클래스, 자식클래스 (0) | 2023.02.27 |
JAVA 코딩 API (Application Programming Interface) - Collections Framework (0) | 2023.02.23 |