コピー

2012年03月29日

【VBA】ブックのコピー

【準備】
・プロジェクトにて、
 参照設定->Microsoft Scripting Runtime の参照にチェックを入れる

《ファイルのコピー》
Sub FILECOPY_SAMPLE1(strInput As String, strOutput As String)
  Dim objFSO As FileSystemObject
  ' コピー元ファイルのパス
  strInput = ThisWorkbook.Path & "\" & strInput & ".xls"
  ' コピー先ファイルのパス
  strOutput = ThisWorkbook.Path & "\" & strOutput & ".xls"
  ' FileSystemObjectのインスタンス生成
  Set objFSO = New FileSystemObject
  ' FSOによるファイルコピー
  objFSO.CopyFile strInput, strOutput, True
  Set objFSO = Nothing ' マナー
End Sub

《FILECOPY_SAMPLE1()の呼び出し》
Sub sample()
  Call FILECOPY_SAMPLE1("from", "to")
End Sub

結果、from.xlsのコピーとして、to.xlsが同ディレクトリにできる。
続きを読む

PermalinkComments(0)

【Java】CSVファイルを文字コード指定でコピー

/**
* csvファイルの作成+文字コード指定
* @param path コピー先ファイルを入れるディレクトリのパス
* @param outputName コピー元ファイルの名前
* @param inputName コピー先ファイルの名前
* @throws IOException
*/
static void copyCSV(String path,String outputName,String inputName) throws IOException{
  File from = new File("./data/" + inputName + ".csv"); // コピー元File
  File to = new File(path+"/" + outputName + ".csv"); // コピー先File
  //文字コード指定で書き出す
  InputStreamReader br;
  String line;
  // 文字コード指定で読み込む
  br = new InputStreamReader (new FileInputStream(from),"SJIS");
  LineNumberReader lnr = new LineNumberReader(br);
  // 文字コード指定で書き込む
  PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(to),"SJIS")));
  // 最終行まで、一行ずつ読み込み→書き込み
  while ((line = lnr.readLine()) != null) {
    pw.println(line);
  }
  br.close();
  lnr.close();
  pw.close();
}


PermalinkComments(0)

2012年01月26日

【Java】バイナリファイルをコピーする

【準備】
import java.io.*;


《バイナリファイルをコピーする》
/**
* バイナリファイルをコピー&ファイル名書き換え
* @param inPath コピー元ファイルのパス
* @param inputName コピー元ファイルの名前
* @param outPath コピー先ファイルのパス
* @param outputName コピー先ファイルの名前
* @throws IOException
*/
static void createFig(String inPath ,String inputName , String outPath , String outputName ) throws IOException {
  File inputFig = new File(inPath +"//" + outputName + ".pdf");
  BufferedInputStream input = new BufferedInputStream(new FileInputStream(inputFig));
  File outputFig = new File(outPath + "//" + outputName + ".pdf");
  BufferedOutputStream output= new BufferedOutputStream(new FileOutputStream(outputFig));
  int bf;
  while ((bf = input.read()) >= 0){
    output.write(bf);
  }
  output.flush();
  output.close();
  input.close();
}



PermalinkComments(0)