Java代码,在文件中搜索数字并读取行

标签 java

编写打开 Jframe 的代码,扫描那边的条形码,读取它并进入正确的文件,搜索 cust_number 并读取该行并写入新文件。我有两个问题请帮助我!!谢谢!!

这是我的代码:

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.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

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


public class searchfile2 {

    /**
     * @param args
     */
    
    public static void delFileFromDir(String dirPath) {
        File dir = new File(dirPath);
        if(dir.listFiles() == null)
            return;
        for(File file: dir.listFiles())
        {
            if(!file.isDirectory())
                file.delete();
        }
    }
    
    
    public static void main(String[] args) throws IOException  {
        // 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 bfAll = null;
                
                String scanner = (text.getText());
                //System.out.println(scanner);

                try {
                    for (String s : scanner.split("\n")) {
                        String[] actionID = s.split("\\|");
                        String cust_num  = actionID[0];
                        String date = actionID[1];
                        String type = actionID[2];
                        //System.out.println(cust_num + "     Type:     " + type);
                    
                        if(type.equals("BW")){
                            //System.out.println(type);
                            
                            File folderBW = new File("prod\\BW");
                            File[] BFFileBW = folderBW.listFiles();
                            
                            String reprintbw = ("out\\" + "BWreprintrecord" + ".txt");
                            bfAll = new BufferedWriter(new FileWriter(reprintbw));
                            
                            
                            for (File file1 : BFFileBW) {
                                String strbw = file1.getName();
                                //System.out.println(strbw);
                                
                                
                                if((date.subSequence(0, 2)).equals(strbw.subSequence(0, 2)) && (date.substring(2, 4)).equals(strbw.subSequence(3, 5)) && (date.subSequence(4, 6)).equals(strbw.subSequence(8, 10))){
                                    System.out.println("hdssdjsshdghjsdghjsdghjsdghjsdgjhsd               " + strbw);
                                    
                                    File foldertotalcountlettersdate = new File("prod\\BW\\" + strbw);
                                    File[] listOfFilestotalcountlettersdate = foldertotalcountlettersdate.listFiles();

                                    String totalcountlettersdate;
                                    
                                    try{
                                        for (int itotalcountdate = 0; itotalcountdate < listOfFilestotalcountlettersdate.length; itotalcountdate++) {
                                            if (listOfFilestotalcountlettersdate[itotalcountdate].isFile()) {
                                                totalcountlettersdate = listOfFilestotalcountlettersdate[itotalcountdate].getAbsolutePath();
                                                System.out.println("File Name: " + totalcountlettersdate);
                                                
                                                br = new BufferedReader(new FileReader(totalcountlettersdate));
                                                String line;
                                                line = br.readLine();
                                                bfAll.write(line);
                                                bfAll.newLine();
                                                line = br.readLine();
                                                bfAll.write(line);bfAll.newLine();
                                                line = br.readLine();
                                                bfAll.write(line);bfAll.newLine();
                                                line = br.readLine();
                                                bfAll.write(line);bfAll.newLine();
                                                line = br.readLine();
                                                bfAll.write(line);bfAll.newLine();
                                                line = br.readLine();
                                                bfAll.write(line);bfAll.newLine();
                                                line = br.readLine();
                                                bfAll.write(line);bfAll.newLine();
                                                
                                                while ((line = br.readLine()) != null) {
                                                
                                                    String[] actionIDprod = line.split("\\|");
                                                    String typeprod  = actionIDprod[3];
                                                    String typeprodname  = actionIDprod[4];
                                                    
                                                    if(typeprod.equals(cust_num)) {
                                                        line = br.readLine();
                                                        System.out.println(line);
                                                        System.out.println(cust_num + "-------" + typeprodname);
                                                    }
                                                }
                                                br.close();
                                                
                                            }
                                        }
                                    } catch(Exception e2) {
                                        e2.printStackTrace();
                                    }
                                    
                                }
                            }
                            
                            bfAll.newLine();
                            bfAll.flush();
                            bfAll.close();
                        }
                        
                    }
                
                } catch(Exception e1) {
                    e1.printStackTrace();
                }
                
                frame.dispose();
            }
            });
        frame.setSize(500, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}

问题是在文件中写入该行时..

bfAll.write(line);

它只写最后一行..我需要将所有行写在一个文件中,我认为它可以替换!!请帮我!!谢谢!!

并且在 Jframe 中!! 我扫描的是:

027421940|072213|BW|
600295885|072113|BW|
600253827|072113|BW|
600295333885|072113|LETTERS|

在文件中搜索所有 027421940 cust_number::读取该行写入文件.. 但是,它只在文件中写了一行。我认为它被替换了,这就是为什么!!

最佳答案

readline 前进当前指针。一旦找到正确的行,看起来您只想打印出您已有的“行”的值,而不是阅读下一行。

关于Java代码,在文件中搜索数字并读取行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17835081/

相关文章:

Java - 匿名类是否是静态的

java - 我不应该在 Java 中执行 `String s = new String("一个新字符串");`,即使有自动字符串实习?

java - 如何将多个 JasperPrint 对象组合成一个具有混合页面方向的报告

java - Java 中的公共(public)接口(interface)和已发布接口(interface)有什么区别?

java - Apache FOP。西里尔字符问题

java - 使用 StandardCharset 的 PrintWriter

java - driver.connect() 与 DriverManager.getConnection()

java - 移动二维数组中的元素

java - 改造 2.1.0 + Gson。序列化作为子类的@Body

java - 在 "BEGIN-OF-STATEMENT"之后找到 CallableSamentment 。预期的 token 可能包括 : "<space>"