gpt4 book ai didi

java - 为什么我正在编写的文件中多了一行?

转载 作者:太空宇宙 更新时间:2023-11-04 13:54:12 25 4
gpt4 key购买 nike

如何从文本文件中读取用户名的所有名称并将其写入新的文本文件中。没有编译错误,但我的输出不正确。帮我找出问题所在或者给我一个例子。谢谢。

文件resident.txt:

  james|123
kelvin|123

预期结果:

 fee.txt
jan|james
jan|kelvin

我的最终结果:

 fee.txt
jan|james
jan|kelvin
jan|

来源:

private void btnConfirmActionPerformed(java.awt.event.ActionEvent evt) {                                           
// TODO add your handling code here:
boolean flag = false;
File file = new File("resident.txt");
ArrayList al = FileFunction.getContent(file);
for (Object obj : al) {
String newobj = obj.toString();
String s[] = newobj.split("\\|");

String strMonth = txtMonth.getSelectedItem().toString();
try (PrintWriter out = new PrintWriter(new FileWriter("fee.txt", true))) {
out.println(strMonth + "|" + s[0]);
flag = true;
new ClerkPage(txtUsername.getText()).setVisible(true);
this.dispose();
} catch (Exception e) {
}
}
if(flag){
JOptionPane.showMessageDialog(this, "Monthly fee was successfully charged! ");
}
}

文件函数.java

package data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

public class FileFunction {

public static ArrayList getContent(File f) {
ArrayList ls = null;
try (BufferedReader in = new BufferedReader(new FileReader(f));) {
String line;
ls = new ArrayList();
while ((line = in.readLine()) != null) {
ls.add(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return ls;
}

public static boolean setContent(String oldInput, String newInput, File f) {
boolean isDone = false;
//temp bag
ArrayList tempLs = null;
try (Scanner scan = new Scanner(f);) {
tempLs = new ArrayList();
while (scan.hasNextLine()) {
String tempLine = scan.nextLine();
//check if it s the same with old..
if (tempLine.equals(oldInput)) {
tempLs.add(newInput);
} else {
tempLs.add(tempLine);
}
}
if (makeNewContent(tempLs, f)) {
isDone = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return isDone;
}

private static boolean makeNewContent(ArrayList newContent, File f) {

try {
f.createNewFile();
StringBuilder sb = new StringBuilder();
for (Object object : newContent) {
sb.append(object);
sb.append("\r\n");
}
addContent(sb.toString(), f, false);
return true;
} catch (Exception e) {
return false;
}

}

public static boolean addContent(String input, File f, boolean append) {
boolean isDone = false;
try (PrintWriter out = new PrintWriter(new FileWriter(f, append))) {
out.print(input);
isDone = true;
} catch (Exception e) {
}
return isDone;
}

public static boolean removeContent(String oldInput, File f) {
boolean isDone = false;
ArrayList tempLs = null;
try (BufferedReader in = new BufferedReader(new FileReader(f))) {
tempLs = new ArrayList();
String line;
while ((line = in.readLine()) != null) {
if (line.equals(oldInput)) {
continue;
}
tempLs.add(line);
}
if (f.exists()) {
f.delete();
}
if (makeNewContent(tempLs, f)) {
isDone = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return isDone;
}
}

最佳答案

resident.txt 末尾必须有换行符。我使用模拟文本文件尝试了您的代码,当我在 resident.txt 底部添加额外的(空白)行时,我重现了您的问题。您可以通过将此修改添加到 FileFunction.getContent() 来解决此问题。 :

while ((line = in.readLine()) != null) {
if (line.trim().length() > 0) {
ls.add(line);
}
}

正如其他人所指出的,您应该更改创建 ArrayList 的方式。 s:

ArrayList ls = null; // wrong
ArrayList<String> ls = null; // correct
ls = new ArrayList<String>(); // can do "new ArrayList<>();" if you're using Java 8

public static ArrayList getContent(File f) // wrong
public static ArrayList<String> getContent(File f) // correct

你明白了。 < > 之间的空格括号是放置将存储在数组中的对象类型的位置。 Map 也是如此, Collection

关于java - 为什么我正在编写的文件中多了一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30002575/

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