English 中文(简体)
在将档案转换为上一级案件时,如何添加名称和日期的线子?
原标题:How to add lines with name and date when converting file to upper case?

我正试图让我的节目读到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
"
             + "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();
        
    }
    

    }

我需要该方案在我的名字的档案中增加一个新线,然后在文件日期的末尾增加,因此产出应当比投入多两条线。

在我的班子和Java课本中,我根本没有学会如何在文件的具体内容上添加内容,因此我不知道我会如何增加第一行的内容。 无论是那还是那个时候,还是那个时候,都忽略了这个部分。

问题回答

你的法典似乎在正确的轨道上,但有一个小问题妨碍它按照预期开展工作。 你试图从档案中读取两倍,因为经过第一次阅卷之后,扫描仪已经接近尾声,随后尝试阅读文件,不会产生更多的投入。

页: 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
"
                    + "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();
    }
}

在经过更正的版本中,我把阅读和书写逻辑合并为一个单一循环,以确保每一行都读到,转换成上门,然后立即写到产出档案中。 另外,从档案中读取的斯堪纳语也载于一份有案可查的资源说明中,以确保其使用后适当关闭。

迄今为止,答案似乎并未涵盖你所遇到的困难,即把你的名字和日期放在被转换的档案末尾,尽管他们的意见是有效的。 我没有在这里重复他们的评论,尽管我除了增加我自己的部分外,还编辑了一些your<>/em>的评论。 我没有回过这ole子,但你可以很容易地读到into a String First,因为其他至少一个答案是:

/*
 * 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");

    }
}

因此,我昨天晚上工作,处理我应该处理此事的次序,并得出结论:

I should print my name to the output. then print the converted data. then I should print the date to the file.

而这项工作也是如此。 我赞赏大家给我的所有ti!

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


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

public class UpperCaseFileConverter {

    public static void main(String[] args) throws IOException 
    {  
        //Create scanner for keyboard input from user 
        Scanner scanner=new Scanner(System.in);
        
        //Create string name to hold user input
        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);
        
        //Error message
        while(!file.exists()) {
             System.out.print(userFileName+" does not exists. Please re- 
   enter the file name
"
             + "remember for this example we are using "convert.txt"" );
             userFileName = scanner.nextLine();
             file = new File(userFileName);
        }
        //Create output file     
        Scanner fileToScan = new Scanner(file);
        PrintWriter fileToWrite = new PrintWriter("convertToUpper.txt");
        
        //Test file
        System.out.println("NAME");
        while(fileToScan.hasNext()) {
            System.out.println(fileToScan.nextLine().toUpperCase());
        }
        System.out.println("02,19,2024");
        
        //Write file to new .txt file
        //Add name
        fileToWrite.println("NAME");
        //Add file
        while(fileToScan.hasNext()) {
            fileToWrite.println( fileToScan.nextLine().toUpperCase());
        }
        //Add date to the end
        fileToWrite.println("02,19,2024");
        
        //Confirm message
        System.out.println("Contents have been converted to upper case, 
    Name and date have been added. New data saved in convertToUpper.txt");
        //Close input and output files.
       fileToWrite.close();
       fileToScan.close();
        
    }
    

    }

我需要该方案在我的名字的档案中增加一个新线,然后在文件日期的末尾增加,因此产出应当比投入多两条线。

为此,您可使用ToWrite/code>,在(和之后)读到档案内容之前(即

fileToWrite.println("Name: John Doe");

// Your code to read and convert to upper case

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

Depending on what format you want, there are various ways to get the current date.


请参看。 Jacob Poland 回答,看你是否与你的<代码>有问题,同时,读写和改写上上上门的规律。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签