我读取文件然后写入文件,文本之间的空格消失了
I read file then write the file and the spaces between text disappear
我从临时文件中读取并将其写入永久文件,但字符串在某处丢失了所有空格
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
String b, filename;
b = null;
filename = (textfieldb.getText());
try {
// TODO add your handling code here:
dispose();
Scanner scan;
scan = new Scanner(new File("TempSave.txt"));
StringBuilder sb = new StringBuilder();
while (scan.hasNext()) {
sb.append(scan.next());
}
b = sb.toString();
String c;
c = b;
FileWriter fw = null;
try {
fw = new FileWriter(filename + ".txt");
} catch (IOException ex) {
Logger.getLogger(hiudsjh.class.getName()).log(Level.SEVERE, null, ex);
}
PrintWriter pw = new PrintWriter(fw);
pw.print(c);
pw.close();
System.out.println(c);
} catch (FileNotFoundException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
dispose();
hiudsjh x = new hiudsjh();
x.setVisible(true);
System.out.println(b);
}
没有错误信息只是输出应该是一个有剩余空间的文件
这个:
while (scan.hasNext()) {
sb.append(scan.next());
}
是删除空格的原因...next()
将 return 来自扫描器的下一个完整标记,这不包括空格。您将需要附加空格或更改读取文件的方式...
来自扫描仪documentation:
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
并且来自 next methods docu
Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.
换句话说,Scanner 将输入字符串拆分为没有空格的序列。要将文件作为字符串读取,您可以使用 new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
读取整个文件。
您可以逐行读取文件并在每行后附加一个行分隔符,而不是扫描每个标记:
while (scan.hasNextLine()) {
sb.append(scan.nextLine());
sb.append(System.lineSeparator());
}
而不是 hasNext()
和 next()
你没有得到空格,使用 hasNextLine()
和 nextLine()
逐行读取 filr 并在后面追加每行一个行分隔符:
while (scan.hasNextLine()) {
sb.append(scan.nextLine());
sb.append(System.lineSeparator());
}
我从临时文件中读取并将其写入永久文件,但字符串在某处丢失了所有空格
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
String b, filename;
b = null;
filename = (textfieldb.getText());
try {
// TODO add your handling code here:
dispose();
Scanner scan;
scan = new Scanner(new File("TempSave.txt"));
StringBuilder sb = new StringBuilder();
while (scan.hasNext()) {
sb.append(scan.next());
}
b = sb.toString();
String c;
c = b;
FileWriter fw = null;
try {
fw = new FileWriter(filename + ".txt");
} catch (IOException ex) {
Logger.getLogger(hiudsjh.class.getName()).log(Level.SEVERE, null, ex);
}
PrintWriter pw = new PrintWriter(fw);
pw.print(c);
pw.close();
System.out.println(c);
} catch (FileNotFoundException ex) {
Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
dispose();
hiudsjh x = new hiudsjh();
x.setVisible(true);
System.out.println(b);
}
没有错误信息只是输出应该是一个有剩余空间的文件
这个:
while (scan.hasNext()) {
sb.append(scan.next());
}
是删除空格的原因...next()
将 return 来自扫描器的下一个完整标记,这不包括空格。您将需要附加空格或更改读取文件的方式...
来自扫描仪documentation:
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
并且来自 next methods docu
Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.
换句话说,Scanner 将输入字符串拆分为没有空格的序列。要将文件作为字符串读取,您可以使用 new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8);
读取整个文件。
您可以逐行读取文件并在每行后附加一个行分隔符,而不是扫描每个标记:
while (scan.hasNextLine()) {
sb.append(scan.nextLine());
sb.append(System.lineSeparator());
}
而不是 hasNext()
和 next()
你没有得到空格,使用 hasNextLine()
和 nextLine()
逐行读取 filr 并在后面追加每行一个行分隔符:
while (scan.hasNextLine()) {
sb.append(scan.nextLine());
sb.append(System.lineSeparator());
}