java - 在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();
}
}
}
解决方案
推荐阅读
- sql - 为什么使用 Knex.js 选择 PostgreSQL 间隔会返回 JSON 或 JavaScript 对象而不是字符串?
- javascript - 可观察数组不会在 Knokcout 中重置
- java - 在 IntelliJ 中自动为每个参数生成“Objects.requireNonNull”行?
- android - 按下移动主页按钮时重新启动我的应用程序,并从最近的最后一个应用程序按钮统一再次运行
- regex - 正则表达式 - 我如何匹配这个?
- reactjs - 在 React 中获取 webpack chunkhash 字符串
- java - 使用 Java SDK 获取 Google Cloud Compute 中的区域列表
- bash - 创建一个 convertAll() 函数来转换工作目录中的所有 .filetype
- kotlin - Kotlin 本机互操作链接器找不到框架
- apache - 使用 .htaccess,如何在重定向到一页时保留 GET 数据?