Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 |
Tags
- ObjectOutputStream
- java
- 예제
- 파이썬
- CSS
- Codility
- 사전학습
- 상속
- BorderLayout
- Database
- Position
- GridLayout
- inline
- html 기초
- 반응형웹
- FileWriter
- 퍼셉트론
- 반응형 웹 프로젝트
- rnn
- oracle
- 메서드
- FFT
- html 프로젝트
- g검정
- 푸리에 변환
- HTML
- iframe 태그
- FlowLayout
- 미디어쿼리
- css 기초
Archives
- Today
- Total
도라에몽주머니
[Java/Eclipse] Java 38일차 본문

e.printStackTrace()
: 예외 발생 당시의 호출스택에 있던 메소드의 정보와 예외 결과를 화면에 출력
File
: 파일과 폴더를 관리하는 클래스
- getAbsolutePath() : 절대경로를 추출
- isFile() : 저장된 경로가 파일인지 검사
- isDirectory() : 저장된 경로가 폴더인지 검사
- isHidden() : 저장된 경로가 숨김 형태인지 검사
- exists() : 저장된 경로에 파일이 실제로 존재하는지 검사
- mkdirs() : 폴더 만들기
- getName() : 마지막 "/" 이후 단어 리턴
- getParent() : 마지막 "/" 이전 단어 리턴
import java.io.File;
public class Ex {
public static void main(String[] args) {.
File f1 = new File("src\\Main01.java");
System.out.println("f1 = " + f1);
System.out.println("-------------");
System.out.println(f1.getAbsolutePath());
System.out.println("-------------");
boolean is_file = f1.isFile();
System.out.println("is_file : " + is_file);
}
}
파일 출력(FileWriter)
import java.io.FileWriter;
import java.io.IOException;
public class Ex {
public static void main(String[] args) {
String str = "가나다라마바사abcdefg";
String path = "test2.txt";
try {
// new FileWriter(path) : 파일 생성 및 파일 오픈
FileWriter fileWriter = new FileWriter(path);
fileWriter.write(str); // 파일에 문자열 출력
fileWriter.close(); // 파일 닫기
System.out.println("파일 저장 성공");
} catch (IOException e) {
System.out.println("파일 저장 실패");
// e.printStackTrace();
}
}
}
파일 입력(FileReader)
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Ex {
public static void main(String[] args) {
String str = "가나다라마바사abcdefg";
String path = "test2.txt";
try {
// new FileReader(path) : 파일이 있는지 확인하고 파일을 오픈함
FileReader fileReader = new FileReader(path);
int data = 0;
String result = "";
// fileReader.read() : 파일에서 문자 1개를 유니코드로 읽어옴
// -1 : EOF (End of File)
while((data = fileReader.read()) != -1) {
result += data;
}
} catch (FileNotFoundException e) {
System.out.println("파일 경로에 문제가 있습니다 >> " + path);
// e.printStackTrace();
} catch (IOException e) {
System.out.println("파일 읽기 실패입니다.");
// e.printStackTrace();
} catch(Exception e) {
System.out.println("알 수 없는 에러입니다.");
}
}
}
인코딩(Encoding)
: 문자를 컴퓨터가 이해하기 쉽게 변환하는 과정
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
public class Ex {
public static void main(String[] args) {
String str = "가나다라마바사abcdefg";
String path = "text3.txt";
// "utf-8" encoding으로 저장
// => 스트림 클래스 사용
// => byte 배열로 처리
// 1) 문자열을 byte 배열로 변환
byte[] buff = null;
try {
buff = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
System.out.println("인코딩 형식이 잘못되었습니다.");
// e.printStackTrace();
}
// 2) byte 배열을 파일로 출력
OutputStream out = null;
try {
// 파일 생성 및 파일 오픈
out = new FileOutputStream(path);
// 파일 출력
out.write(buff);
System.out.println("[INFO] 파일 저장 성공 >> " + path);
} catch (FileNotFoundException e) {
System.out.println("[ERROR] 파일 생성을 못했습니다. >> " + path);
// e.printStackTrace();
} catch (IOException e) {
System.out.println("[ERROR] 파일 저장 실패입니다.");
// e.printStackTrace();
} catch(Exception e) {
System.out.println("[ERROR] 알 수 없는 에러입니다.");
} finally {
try {
if(out != null) out.close();
} catch (IOException e) {
System.out.println("[ERROR] 파일 닫기 실패입니다.");
// e.printStackTrace();
}
}
}
}'Study > Java' 카테고리의 다른 글
| [Java/Eclipse] Java 41일차 (0) | 2022.11.11 |
|---|---|
| [Java/Eclipse] Java 39, 40일차 (0) | 2022.11.10 |
| [Java/Eclipse] Java 36, 37일차 (0) | 2022.11.07 |
| [Java/Eclipse] Java 34, 35일차 (0) | 2022.11.03 |
| [Java/Eclipse] Java 33일차 (0) | 2022.11.01 |