提问人:Emil 提问时间:10/7/2020 更新时间:10/7/2020 访问量:851
Java:如何在不破坏线条外观的情况下将 txt 文件的内容复制到另一个 txt 文件中(扫描仪)?
Java: How to copy the contents of a txt file into another txt file without ruining how the lines look (scanner)?
问:
我有一个与文件类、扫描仪和打印机相关的作业。大部分任务应该已经完成,这只是我无法解决的部分。对于上下文,这些是说明
“编写一个程序来编辑文本文件以获取额外的空白。该程序将用单个空格替换任何包含两个或多个空格的字符串。您的程序应按如下方式工作:创建一个临时文件。从文件复制到临时文件,但不要复制多余的空白。将临时文件的内容复制回原始文件。使用类 File 中的一个或多个方法删除临时文件。您还需要将 lass 文件用于程序中的其他事情。临时文件的名称应与所有现有文件不同,以便现有文件不受影响(正在编辑的文件除外)。您的程序将要求用户输入要编辑的文件的名称。但是,它不会要求用户提供临时文件的名称,而是会在程序中生成名称。您可以以任何清晰有效的方式生成名称。生成临时文件的一种可能方法是从一个不太可能的名称(如“TempX”)开始,并附加一个字符,例如 X',直到找到不命名现有文件的名称。
这是我们必须修复的文件的图像,我们需要将此文件的内容复制到临时文件中,然后在不破坏格式的情况下删除句子中的任何多余空格。然后,一旦它修复,我们将 conents 复制回原始 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赞
Emil
10/7/2020
#1
谢谢大家,似乎用户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");
}
评论