[ 개발자 로드맵 ]
* 보조스트림 * p.615
보조스트림을 연결해서 데이터 입력 / 출력
[ 문자 변환 보조스트림 ] _ 책 p.618
CharactorConvertStreamEx 클래스
> main 메소드
> write () 메소드
> read () 메소드
package iotest;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
public class CharactorConvertStreamEx {
public static void main(String[] args) throws IOException {
write("문자 변환 스트림을 사용합니다.");
String data = read();
System.out.println(data);
}
public static void write(String str) throws IOException {
FileOutputStream fos = new FileOutputStream("c:\\temp\\test1.txt");
Writer writer = new OutputStreamWriter(fos);
writer.write(str);
writer.flush();
writer.close();
}
public static String read() throws IOException {
FileInputStream fis = new FileInputStream("c:\\temp\\test1.txt");
Reader reader = new InputStreamReader(fis);
char[] buffer = new char[100];
int readCharNum = reader.read(buffer);
reader.close();
String data = new String(buffer, 0, readCharNum);
// System.out.println(readCharNum); // 17 (글자수_공백포함)
return data;
}
}
문자 변환 스트림을 사용합니다.
// 17
[ File 클래스 ] _ p.640
(File01)
* FileWriter 와 for문으로 문자열 반복출력 (try/catch 사용)
package file;
import java.io.FileWriter;
import java.io.IOException;
public class File01 {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("C:\\temp\\fileWriter.txt");
for (int i = 0; i < 10; i++) {
String data = i + "번째 글을 씁니다.\n";
fw.write(data);
}
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
(File02)
package file;
import java.io.FileWriter;
import java.io.IOException;
// 기존 파일에 이어서 저장하기
public class File02 {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("C:\\temp\\fileWriter.txt", true); // **
for (int i = 0; i < 10; i++) {
String data = i + "번째 이어서 씁니다.\n";
fw.write(data);
}
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
** true 없으면 위 캡쳐의 파일(기존 데이터) 덮어씀
* 실행하는만큼 아래로 계속 이어짐
(FileEx)
* mkdir() : 새로운 폴더 생성
* listFiles() : 디렉터리에 있는 파일목록을 반환
package file;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
public class FileEx {
public static void main(String[] args) throws IOException {
File dir = new File("C:\\temp\\images");
File file1 = new File("C:\\temp\\file1.txt");
File file2 = new File("C:\\temp\\file2.txt");
File file3 = new File("C:\\temp\\file3.txt");
if(!dir.exists()) { // dir파일이 없으면
dir.mkdir(); // 만들어
}
if(file1.exists() == false) { // file1이 없다면
file1.createNewFile(); // 만들어
}
if(file2.exists() == false) { // file2 만들기
file2.createNewFile();
}
if(file3.exists() == false) { // file3 만들기
file3.createNewFile();
}
File temp = new File("C:\\temp");
File[] contents = temp.listFiles(); // 만든 파일들 temp폴더에 넣기
System.out.println("시간\t\t\t형태\t\t크기\t이름");
System.out.println("-------------------------------------------------------");
}
}
시간 형태 크기 이름
-------------------------------------------------------
( contents (디렉터리의 파일목록들) 출력)
System.out.println(Arrays.toString(contents));
System.out.println(contents[9].getName());
System.out.println(contents[9].length());
System.out.println(contents[9].isDirectory());
* getName () : 파일의 이름을 리턴
* lengt () : 파일의 크기를 리턴
* isDirectory () : 폴더인지 여부를 확인
[C:\temp\Excel02.xls, C:\temp\Excel02.xlsx, C:\temp\file1.txt, C:\temp\file2.txt, C:\temp\file3.txt, C:\temp\fileWriter.txt, C:\temp\Hello.class, C:\temp\Hello.java, C:\temp\images, C:\temp\temp.txt, C:\temp\test.xls, C:\temp\test1.db, C:\temp\test1.txt, C:\temp\test2.db, C:\temp\write.txt]
temp.txt
9
false
package file;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
public class FileEx {
public static void main(String[] args) throws IOException {
File dir = new File("C:\\temp\\images");
File file1 = new File("C:\\temp\\file1.txt");
File file2 = new File("C:\\temp\\file2.txt");
File file3 = new File("C:\\temp\\file3.txt");
if(!dir.exists()) {
dir.mkdir();
}
if(file1.exists() == false) {
file1.createNewFile();
}
if(file2.exists() == false) {
file2.createNewFile();
}
if(file3.exists() == false) {
file3.createNewFile();
}
File temp = new File("C:\\temp");
File[] contents = temp.listFiles();
System.out.println("시간\t\t\t형태\t\t크기\t이름");
System.out.println("-------------------------------------------------------");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd a HH:mm"); // 초를 빼면 정렬됩니다.
for (File file : contents) {
System.out.print(sdf.format(new Date(file.lastModified())));
if(file.isDirectory()) {
System.out.printf("\t<DIR>\t\t\t" + file.getName());
} else {
System.out.printf("\t\t\t" + file.length() + "\t" + file.getName());
}
System.out.println("");
}
}
}
시간 형태 크기 이름
-------------------------------------------------------
2023-06-28 오후 14:28 4608 Excel02.xls
2023-06-28 오후 12:37 4608 Excel02.xlsx
2023-06-28 오전 10:26 0 file1.txt
2023-06-28 오전 10:26 0 file2.txt
2023-06-28 오전 10:26 0 file3.txt
2023-06-28 오전 10:16 550 fileWriter.txt
2023-06-01 오후 14:12 413 Hello.class
2023-06-01 오후 14:11 118 Hello.java
2023-06-28 오전 10:26 <DIR> images
2023-06-26 오전 10:28 9 temp.txt
2023-06-28 오후 14:12 4096 test.xls
2023-06-27 오후 12:18 3 test1.db
2023-06-28 오전 10:44 43 test1.txt
2023-06-27 오후 15:17 6 test2.db
2023-06-27 오후 15:45 78 write.txt
'국비과정 > JAVA' 카테고리의 다른 글
20230628 _[19일차]_03. Thread (스레드) (0) | 2023.06.28 |
---|---|
20230628 _[19일차]_02. Excel을 자바로 (0) | 2023.06.28 |
20230626 _[17일차]_02. HTML_게시판 만들기 (0) | 2023.06.26 |
20230623 _[16일차]_02. db연결 및 데이터불러오기 (0) | 2023.06.26 |
20230621 _[14일차]_02. 정처기 문제연습 (0) | 2023.06.25 |