尝试将txt读取到String时找不到路径

时间:2017-05-12 06:06:40

标签: jenkins groovy

我想在我的groovy脚本中读取一个txt文件。因此,我使用以下代码:

stage('exists'){

    def filePath = "C:\\JenkinsSlave\\lastSuccessfull\\folder\\lastSuccess.txt"

    node('fetch'){


        if (fileExists(filePath)) {
            echo 'Yes'
            File file = new File(filePath).text 
            println file 
        } else {
            echo 'No. create file...'
            fileOperations([fileCreateOperation(fileContent: '1.111', fileName: filePath)])

        }

    }
}

如果文件不存在,脚本会创建它。如果存在,则if cause检测到文件。但如果我想阅读内容,我会收到java.io.FileNotFoundException: C:\JenkinsSlave\lastSuccessfull\folder(The system cannot find the path specified)错误消息。但这怎么可能呢?路径不能是假的,因为我使用相同的变量......

2 个答案:

答案 0 :(得分:0)

替换此

  static void Main(string[] args)
        {
            Console.WriteLine("Blob encryption sample");

            // Retrieve storage account information from connection string
            // How to create a storage connection string - https://azure.microsoft.com/en-us/documentation/articles/storage-configure-connection-string/
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            "DefaultEndpointsProtocol=https;AccountName=brandofirststorage;AccountKey=4j8EjQzNtkzQ22Xp3NZcxvJz/+PUOOOQRTSZ9TieQg1lYM6eBCDpKoJgMcNWoG6p1GjMQhkYrxPKRBralzQoZA==;EndpointSuffix=core.windows.net");

            CloudBlobClient client = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = client.GetContainerReference("example");


                container.CreateIfNotExists();
                int size = 5 * 1024 * 1024;
                byte[] buffer = new byte[size];

                Random rand = new Random();
                rand.NextBytes(buffer);

                CloudBlockBlob blob = container.GetBlockBlobReference("test");

                // Create the IKey used for encryption.
                RsaKey key = new RsaKey("private:key1");

                // Create the encryption policy to be used for upload.
                BlobEncryptionPolicy uploadPolicy = new BlobEncryptionPolicy(key, null);

                // Set the encryption policy on the request options.
                BlobRequestOptions uploadOptions = new BlobRequestOptions() { EncryptionPolicy = uploadPolicy };

                Console.WriteLine("Uploading the encrypted blob.");

                // Upload the encrypted contents to the blob.
                using (MemoryStream stream = new MemoryStream(buffer))
                {
                    blob.UploadFromStream(stream, size, null, uploadOptions, null);
                }





                // Download the encrypted blob.
                // For downloads, a resolver can be set up that will help pick the key based on the key id.
                LocalResolver resolver = new LocalResolver();
                resolver.Add(key);

                BlobEncryptionPolicy downloadPolicy = new BlobEncryptionPolicy(null, resolver);

                // Set the decryption policy on the request options.
                BlobRequestOptions downloadOptions = new BlobRequestOptions() { EncryptionPolicy = downloadPolicy };

                Console.WriteLine("Downloading the encrypted blob.");

                // Download and decrypt the encrypted contents from the blob.
                using (MemoryStream outputStream = new MemoryStream())
                {
                    blob.DownloadToStream(outputStream, null, downloadOptions, null);
                }

                Console.WriteLine("Press enter key to exit");
                Console.ReadLine();


        }

 File file = new File(filePath)).text 
        println file 

答案 1 :(得分:0)

我刚看到Jenkins管道语法中有一个命令

fileContents = readFile encoding: 'UTF-8', file: filePath

但我仍然想知道为什么jenkins在使用简单的groovy代码时给出了上述错误......