首页 > 解决方案 > 在java中使用文件时只读取一行的逻辑错误

问题描述

我有一个任务,我需要创建一个包含两个文件的程序,第一个存储一组字符串,然后第二个存储这些字符串的副本,但全部大写。

我成功地创建了这个程序,但是当我的教授给我的作业评分时,他从这行代码中因为“只读一行时出现逻辑错误”而扣分:

 String first_file_string = first_file_input.nextLine();

我不太确定这意味着什么或如何解决它,任何输入或帮助将不胜感激。感谢您的时间和考虑!

这是我的整个代码,上面的行是第 50 行,我将在行的前后放置 3 个 *。

import java.io.IOException;
import java.io.*;
import java.util.Scanner;


public class UppercaseFileConverter
{
    public static void main(String[] args) throws IOException
    {
        Scanner keyboard = new Scanner(System.in);
        // file creation and writing string of characters to the file
        System.out.print("Enter a file name to create the first file to read from: ");
        String file_one = keyboard.nextLine();
        PrintWriter outputFile_one = new PrintWriter(file_one);

    System.out.print("Enter a string of characters to store in the first file: ");
    String user_char = keyboard.nextLine();
    outputFile_one.println(user_char);
    outputFile_one.close();
    System.out.printf("\nThe file: %s has been generated.\n\n", file_one);

    System.out.print("Enter a file name to create the second file to write into: ");
    String file_two = keyboard.nextLine();
    PrintWriter outputFile_two = new PrintWriter(file_two);
    outputFile_two.close();
    System.out.printf("\nThe file: %s has been generated.\n", file_two);

    // Opening the first file
    System.out.print("\nEnter the first file name to read from: ");
    String first_file = keyboard.nextLine();

    // reading the first file
    File open_first = new File(first_file);
    if (!open_first.exists())
    {   // If file doesn't exist, program ends
        System.out.printf("ERROR: File, %s, cannot be found.\n", first_file);
        System.out.println();
        System.exit(0);
    }
    else
    {
        // accessing contents of first file
        Scanner first_file_input = new Scanner(open_first);
        ***String first_file_string = first_file_input.nextLine();***

        // opening the second file
        System.out.print("\nEnter the second file name to write into: ");
        String second_file = keyboard.nextLine();

        // reading the second file
        File open_second = new File(second_file);
        if (!open_second.exists())
        {   // If file doesn't exist, program ends
            System.out.printf("ERROR: File, %s, cannot be found.\n", second_file);
            System.out.println();
            System.exit(0);
        }
        else
        {   // storing data into the second file
            String second_file_string = first_file_string.toUpperCase();
            FileWriter second_fwriter = new FileWriter(open_second, true);
            PrintWriter output_second = new PrintWriter(second_fwriter);
            output_second.println(second_file_string);
            output_second.close();
        }
        first_file_input.close();

        System.out.printf("\nThe contents of %s has been changed to uppercase and stored in %s.\n", file_one, file_two);
        System.out.println();
    }
}

}

标签: javafileimportlogic

解决方案


推荐阅读