Java Gui文本文件输出

时间:2014-05-28 09:47:32

标签: java swing user-interface io bufferedreader

try {       
            String hour = (String) comboBox.getSelectedItem();
            String filename =  fileName.getText();
            String date = ((JTextField)dateChooser.getDateEditor().getUiComponent()).getText();         
            String text = txtKeyword.getText();                           
            String newline = "\n";
            String directory = Directory.getText();
            File path = new File(directory);   
            File[] faFiles = path.listFiles();         
                    for(File file: faFiles){    
                        **if(file.getName().contains(filename + "-" + date + "[" + hour + "]") == true == true || file.getName().contains(filename + "-" + date) || file.getName().contains(filename)){** 
                    String line = null;                         
                    Reader reader = new InputStreamReader(new FileInputStream(file), "utf-8");           
                    BufferedReader br = new BufferedReader(reader);                   
                     while ((line = br.readLine()) != null) {                                                      
                    if(line.contains(text)){                 
                        jTextArea1.append(line + newline);    
                        btnClear.setEnabled(true); 
                         btnExport.setEnabled(true); 
                    }   

                    } 
                       br.close();
                     }          
                }               
              } 
        catch(Exception e){               
        }        

这是我的问题。我正在尝试使用输入和循环方法来搜索文件。上面的代码有效但我的问题是我试着找到2个不同的文本文件

1. billing-20140527[09].txt has 

 a)XGMS,2014-05-27 10:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.

 b)XGMS,2034-05-27 30:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.


2. billing-20140527[10].txt has

 a)XCGS,2014-05-27 10:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.

 b)HELO





**I try to find the number 1 in both text files, if lets say I input the text file name is
        billing, I can find the number 1 in both text file and output them:**

a) XGMS,2014-05-27 10:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.

b) XCGS,2014-05-27 10:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.



**However, if I specify the text file name: billing-20140527[09].txt and find the number 1 inside the text file, it will only output:

a) XGMS,2014-05-27 10:08:04,122,PLAYER_VERIFY,VERIFY to LBA,0x580000,0xC0000,253040.**

任何人都可以帮我吗?指导或帮助?

2 个答案:

答案 0 :(得分:0)

我会使用BufferedReader。因为它读了一整行。然后你可以用分隔符分割线(比如说空格"")。在你的情况下,我会写一个split方法,它接收一个String并搜索你想要的正则表达式。

答案 1 :(得分:0)

private void doSearch(File f2) throws IOException,
        fileHandler.FileException {
    File[] children = f2.listFiles();
    if (children != null && searching)
        for (int i = 0; i < children.length; i++) {
            if (g.isReady()) {
                g.setReady(false);
                if (!searching) {
                    g.setReady(true);
                    break;
                } else if (isDirectory(children[i])) {
                    g.getActualDirectoryInhalt().setText(children[i].getPath()
                            .substring(g.root.getText().length()));
                    counterDirectories++;
                    doSearch(children[i]);
                } else if (advancedSearch && !filterSpecified(children[i])) {
                    raiseCounterForDirectorySize(children[i]);
                    continue;
                } else if (checkFile(children[i])) {
                    counterFiles++;
                    searchThroughFile(children[i], this.regex);
                    raiseCounterForDirectorySize(children[i]);
                } else {
                    g.getTextAreaUnreachable().setText(
                            g.getTextAreaUnreachable().getText() + f2
                                    + "\n");
                    raiseCounterForDirectorySize(children[i]);
                }
            } else {
                doSearch(children[i]);
            }
            g.setReady(true);
        }
}

这是另一种方法:

    public static void searchThroughFile(File f2, String regex) throws IOException,
        fileHandler.FileException {
    try {
        InputStream is = new BufferedInputStream(new FileInputStream(f2));
        String mimeType = URLConnection.guessContentTypeFromStream(is);

        ArrayList<String> linesFromFile = fileHandler.FileReaderer
                .readFileIntoStringArrayList(f2);
        String line = null;
        if (f2.getAbsolutePath().contains(regex)) {
            g.getTextAreaAdvanced()
                    .setText(
                            g.getTextAreaAdvanced().getText()
                                    + f2.getPath() + "\n");
        }
        if (linesFromFile.size() != 0) {
            for (int i = 0; i < linesFromFile.size(); i++) {
                line = linesFromFile.get(i);
                Pattern MY_Pattern = Pattern.compile(regex);
                Matcher m = MY_Pattern.matcher(line);
                if (!searching) {
                    break;
                }
                MarkOne: if (!g.isReady()) {
                    break MarkOne;
                } else {
                    g.setReady(false);
                }
                while (m.find() && searching) {
                    counterFoundPattern++;
                    g.getFoundFilesInhalt().setText(counterFoundPattern + "");
                    if (mimeType != null) {
                        g.getTextAreaAdvanced().setText(
                                g.getTextAreaAdvanced().getText()
                                        + f2.getPath() + " " + m.group()
                                        + " " + mimeType + " " + i+1 + "\n");
                    } else {
                        g.getTextAreaAdvanced().setText(
                                g.getTextAreaAdvanced().getText()
                                        + f2.getPath() + " " + m.group()
                                        + " " + i+1 + "\n");
                    }
                }
                g.setReady(true);
            }
        }
    } catch (IOException e) {
        MarkOne: if (!g.isReady()) {
            break MarkOne;
        } else {
            g.setReady(false);
        }
        g.getTabpane().setForegroundAt(2, Color.RED);
        g.getTextAreaException().setText(
                g.getTextAreaException().getText() + e + "\n");
        g.setReady(true);
    } catch (OutOfMemoryError oute) {
        MarkOne: if (!g.isReady()) {
            break MarkOne;
        } else {
            g.setReady(false);
        }
        g.getTextAreaException().setText(
                g.getTextAreaException().getText() + "\n"
                        + "Fatal Error encured! The File will be skipped!"
                        + "\n" + f2.getAbsolutePath());
        g.getTabpane().setSelectedIndex(2);
        g.setReady(true);
        return;
    }
}