我有一个与文件类,扫描仪和印刷机有关的作业。大部分任务应该完成,只有我无法解决的一部分。对于上下文,这些是说明
“编写一个程序来编辑文本文件中多余的空格。该程序会将两个或多个空格的任何字符串替换为一个空格。您的程序应按以下方式工作:创建一个临时文件。从文件中复制到临时文件,但不要复制多余的空白文件,将临时文件的内容复制回原始文件中,使用File类中的一个或多个方法来删除临时文件,您还需要使用lass File临时文件的名称应与所有现有文件的名称不同,以便不影响现有文件(正在编辑的文件除外)。程序将要求用户提供文件名但是,它不会向用户询问临时文件的名称,而是会在程序中生成该名称。您可以通过任何清晰有效的方式来生成该名称。临时文件以不太可能的名称开头,例如“ TempX”,并附加一个字符,例如 X',直到找到未命名现有文件的名称。
这是我们必须修复的文件映像,我们需要将该文件的内容复制到一个临时文件中,然后删除句子中的所有多余空格而不会破坏格式。然后,将其修复后,将内容复制回原始testFile并删除临时文件。
我已经接近了,我能够删除所有多余的空格,但是标题错误。我可以使用什么来保留格式并删除任何多余的空格?
如果需要查看,这是我的代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class ProgramA4 {
public static void main(String[] args) {
File file;
int i = 1;
String FileName = "temporary_file"; // name of the temporary file
do {
file = new File("C:\\Users\\emili\\Eclipse-Workplace-2\\Assignment4\\" + FileName + ".txt");
if (!file.exists()) // if the name for Filename (in this case "temporary_file") already exists,
// then create a new file anyway but with an "x" next to it. The loop will work
// no matter how many file names are there.
break;
if (file.exists()) {
System.out.println("That file already exists, creating a new one with an extra 'x' on it!");
// file.delete();
FileName = FileName + "_x";
file = new File("C:\\Users\\emili\\Eclipse-Workplace-2\\Assignment4\\" + FileName + ".txt");
}
} while (file.exists()); // loop if file exist
//
PrintWriter tempfile_printwriter = null;
PrintWriter testfile_printwriter = null;
Scanner tempfile_scanner = null;
Scanner testfile = null;
String line = null;
//
try { // crate a printwriter to add words into the temp file, testfile is a scanner to
// get all the words from testfile. Temp file scanner is used later.
tempfile_printwriter = new PrintWriter(
new FileOutputStream("C:\\Users\\emili\\Eclipse-Workplace-2\\Assignment4\\" + FileName + ".txt"));
tempfile_scanner = new Scanner(
new FileInputStream("C:\\Users\\emili\\Eclipse-Workplace-2\\Assignment4\\" + FileName + ".txt"));
testfile = new Scanner(
new FileInputStream("C:\\Users\\emili\\Eclipse-Workplace-2\\Assignment4\\testfile.txt"));
while (testfile.hasNextLine()) {
// while loop removes empty or extra spaces, preserves format.
// Copies contents of testfile.txt into the temporary_file.
line = testfile.nextLine();
line = line.replaceAll(" +", " ");
tempfile_printwriter.println(line);
}
tempfile_printwriter.close();
} catch (FileNotFoundException exception) {
System.out.println("IO ERROR");
}
String newLine = null;
try {
// printwriter for testFile so we can write inside the file and print the corrected version from temp file.
testfile_printwriter = new PrintWriter(
new FileOutputStream("C:\\Users\\emili\\Eclipse-Workplace-2\\Assignment4\\testfile.txt"));
while (tempfile_scanner.hasNextLine()) {
newLine = tempfile_scanner.nextLine();
testfile_printwriter.println(newLine);
}
testfile_printwriter.close();
testfile.close();
tempfile_scanner.close();
file.deleteOnExit();
// CLOSES ALL STREAMS, DELETES TEMPORARY FILE
} catch (FileNotFoundException exception) {
System.out.println("IO ERROR");
}
}
}
请记住,我正在尝试使用此Java级别上的资源。
答案 0 :(得分:0)
谢谢大家,看来camickr给了我我需要的想法。我不得不去寻找更多可以使用的方法,所以我使用findInLine以便在while循环之前在文件内打印行。
line = testfile.findInLine(" A SCANDAL IN BOHEMIA ");
tempfile_printwriter.print(line);
//Then the while loop starts.
while (testfile.hasNextLine()) {
//while loop removes empty or extra spaces, preserves format.
//Copies contents of testfile.txt into the temporary_file.
line = testfile.nextLine();
line = line.replaceAll(" +"," ");
tempfile_printwriter.println(line);
}
tempfile_printwriter.close();
} catch (FileNotFoundException exception ) {
System.out.println("IO ERROR");
}