将文件转换为大写

问题描述 投票:0回答:1

我试图让我的程序从 .txt 文件调用输入,然后将输入转换为大写字母。

/*
 * 2/16/24
 * Purpose of this program is to convert contents of a file to upper case letters.
 */


package UpperCaseFileConverter;
//import
import java.io.File;
import java.io.*;
import java.util.Scanner;

public class UpperCaseFileConverter {

    public static void main(String[] args) throws IOException 
    {    
        Scanner scanner=new Scanner(System.in);
        
        String userFileName;
          
        // get file name (my file name is going to be convert.txt for this example)
        System.out.print(" Enter the Filename: ");
        userFileName = scanner.nextLine();
               
        // open file        
        File file = new File(userFileName);
        
        while(!file.exists()) {
             System.out.print(userFileName+"does not exists. Please re-enter the file name\n"
             + "remember for this example we are using \"convert.txt\"" );
             userFileName = scanner.nextLine();
             file = new File(userFileName);
        }
        
        Scanner fileToScan = new Scanner(file);
        PrintWriter fileToWrite = new PrintWriter("convertToUpper.txt");
        //Test file
        while(fileToScan.hasNext()) {
            System.out.println(fileToScan.nextLine().toUpperCase());
        //Write file to new .txt file   
        }
        while(fileToScan.hasNext()) {
        fileToWrite.println( fileToScan.nextLine().toUpperCase());
        }
        System.out.println("Contents have been converted to upper case and saved in convertToUpper.txt");
       fileToWrite.close();
       fileToScan.close();
        
    }
    

    }

我需要程序在包含我名字的文件中添加一个新行,然后将日期添加到文件末尾,因此输出应该比输入多 2 行。

在我的课程和 Java 教科书中,我根本没有被教导如何添加到文档的特定行,所以我不知道如何添加到第一行......要么是这样,要么是我和白痴,错过了那部分。

java io
1个回答
0
投票

您的代码似乎走在正确的轨道上,但有一个小问题阻止它按预期工作。您尝试从文件中读取两次,这不会按预期工作,因为在第一次循环读取文件后,扫描仪已到达文件末尾,并且后续尝试读取文件将不会产生任何更多输入。

这是代码的更正版本:

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class UpperCaseFileConverter {

    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);

        String userFileName;

        // get file name (my file name is going to be convert.txt for this example)
        System.out.print("Enter the Filename: ");
        userFileName = scanner.nextLine();

        // open file
        File file = new File(userFileName);

        while (!file.exists()) {
            System.out.print(userFileName + " does not exist. Please re-enter the file name\n"
                    + "remember for this example we are using \"convert.txt\"");
            userFileName = scanner.nextLine();
            file = new File(userFileName);
        }

        // Open a new PrintWriter for writing to the output file
        PrintWriter fileToWrite = new PrintWriter("convertToUpper.txt");

        // Open a Scanner to read from the input file
        try (Scanner fileToScan = new Scanner(file)) {
            // Read each line from the input file, convert it to uppercase, and write to the output file
            while (fileToScan.hasNextLine()) {
                String line = fileToScan.nextLine();
                String upperCaseLine = line.toUpperCase();
                fileToWrite.println(upperCaseLine);
                // Optionally, you can also print the converted line to the console
                System.out.println(upperCaseLine);
            }
        }

        System.out.println("Contents have been converted to uppercase and saved in convertToUpper.txt");
        // Close the PrintWriter
        fileToWrite.close();
    }
} ```

In this corrected version, I've combined the reading and writing logic into a single loop to ensure that each line is read, converted to uppercase, and then immediately written to the output file. Also, the Scanner for reading from the file is enclosed within a try-with-resources statement to ensure it's properly closed after its use.
© www.soinside.com 2019 - 2024. All rights reserved.