[英]Printwriter making newlines out of spaces
我的阅读器正在读取文件,以进行编辑并在以后使用印刷机保存。 开始输入像这样的问题是,有时空格被误认为是新线不知何故喜欢这里 。 像这样第一次后我又进一步削减了它
我尝试了一些不同的分割字符,例如
(实际上是什么(您可以在System.out.println中看到)),但我无法使其正常工作
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
数组作为参数传递给is。 通过用单独的方法隔离有问题的代码,它应该更容易调试。 我还注意到您正在将String
对象与运算符进行比较,不建议这样做,并且也不会按照您的想法做。 阅读此答案以获取更多信息。
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可能是您问题的实际原因。 出于可见性考虑,我将粘贴以下注释:
由于您的text.substring(44,text.length()-16);,该文本的前44个字符被丢弃。 这包括直到“-基础”为止的所有内容(就在“损坏”之前)。
完整的解决方案
我在以下代码中为您的问题写了完整的解决方案。 试试看代码,看看它是否对您有用,然后阅读代码下方的说明:
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
) ,这是导致此问题的部分。 如果您还想了解此代码中包含的模式如何工作,请参阅此演示 。
其余代码工作正常,因此您所要做的就是调用printToFile
方法,并将textPane.getText()
的返回值作为output
String
参数传递,它将为您处理所需的结果并将其打印到位于下面的文本文件中。您选择的路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.