2016-12-02 12 views
0

ファイルが残っているかディレクトリが存在しません。 Spring Application Contextを作成するGroovyスクリプト内で実行しています。同じ種類のパスを使用して別のファイルを簡単に読み込みます。しかし、私が読んでいるファイルはSpringのクラスパスにあります。このスクリプトは、さまざまなファイルシステムを持つ多数のユーザーによって実行される可能性があるため、パスをハードコードすることはできません。私は相対的な道が必要です。新しいファイルを任意のディレクトリに保存するGroovy

これはクラスの上にありますが、重要な情報です。

private static String saveFilesToLocation = "/retrieve/"; 

ここにコードがあります。

CSVReader reader = new CSVReader(new InputStreamReader(balanceFile), SEPARATOR) 
    String[] nextLine 
    int counter = 0; 
    while ((nextLine = reader.readNext()) != null) { 
     counter++ 
     if (nextLine != null && (nextLine[0] != 'FileLocation')) { 
      counter++; 
      try { 

       //Remove 0, only if client number start with "0". 
       String fileLocation = nextLine[0]; 

       byte[] fileBytes = documentFileService.getFile(fileLocation); 
       if (fileBytes != null) { 
        String fileName = fileLocation.substring(fileLocation.indexOf("/") + 1, fileLocation.length()); 
        File file = new File(saveFilesToLocation+fileLocation); 
        file.withOutputStream { 
         it.write fileBytes 
        } 

        println "$counter) Wrote file ${fileLocation} to ${saveFilesToLocation+fileLocation}" 
       } else { 
        println "$counter) UNABLE TO RETRIEVE FILE: $fileLocation"; 
       } 

      } catch (Exception e) { 
       e.printStackTrace() 
      } 
     } 
    } 

文字列内のパスには、余分な文字は含まれません。

UPDATE:

おかげであなたの答えは、あまりにも作業し、働いていた私たちの最終的な結果よりも優れgroovinessを持っているでしょうloteq。それは一種のものなので、あなたが持っているより良いバージョンに変更する時間がありません。

私たちのために働いたコードはここにありますが、saveFilesToLocationがすでに存在するディレクトリに設定されている点を除いて、これは上記と同じです。以前のものは存在しなかったので、loteq のようにmkdirを呼び出す必要がありました。

private static String saveFilesToLocation = "/tmp/retrieve/"; 


    CSVReader reader = new CSVReader(new InputStreamReader(balanceFile), SEPARATOR) 
    String[] nextLine 
    int counter = 0; 
    while ((nextLine = reader.readNext()) != null) { 
     if (nextLine != null && (nextLine[0] != 'FileLocation')) { 
      counter++; 
      try { 

       //Remove 0, only if client number start with "0". 
       String fileLocation = nextLine[0]; 

       byte[] fileBytes = documentFileService.getFile(fileLocation); 
       if (fileBytes != null) { 
        String fileName = fileLocation.substring(fileLocation.indexOf("/") + 1, fileLocation.length()); 
        File file = new File(saveFilesToLocation+fileName); 
        file.withOutputStream { 
         it.write fileBytes 
        } 

        println "$counter) Wrote file ${fileLocation} to ${saveFilesToLocation+fileLocation}" 
       } else { 
        println "$counter) UNABLE TO RETRIEVE FILE: $fileLocation"; 
       } 

      } catch (Exception e) { 
       e.printStackTrace() 
      } 
     } else { 
      counter++; 
     } 
    } 

答えて

1

あなたのコードには何かが追加されているようですが、それはバグだとは確信できません。

fileNameを計算し、それを実際に使用してターゲットファイルを作成しません。代わりに、プレフィックスsaveFilesToLocationに元のパスを追加するだけです。

   String fileName = fileLocation.substring(fileLocation.indexOf("/") + 1, fileLocation.length()); 
       File file = new File(saveFilesToLocation+fileLocation); 

これは奇妙に思えます。

次に、fileLocationに作成する必要のあるディレクトリが含まれている場合は、それらをmkdirs()する必要があります。そうしないと、エラーが発生します。

2つのスニペットを提供します.1つは、上記のyouirコードがバグであると想定しています。他のものは、より安全なやり方で、慣用的なグルーヴィーで行います。

文字列場合、最初ではなく、実際のファイルオブジェクトで作業をすることができます:上記のコードのバグを想定

private static File saveFilesToLocationDir = saveFilesToLocation as File 

バージョン:あなたのような作成したファイル名を(使用していない

private static String saveFilesToLocation = "/retrieve/"; 

private static File saveFilesToLocationDir = saveFilesToLocation as File 

CSVReader reader = new CSVReader(new InputStreamReader(balanceFile), SEPARATOR) 
String[] nextLine 
int counter = 0; 
while ((nextLine = reader.readNext()) != null) { 
    counter++ 
    if (nextLine != null && (nextLine[0] != 'FileLocation')) { 
     counter++; 
     try { 

      //Remove 0, only if client number start with "0". 
      String fileLocation = nextLine[0]; 

      byte[] fileBytes = documentFileService.getFile(fileLocation); 
      if (fileBytes != null) { 

       int firstSlash = fileLocation.indexOf("/") + 1 
       String fileName = fileLocation[firstSlash..-1] 

       File destination = new File(saveFilesToLocationDir, fileName) 
       destination.parentFile.mkdirs() 

       destination.withOutputStream { it << fileBytes } 

       println "$counter) Wrote file ${fileLocation} to ${destination.absolutePath}" 
      } else { 
       println "$counter) UNABLE TO RETRIEVE FILE: $fileLocation"; 
      } 

     } catch (Exception e) { 
      e.printStackTrace() 
     } 
    } 
} 

バージョンを):

private static String saveFilesToLocation = "/retrieve/"; 

private static File saveFilesToLocationDir = saveFilesToLocation as File 

CSVReader reader = new CSVReader(new InputStreamReader(balanceFile), SEPARATOR) 
String[] nextLine 
int counter = 0; 
while ((nextLine = reader.readNext()) != null) { 
    counter++ 
    if (nextLine != null && (nextLine[0] != 'FileLocation')) { 
     counter++; 
     try { 

      //Remove 0, only if client number start with "0". 
      String fileLocation = nextLine[0]; 

      byte[] fileBytes = documentFileService.getFile(fileLocation); 
      if (fileBytes != null) { 

       int firstSlash = fileLocation.indexOf("/") + 1 
       String fileName = fileLocation[firstSlash..-1] 

       File destination = new File(saveFilesToLocationDir, fileLocation) 
       destination.parentFile.mkdirs() 

       destination.withOutputStream { it << fileBytes } 

       println "$counter) Wrote file ${fileLocation} to ${destination.absolutePath}" 
      } else { 
       println "$counter) UNABLE TO RETRIEVE FILE: $fileLocation"; 
      } 

     } catch (Exception e) { 
      e.printStackTrace() 
     } 
    } 
} 
関連する問題