본문 바로가기

연습문제/JAVA

JAVA - FileExample3번

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

//p 485
public class FileOutputStreamTest {
//FileOutputStream 테스트

public static void main(String[] args) {
FileOutputStream fo = null;
String msg = "안녕안녕안녕안녕";
byte [] byteArray = msg.getBytes(StandardCharsets.UTF_8);
//문자열 msg byte로 가져온다 .
// StandarCharsets.UTF_8을 넣어서 안전하게 한글도 사용가능하게


try{
//상대경로를 지정할때 어떻게 테스트 해가면서 쓸지? : ./ 찍어서 실제 동작이 어떻게 되는지 확인
// append 의 뜻 : 기존 컨텐츠에 ++ 아래 예제 있음!!
fo = new FileOutputStream(".\\FileExample6\\demo\\안녕로봇.txt",true);
//상대경로 : 호출하는 코드의 위치를 기준으로 한다
fo.write(byteArray);

}
catch (IOException io){
io.printStackTrace();
}
finally {
try{
fo.close();
}
catch (IOException io){
io.printStackTrace();
}
}

//append 설명
StringBuilder sb = new StringBuilder();
sb.append("hihi");
sb.append("방가방가");
System.out.println(sb);
//뒤에 append가 추가됨

String str = "hihi";
str = str + "방가방가";
System.out.println(str);
}

// sb출력이랑 str출력 똑같이 hihi방가방가로 출력된다
}

 

이제부터 강조할 부분만 배경색을 넣어야겠다

전부 배경색을 넣으니 보기가 너무 힘들다

 

".\\FileExample6\\demo\\안녕로봇.txt" 로 상대경로를 넣으면 저장되는 위치를 보자

다음은 예제를 세 번 실행했을때 결과물이다.  안녕안녕안녕안녕안녕안녕안녕안녕안녕안녕

'연습문제 > JAVA' 카테고리의 다른 글

Java- FileExample5번 (p490 ~ 491)  (0) 2021.08.13
Java- FileExample4번 (p487)  (0) 2021.08.13
JAVA - FileExample2번  (0) 2021.08.13
JAVA - FileExample1번  (0) 2021.08.13
Java InterfacePractice2 (인터페이스 예제2 - 0811)  (0) 2021.08.11