将文件转换为大写时如何添加包含名称和日期的行?

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

我试图让我的程序从

.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 file-io io output printwriter
3个回答
1
投票

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

这是代码的更正版本:

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();
    }
}

在这个更正的版本中,我将读取和写入逻辑合并到一个循环中,以确保读取每一行,将其转换为大写,然后立即写入输出文件。此外,用于读取文件的扫描程序包含在 try-with-resources 语句中,以确保它在使用后正确关闭。


1
投票

到目前为止的答案似乎没有涵盖您遇到的困难,即将您的姓名和日期放在转换后的文件的末尾,尽管他们的评论是有效的。我没有在这里重复他们的评论,尽管除了添加我自己的一些评论之外,我还编辑了一些“你的”评论。我没有将重写回显到控制台,但您可以通过将行 读入 String 首先

 来轻松做到这一点,至少像其他答案之一一样:
/* * 2/16/24 * Purpose of this program is to convert contents of a file to upper case letters. */ package uppercasefileconverter; // Package names are lower case in Java import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.FileWriter; import java.util.Scanner; import java.time.LocalDate; public class UpperCaseFileConverter { public static void main(String[] args) throws IOException { final String NAME = "TheBigDookie"; Scanner scanner = new Scanner(System.in); // get file name (my file name is going to be convert.txt for this example) System.out.print(" Enter the Filename: "); String userFileName = scanner.nextLine(); // Specify file - NB this does not open the file File file = new File(userFileName); while (!file.exists()) { System.err.printf( "%s does not exist. Please re-enter the file name%n and remember for this example we are using \"convert.txt\"", userFileName); userFileName = scanner.nextLine(); file = new File(userFileName); } try (Scanner fileToScan = new Scanner(file); PrintWriter fileToWrite = new PrintWriter("convertToUpper.txt")) { while (fileToScan.hasNextLine()) { // Write file to new .txt file fileToWrite.println(fileToScan.nextLine().toUpperCase()); } // Now write name and date fileToWrite.println(NAME); fileToWrite.println(LocalDate.now().toString()); } System.out.println("Contents have been converted to upper case and saved in convertToUpper.txt"); } }



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

为此,您可以使用
fileToWrite

在读取文件内容之前(和之后)添加行(即在

while
循环之前)。
fileToWrite.println("Name: John Doe");

// Your code to read and convert to upper case

String date = ...;
fileToWrite.println("Date: " + date);

根据您想要的格式,有
各种方法

可以获取当前日期。

您应该参考
Jacob Polish

的答案,看看您的 while 循环逻辑用于读取和转换为大写字母是否有问题。

    

© www.soinside.com 2019 - 2024. All rights reserved.