Java 为什么我正在写的文件中有一行额外的内容?

Java 为什么我正在写的文件中有一行额外的内容?,java,file,arraylist,Java,File,Arraylist,如何从文本文件中读取用户名的所有名称并将其写入新的文本文件中。没有编译错误,但我的输出不正确。帮我找出问题或给我举个例子。多谢各位 文件resident.txt: james|123 kelvin|123 预期结果: fee.txt jan|james jan|kelvin 我的最终结果是: fee.txt jan|james jan|kelvin jan| 资料来源: private void btnConfirmActionPerformed(java.awt.e

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

文件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! ");
    }
}    
FileFunction.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来解决此问题:

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


你明白了。<>括号之间的空间是放置将存储在数组中的对象类型的位置。地图、集合等也是如此。

请添加FileFunction.getContent的源代码,因为可能存在问题。作为旁注:不要使用原始类型。使用ArrayList,而不是ArrayList,并将for与字符串变量(而不是对象)一起使用,这样您就不需要使用obj.toString。您应该阅读如何调试像您这样的小程序。@realpoint我已经发布了filefunction.java?我不知道如何使用arraylist。能给我一个例子吗?你的源文件末尾有换行符吗?也许getContent应该解决这个问题。@Davidermann对不起,我不明白你的问题
while ((line = in.readLine()) != null) {
    if (line.trim().length() > 0) {
        ls.add(line);
    }
}
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