繁体   English   中英

为什么我正在写的文件中有多余的一行?

[英]Why is there an extra line in the file I am writing?

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

文件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()来解决此问题:

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

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

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

你明白了。 在之间的空间< >括号是放置将被存储在数组中的对象的类型。 MapCollection等也是如此。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM