0

几个月来,我一直在尝试自学 Java。我厌倦了那些教你如何说“你好”和其他 IMO 的书。

这是我的问题,我想将“制表符分隔”文本文件读入特定字段。有些字段是 int,有些是 String,有些是 int[] 数组。(我很难相信阅读一个简单的文件有这么难!)

如何获取令牌变量,其中记录(行)中的第一个令牌是“int”,第二个是“String”,3 到 5 是单独的“int”,接下来的 21 个是在“int”数组中 [ ]?

在此先感谢您的帮助。

package viewOfGolfSystem;

import java.io.File;
import java.io.FileReader;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

class TextDataCheck {
    static public void main(String args[]) throws Exception {

    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "TXT and CSV files", "txt", "csv");
    chooser.setFileFilter(filter);
    int returnVal = chooser.showOpenDialog(chooser);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        chooser.addChoosableFileFilter(filter);
        chooser.showOpenDialog(null);
        File f = chooser.getSelectedFile();

        String filename = f.getAbsolutePath();
            // should return the filename(i.e.path)

        FileReader flr = new FileReader(filename);
        Scanner sInput = new Scanner(flr).useDelimiter("\t");

        while (sInput.hasNext()) {
        System.out.println(sInput.next());
            }
        sInput.close();

        } else {
            System.out
            .println("File chosen is not an approved option.  Try again\n");
        }
    }
}
4

2 回答 2

0

怎么样:

private static class Thing {

    private final int int1;
    private final String string2;
    private final int int3;
    private final int int4;
    private final int int5;
    private final int[] int21;

    public Thing(final String[] line) {
        int1 = Integer.parseInt(line[0]);
        string2 = line[1];
        int3 = Integer.parseInt(line[2]);
        int4 = Integer.parseInt(line[3]);
        int5 = Integer.parseInt(line[4]);
        int21 = new int[21];
        for (int i = 0; i < 21; ++i) {
            int21[i] = Integer.parseInt(line[i - 5]);
        }
    }
}

public static void main(String[] args) throws Exception {
    final BufferedReader reader = new BufferedReader(new FileReader("myFile.txt"));
    final List<Thing> myThings = new LinkedList<Thing>();
    try {
        String line;
        while ((line = reader.readLine()) != null) {
            myThings.add(new Thing(line.split("\t")));
        }
    } finally {
        reader.close();
    }
}

逐行读取文件,然后在\t.

创建一个接受 aString[]并将其解析为所需值的类。

显然这没有考虑分隔符和转义字符,为了做到这一点,您需要一个更复杂的解析方法。使用像OpenCSV这样的现有库可能会更容易。

于 2013-05-14T00:48:20.967 回答
0

根据您想走的路线,以及您需要的灵活性,您可以自己编写一些代码,此类代码的示例可以在数百个站点上找到,例如在这个博客上。他们提出的代码的核心是这样的:

    BufferedReader bReader = new BufferedReader(
            new FileReader(dataFileName));

    String line;

    /**
     * Looping the read block until all lines in the file are read.
     */
    while ((line = bReader.readLine()) != null) {

        /**
         * Splitting the content of tabbed separated line
         */
        String datavalue[] = line.split("\t");
        String value1 = datavalue[0];
        String value2 = datavalue[1];
        int value3 = Integer.parseInt(datavalue[2]);
        double value4 = Double.parseDouble(datavalue[3]);

        /**
         * Printing the value read from file to the console
         */
        System.out.println(value1 + "\t" + value2 + "\t" + value3 + "\t"
                + value4);
    }
    bReader.close();

应该很容易适应您的目的。

现在,完全不同的方法是使用现有的通用平面文件解析器,如JSAPar。该解决方案的美妙之处在于整个转换由配置文件驱动,因此如果明天您想更改文件格式,那么调整您的程序会更容易。当然,没有什么是免费的,学习这样的库需要一些时间,而且它会为您的解决方案增加额外的大小。

于 2013-05-14T00:40:12.643 回答