[Java] File sample

프로그래밍/JAVA 2019. 5. 26. 14:57 posted by 야매코더
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
 * file Directory
 */
public static void FileDirList() {
    String tmpPath = System.getProperty("user.dir");
 
    File dir = new File(".\\DATA");
    File dirOut = new File(".\\out");
    File[] fList = dir.listFiles();
 
    for (File file : fList) {
        if (file.isDirectory()) {
 
        } else {
            long len = file.length();
            System.out.println(file.getAbsolutePath() + " " + file.getName());
 
            if (!dirOut.exists()) {
                dirOut.mkdirs();
            }
            String srcFile = tmpPath + "\\out\\";
            CopyFile(file.getAbsolutePath(), srcFile + file.getName());
        }
    }
 
}
 
/**
 * Bin 파일 복사
 */
public static void CopyFile(String inputFile, String outputFile) {
    final int BUFFER_SIZE =  512;//4096;
    int readLen;
     
    try {
        InputStream is  =  new FileInputStream(inputFile);
        OutputStream os  =  new FileOutputStream(outputFile);
         
        byte[] buffer = new byte[BUFFER_SIZE];
        while((readLen  = is.read(buffer)) != -1){
            os.write(buffer, 0 , readLen);             
        }
        is.close();
        os.close();
         
    } catch (Exception e) {
        // TODO: handle exception
    }
             
}
 
/**
 *  파일 모두 읽기     * 
 */
public static void readAllFile() {
     
    Path path = Paths.get("C:\\User\\..\\file.txt");
    Charset cs = StandardCharsets.UTF_8;
    List<String> list = new ArrayList<String>();
     
    try {
        list = Files.readAllLines(path, cs);
    } catch (Exception e) {
        // TODO: handle exception
    }
      
    for (String readLine : list) {
        System.out.println(readLine);
    }
}
 
 
 
/**
 * Text 파일 라인별로 읽기
 */
public static void readLineFile() {
    try {
        File file = new File("C:\\User\\..\\file.txt");
 
        FileReader filereader = new FileReader(file);
        BufferedReader bufReader = new BufferedReader(filereader);
         
        String line = "";
         
        while ((line = bufReader.readLine()) != null) {
            System.out.println(line);
        }
         
        bufReader.close();                 
         
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
/**
 * 파일 쓰기
 */
public static void SaveFile() {
    try {
        File file = new File("C:\\\\User\\\\..\\\\file.txt");
        BufferedWriter bw = new BufferedWriter (new FileWriter (file));
         
        if(file.isFile() && file.canWrite()) {
            bw.write("문자열 추가");
            bw.newLine(); // 개행문자
            bw.write("문자열 추가1");
            bw.close();
        }          
    } catch (Exception e) {
        // TODO: handle exception
    }
}