JAVA chapter18. IO 기반 입출력 및 네트워킹. 


18.4 파일 입출력

18.4.1 File 클래스


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
31
32
33
34
35
package sec04.exam01_file;
 
import java.io.File;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class FileExample {
    public static void main(String[] args) throws Exception {
        File dir = new File("C:/Temp/Dir");
        File file1 = new File("C:/Temp/file1.txt");
        File file2 = new File("C:/Temp/file2.txt");
        File file3 = new File(new URI("file:///C:/Temp/file3.txt"));
        
        if(dir.exists() == false) {  dir.mkdirs();  }
        if(file1.exists() == false) {  file1.createNewFile();  }
        if(file2.exists() == false) {  file2.createNewFile();  }
        if(file3.exists() == false) {  file3.createNewFile();  }
 
        File temp = new File("C:/Temp");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd   a   HH:mm");
        File[] contents = temp.listFiles();
        System.out.println("날짜              시간         형태       크기    이름");
        System.out.println("-------------------------------------------------------------------");
        for(File file : contents) {
            System.out.print(sdf.format(new Date(file.lastModified())));
            if(file.isDirectory()) {
                System.out.print("\t<DIR>\t\t\t" + file.getName());
            } else {
                System.out.print("\t\t\t" + file.length() + "\t" + file.getName());
            }
             System.out.println();
        }
    }
}
cs



18.4.2 FileInputStream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package sec04.exam02_fileinputstream;
 
import java.io.FileInputStream;
 
public class FileInputStreamExample {
    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("C:/JavaProgramming/source/chap18/src/sec04/exam02_fileinputstream/FileInputStreamExample.java");
            int data;
            while ( (data = fis.read() ) != -1 ) {
                System.out.write(data);
            }
            // System.out.flush()
            fis.close();    
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
cs


18.4.3 FileOutputStream



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
package sec04.exam03_fileoutputstream;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
 
public class FileOutputStreamExample {
    public static void main(String[] args) throws Exception {
        String originalFileName = "C:/JavaProgramming/source/chap18/src/sec04/exam03_fileoutputstream/house.jpg";
        String targetFileName = "C:/Temp/house.jpg";
        
        FileInputStream fis = new FileInputStream(originalFileName);
        FileOutputStream fos = new FileOutputStream(targetFileName);
        
        int readByteNo;
        byte[] readBytes = new byte[100];
        while( (readByteNo = fis.read(readBytes)) != -1 ) {
            fos.write(readBytes, 0, readByteNo);
        }
        
        fos.flush();
        fos.close();
        fis.close();
        
        System.out.println("복사가 잘 되었습니다.");
    }
}
cs



18.4.4 FileReader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package sec04.exam04_file_reader;
 
import java.io.FileReader;
 
public class FileReaderExample {
    public static void main(String[] args) throws Exception {
        FileReader fr = new FileReader("C:/JavaProgramming/source/chap18/src/sec04/exam04_file_reader/FileReaderExample.java");
        
        int readCharNo;
        char[] cbuf = new char[100];
        while ((readCharNo=fr.read(cbuf)) != -1) {
            String data = new String(cbuf, 0, readCharNo);
            System.out.print(data);
        }
        fr.close();
    }
}
cs



18.4.5 FileWriter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package sec04.exam04_file_writer;
 
import java.io.File;
import java.io.FileWriter;
 
public class FileWriterExample {
    public static void main(String[] args) throws Exception {
        File file = new File("C:/Temp/file.txt");    
        FileWriter fw = new FileWriter(file, true);        
        fw.write("FileWriter는 한글로된 " + "\r\n");
        fw.write("문자열을 바로 출력할 수 있다." + "\r\n");
        fw.flush();
        fw.close();
        System.out.println("파일에 저장되었습니다.");
    }
}
cs



Posted by 너래쟁이
: