gpt4 book ai didi

java - Printwriter 用空格换行

转载 作者:行者123 更新时间:2023-11-30 12:05:16 24 4
gpt4 key购买 nike

我有一个阅读器读取一个文件来编辑它,然后用打印器保存它。开始输入类似于 this问题是,有时空格会被误认为是新行,例如 here。 .第一次之后我又把它剪得更远喜欢this

我尝试了一些不同的拆分字符,例如
(它实际上是什么(您可以在 System.out.println 中看到))但我无法让它正常工作

原始加载的文本文件是 this getText 的输出是 this

if (lastClicked != 0) {
String path;
switch (lastClicked) {
case 1:
path = "data/alyxia_status.got";
break;
case 2:
path = "data/mog_status.got";
break;
case 3:
path = "data/telias_status.got";
break;
default:
path = "data/tiernen_status.got";
}
String text = textPane.getText();
String toWrite = text.substring(44, text.length() - 16);
System.out.println(toWrite);
String[] parts = toWrite.split("<br>");

FileWriter fileWriter;
try {
fileWriter = new FileWriter(path);
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.print(parts[0]);
for (int i = 1; i<parts.length; i++) {
if (parts[i] != "" && parts[i] != " ") {
printWriter.println();
printWriter.print(parts[i]);
}
}

printWriter.close();
} catch (IOException e1) {
e1.printStackTrace();
System.err.println("Saving failed");
}

}//end if

它应该在字符串 "<br>" 上拆分而不是在中间的空白处(在 System.out.println 中显示“Base”,然后在换行符中显示“Damage”)

最佳答案

以下代码对我来说运行良好,尝试调用 printToFile方法并传递你的 String array to is 作为参数。通过将有问题的代码隔离在一个单独的方法中,应该更容易调试。我还注意到您正在比较 String带有运算符的对象,不建议这样做,并且不会按照您的想法进行操作。阅读此 answer获取更多信息。

public static void printToFile(String path, String[] output) {

FileWriter fileWriter;
try {
fileWriter = new FileWriter(path);
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.print(output[0]);
for (int i = 1; i < output.length; i++)
{
/* DO NOT compare string with opeators like "!=" or "==,
* instead use equals method to properly compare them
*/
if (!output[i].equals("") && !output[i].equals(" ")) {
printWriter.println();
printWriter.print(output[i]);
}
}
printWriter.close();
}
catch (java.io.IOException e1) {
e1.printStackTrace();
System.err.println("Saving failed");
}
}

public static void main(String[] args) throws IOException
{
Path path = Paths.get("sample.txt");
String[] text = new String[] { "these ", " lines ", "should", " be ", " in new ", "line" };

printToFile(path.toString(), text);
Files.readAllLines(path).forEach(System.out::println);
}

输出

these 
lines
should
be
in new
line

编辑:评论中提到的@DodgyCodeException 可能是您问题的实际原因。为了可见性,我将只粘贴评论:

The first 44 characters of the text are discarded because of your text.substring(44, text.length() - 16);. This includes everything up to "-- Base" (just before "Damage").

完整的解决方案

我已在以下代码中针对您的问题编写了完整的解决方案。尝试代码,看看它是否适合您,然后阅读代码下方的解释:

public class Main {

/**
* Use {@link StringBuilder} to build a single {@code String}
* from the read contents of file located under given path.
*
* @param path {@code Path} of the file to read
* @throws IOException if an I/O error occurs reading from the file
* or a malformed or unmappable byte sequence is read.
*/
private static String getInputFileContent(Path path) throws IOException {

StringBuilder sb = new StringBuilder();
Files.readAllLines(path).forEach(sb::append);
return sb.toString();
}

/**
* @return the matched content contained in <body> tag within
* the provided text or {@code null} if there was no match.
*/
private static @Nullable String getHTMLBodyFromText(String text) {

Pattern pattern = Pattern.compile("(?:\\s*?<body>)(?:\\s*)((.*\\s)*)</body>");
Matcher matcher = pattern.matcher(text);
return matcher.find() ? matcher.group(1) : null;
}

public static void printToFile(Path path, String output) {

String toWrite = getHTMLBodyFromText(output);
if (toWrite == null) {
System.err.println("Unable to find body");
return;
}
String[] parts = toWrite.split("<br>");
FileWriter fileWriter;
try {
fileWriter = new FileWriter(path.toString());
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.print(parts[0]);
for (int i = 1; i < parts.length; i++)
{
/* DO NOT compare string with opeators like "!=" or "==,
* instead use equals method to properly compare them
*/
if (!parts[i].equals("") && !parts[i].equals(" ")) {
printWriter.println(parts[i]);
printWriter.print(parts[i]);
}
}
printWriter.close();
}
catch (java.io.IOException e1) {
e1.printStackTrace();
System.err.println("Saving failed");
}
}
public static void main(String[] args) throws IOException
{
Path inputPath = Paths.get("input.txt");
Path outputPath = Paths.get("output.txt");

printToFile(outputPath, getInputFileContent(inputPath));
}
}

我用过 Regex查找 <body> 中包含的文本您提供的输入文件的标签 (我们想要的实际内容位于 group 1 ) 这是导致此问题的部分。如果您有兴趣了解此代码中包含的模式如何工作,请参阅此 demo .

您的其余代码工作正常,因此您所要做的就是调用 printToFile方法并传递textPane.getText()的返回值作为output String参数,它将为您处理所需的结果并将其打印到位于您选择的路径下的文本文件中。

关于java - Printwriter 用空格换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56430160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com