Coverage Report - jp.co.y_net.amm.tools.ResourceCopy
 
Classes in this File Line Coverage Branch Coverage Complexity
ResourceCopy
0%
0/75
0%
0/24
5
 
 1  
 package jp.co.y_net.amm.tools;
 2  
 
 3  
 import java.io.BufferedReader;
 4  
 import java.io.BufferedWriter;
 5  
 import java.io.File;
 6  
 import java.io.FileInputStream;
 7  
 import java.io.FileOutputStream;
 8  
 import java.io.IOException;
 9  
 import java.io.InputStreamReader;
 10  
 import java.io.OutputStream;
 11  
 import java.io.OutputStreamWriter;
 12  
 import java.io.Writer;
 13  
 import java.nio.channels.FileChannel;
 14  
 import java.nio.charset.Charset;
 15  
 import java.util.ArrayList;
 16  
 import java.util.List;
 17  
 
 18  0
 public class ResourceCopy {
 19  
     public static void main(String[] args) {
 20  
         
 21  0
     }
 22  
     
 23  
     /*
 24  
      * 〓〓 以下、共通部品 〓〓
 25  
      */
 26  
     /**
 27  
      * チャネルを利用したファイルのコピー処理
 28  
      * 
 29  
      * @param formFile コピー元
 30  
      * @param toFileName コピー先
 31  
      */
 32  
     public static void copy(File formFile, String toFileName) {
 33  0
         FileChannel ifc = null;
 34  0
         FileChannel ofc = null;
 35  0
         FileInputStream fis = null;
 36  0
         FileOutputStream fos = null;
 37  
         try {
 38  
             /* 入力元ファイルストリームからチャネル取得 */
 39  0
             fis = new FileInputStream(formFile);
 40  0
             ifc = fis.getChannel();
 41  
 
 42  
             /* 出力先ファイルチャネル取得 */
 43  0
             File outFile = new File(toFileName);
 44  0
             fos = new FileOutputStream(outFile);
 45  0
             ofc = fos.getChannel();
 46  
 
 47  
             /* バイト転送 */
 48  0
             ifc.transferTo(0, ifc.size(), ofc);
 49  
 
 50  0
         } catch (IOException e) {
 51  0
             throw new RuntimeException(e);
 52  0
         } finally {
 53  
             try {
 54  0
                 if (ifc != null)
 55  0
                     ifc.close();
 56  0
                 if (fis != null)
 57  0
                     fis.close();
 58  0
                 if (ofc != null)
 59  0
                     ofc.close();
 60  0
                 if (fos != null)
 61  0
                     fos.close();
 62  0
             } catch (IOException e) {
 63  0
                 throw new RuntimeException(e);
 64  
             }
 65  0
         }
 66  0
     }
 67  
     
 68  
     /**
 69  
      * テキストファイルを読み込む
 70  
      * 
 71  
      * @param filepath
 72  
      * @param charsetName 例:"UTF-8"
 73  
      * @return List<String>
 74  
      */
 75  
     private static List<String> readText(String filepath, String charsetName) {
 76  0
         List<String> result = new ArrayList<String>();
 77  0
         FileInputStream is = null;
 78  0
         InputStreamReader r = null;
 79  0
         BufferedReader br = null;
 80  
         try {
 81  0
             is = new FileInputStream(filepath);
 82  0
             r = new InputStreamReader(is, Charset.forName(charsetName));
 83  0
             br = new BufferedReader(r);
 84  
             // int ch;
 85  
             // while ((ch = in.read()) != -1) {
 86  
             String line;
 87  0
             while ((line = br.readLine()) != null) {
 88  0
                 result.add(line);
 89  
             }
 90  0
         } catch (IOException e) {
 91  0
             throw new RuntimeException(e);
 92  0
         } finally {
 93  
             try {
 94  0
                 if (br != null)
 95  0
                     br.close();
 96  0
                 if (r != null)
 97  0
                     r.close();
 98  0
                 if (is != null)
 99  0
                     is.close();
 100  0
             } catch (IOException e) {
 101  0
                 throw new RuntimeException(e);
 102  
             }
 103  0
         }
 104  0
         return result;
 105  
     }
 106  
     public static List<String> readText(String filepath) {
 107  0
         return readText(filepath, "UTF-8");
 108  
     }
 109  
     /**
 110  
      * テキストファイルを書き出す
 111  
      * 
 112  
      * @param filepath
 113  
      * @param charsetName 例:"UTF-8"
 114  
      * @param text
 115  
      */
 116  
     private static void writeText(String filepath, String charsetName, List<String> text) {
 117  0
         File f = new File(filepath);
 118  0
         OutputStream os = null;
 119  0
         Writer w = null;
 120  0
         BufferedWriter bw = null;
 121  
         try {
 122  0
             os = new FileOutputStream(f);
 123  0
             w = new OutputStreamWriter(os, charsetName);
 124  0
             bw = new BufferedWriter(w);
 125  0
             for (String line : text) {
 126  0
                 bw.write(line);
 127  0
                 bw.newLine();
 128  
                 // bw.flush();
 129  
             }
 130  0
         } catch (Exception e) {
 131  0
             throw new RuntimeException(e);
 132  0
         } finally {
 133  
             try {
 134  0
                 if (bw != null)
 135  0
                     bw.close();
 136  0
                 if (w != null)
 137  0
                     w.close();
 138  0
                 if (os != null)
 139  0
                     os.close();
 140  0
             } catch (IOException e) {
 141  0
                 throw new RuntimeException(e);
 142  
             }
 143  0
         }
 144  0
     }
 145  
     public static void writeText(String filepath, List<String> string) {
 146  0
         writeText(filepath, "UTF-8", string);
 147  0
     }
 148  
 }