0

我编写了程序来创建文件并使用 jframe 添加字段,但它无法正常工作,请帮助我,谢谢!

读取文件获取字段全名和客户编号,并使用 jframe 再添加一个字段

全名和客户编号工作正常,但 jframe 无法正常工作.. 所以请帮助我......

实际上我也需要垂直向下滚动,请帮助我..

假设文件中有6条记录..

我需要放入jframe:

00240000844928953504
00240000844928953505
00240000844928953506
00240000844928953507
00240000844928953508
00240000844928953509

文件输出不正确实际上它把所有 6 个扫描仪线放在所有行文件中..

custID,fullname, 00240000844928953504
00240000844928953505
00240000844928953506
00240000844928953507
00240000844928953508
00240000844928953509

custID,fullname, 00240000844928953504
00240000844928953505
00240000844928953506
00240000844928953507
00240000844928953508
00240000844928953509

像这样!!:(

应该是这样的!!

custID,fullname,00240000844928953504
custID,fullname,00240000844928953505
....

这是我的代码!!

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;


public class scanner {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub



        final JFrame frame = new JFrame("Scan Here: ");
        JPanel panel = new JPanel();

        final JTextArea text = new JTextArea(20, 40);
        JButton button = new JButton("Enter");

        frame.add(panel);
        panel.add(text);
        panel.add(button);

        button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {


         BufferedReader br = null;
         BufferedWriter lbwp = null;


         String scanner = (text.getText());
         System.out.println(scanner);


         try {


             File folderall = new File("FilesIn");
             File[] BFFileall = folderall.listFiles();

             for (File file : BFFileall) {

                 String str = file.getName();

                 String reprintbwletterbwpca = ("FileOut" + "\\" + str);
                 lbwp = new BufferedWriter(new FileWriter(reprintbwletterbwpca));
                 lbwp.write("\"CUSTID\",\"FullName\",\"ONECODE\"," + "\n");


                 br = new BufferedReader(new FileReader(file));

                 String line;
                 line = br.readLine();

                 while ((line = br.readLine()) != null) {
                    String[] actionID = line.split("\\\",\"");

                    String custnumber = actionID[3];
                    String fullname = actionID[18];
                    String add1 = actionID[19];


                    lbwp.write("\"" + custnumber +  "\",\"" + fullname+ "\"," + "\"" + scanner + "\"," + "\n");

                 }
                 lbwp.close();


             }

         } catch(Exception e1) {
                e1.printStackTrace();
            }


         frame.dispose();
            }});
        frame.setSize(500, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    }

}

请帮我!!

4

1 回答 1

1

这里有两个列表:

  • 来自您正在阅读的文件的条目列表,每个条目都在“行”字符串中读取。
  • 您在 JTextArea 中输入的值列表,以“\n”分隔。

首先,您可能需要检查两个列表中是否有相同数量的条目。您还需要使用“\n”作为分隔符来拆分字符串扫描器。

当您逐行阅读时,您还可以使用以下代码,它不会检查条目数是否相等:

int index = 0;
String[] ids = scanner.split("\n");
while ((line = br.readLine()) != null) {
    String[] actionID = line.split("\\\",\"");

    String custnumber = actionID[3];
    String fullname = actionID[18];
    String add1 = actionID[19];

    if (ids.length > index) {
         lbwp.write("\"" + custnumber +  "\",\"" + fullname+ "\"," + "\"" + ids[index] + "\"," + "\n");
    } else {
        lbwp.write("\"" + custnumber +  "\",\"" + fullname+ "\"\n");
    }
    index++;
}

编辑:替换条件

于 2013-09-11T15:31:48.080 回答