我正试图让我的节目读到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课本中,我根本没有学会如何在文件的具体内容上添加内容,因此我不知道我会如何增加第一行的内容。 无论是那还是那个时候,还是那个时候,都忽略了这个部分。