java 当我下载文本文件时,如何读取多行文本文件?

ghg1uchk  于 2023-02-28  发布在  Java
关注(0)|答案(1)|浏览(156)

当我把一个文本文件下载到本地驱动器时,我在尝试插入多行文本文件时遇到了问题。我该怎么做?

public class downloads3 {
    private static String bucketName = "testfyp";
    private static String key        = "pig.txt";
    private static String test2;

    public static void main(String[] args) throws IOException {
        AmazonS3 s3Client = new AmazonS3Client(new PropertiesCredentials(
                downloads3.class.getResourceAsStream(
                        "AwsCredentials.properties")));
        try {
            System.out.println("Downloading an object");
            S3Object s3object = s3Client.getObject(new GetObjectRequest(bucketName, key));
            System.out.println("Content-Type: "  + 
                    s3object.getObjectMetadata().getContentType());
            displayTextInputStream(s3object.getObjectContent());

           // Get a range of bytes from an object.

            GetObjectRequest rangeObjectRequest = new GetObjectRequest(
                    bucketName, key);
            rangeObjectRequest.setRange(0, 10);
            S3Object objectPortion = s3Client.getObject(rangeObjectRequest);

            System.out.println("Printing bytes retrieved.");
            displayTextInputStream(objectPortion.getObjectContent());

            FileWriter fw = new FileWriter("C:/Users/L31305/Desktop/" + key + ".txt");
            // String test =
            // String test3 = s3object.getObjectContent();
            fw.write(test2);
            fw.close();
        } catch (AmazonServiceException ase) {
            ase.printStackTrace();
            System.out.println("Caught an AmazonServiceException, which" +
                    " means your request made it " +
                    "to Amazon S3, but was rejected with an error response" +
                    " for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
        } catch (AmazonClientException ace) {
            System.out.println("Caught an AmazonClientException, which means"+
                    " the client encountered " +
                    "an internal error while trying to " +
                    "communicate with S3, " +
                    "such as not being able to access the network.");
            System.out.println("Error Message: " + ace.getMessage());
        }
    }

    private static void displayTextInputStream(InputStream input) throws IOException {
        // Read one text line at a time and display.

        BufferedReader reader = new BufferedReader(new 
                InputStreamReader(input));

        Scanner sc = new Scanner(System.in);
        String line;

        do{
            line = reader.readLine();
            test2 = line;

            System.out.println("    " + line);
        }
        while (reader.readLine() != null); {
            test2 += line;
            System.out.println();
        }
    }
}
kwvwclae

kwvwclae1#

你有一个do/while循环,每次循环你都会丢失一行。同样,第二个块 not 在while里面,它在while循环之后。由于你奇怪的格式,这对你来说可能不是很明显。

相关问题