2013-07-24 57 views
2

編寫代碼打開Jframe,在那裏掃描條碼,讀取並進入正確的文件,搜索cust_number並讀取該行並寫入新文件。我得到兩個問題,請幫助我!謝謝!!Java代碼,搜索文件中的數字和讀取行

這裏是我的代碼:

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 ::閱讀該行寫在文件.. 但是,它在文件中只寫一行..我認爲它的替代,這就是爲什麼!請幫我,我不知道!先謝謝了!!

回答

1

readline讀取當前指針。一旦你找到正確的線,看起來你只是想打印出你已有的「線」的值,而不是讀下一行。

+0

謝謝你!它的工作現在!! :)它正在寫這行!我怎麼能在一個文件中寫出所有的行?請幫幫我!!它只寫一行文件..我想它取代,請幫助我!謝謝!! –

+0

你會想在第一個'for'循環之外創建你的BufferedWriter。現在,每次爲每行創建一個新的writer,這就是爲什麼你只能在文件中獲得一行。 – Choker

+0

非常感謝你!它工作!! :)再次感謝! –

相關問題