选择要放入电子表格的多个文件

时间:2013-08-15 02:11:07

标签: java csv tokenize bufferedreader jfilechooser

我搜索了互联网以搜索所选目录,并使用包含的.txt文件转换为.csv以处理电子表格中的数据。这个程序在所有的.txt和我都认为我会与你分享我的所有发现时都是有效的。唯一要做的就是在第一个字母中仅限制包含LOG的.txt文件,这对我来说是一个完整的项目。

代码如下:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class LogReader
{
    /**
     *
     */
    public static void main(String[] args)
            throws IOException
    {

        // Creates a GUI based chooser for the user to select the directory to
        // evaluate.
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File("."));
        chooser.setDialogTitle("Folder Chooser");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        int returnVal = chooser.showOpenDialog(chooser);

        if(returnVal == JFileChooser.APPROVE_OPTION)
        {
            JOptionPane.showMessageDialog(null, "The current directory is:\n"
                    + chooser.getSelectedFile().getAbsolutePath());

            String sourceFolder = chooser.getSelectedFile().getAbsolutePath();
            File folder = new File(sourceFolder);
            String temp1;
            String temp2 = "\\";
            String temp3;
            FileWriter output = new FileWriter("Output Log.csv");
            BufferedReader reader;

            // create list of files within a certain folder.
            String[] files = folder.list();

            for(int i = 0; i < files.length; i++)
            {
                if(files[i].lastIndexOf('.') > 0)
                {
                    int lastIndex = files[i].lastIndexOf('.');
                    String str = files[i].substring(lastIndex);
                    if(str.equals(".txt"))
                    {
                        // Variables to concatenate the complete file directory
                        // pointer.
                        temp3 = files[i];
                        temp1 = sourceFolder.concat(temp2);
                        temp1 = temp1.concat(temp3);

                        // Use the BufferedReader with the new file pointer and
                        // store it
                        // in line.
                        reader = new BufferedReader(new FileReader(temp1));
                        String line = reader.readLine();
                        while(line != null)
                        {
                            StringTokenizer tokens = new StringTokenizer(line,
                                    " ");

                            while(tokens.hasMoreTokens())
                            {
                                String word = tokens.nextToken();
                                output.append(word + ',');
                            }
                            output.append('\n');

                            // read next line
                            line = reader.readLine();
                        }
                        output.append('\n');
                    }
                }
            }

            output.flush();
            output.close();

            // If the user selected cancel, then let them know.
        }
        else
        {
            JOptionPane.showMessageDialog(null, "No selection");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我只需要为此添加一个布尔值来比较具有特定要求的字符串。