通过 flatpack 读取 java 中的未分隔文本文件

Reading Un-delimited text file in java via flatpack

我想从 java 中的文本文件中读取数据,但文本文件不包含任何分隔符,如 space 或某些文本后的逗号。有人告诉我可以通过 flatpack 实现。

那么我如何读取文本并将其解析为定界并存储它们。

例如文本文件数据

"Prod Name" "City" "Price" "zipcode" "Date"

samsungA London 65001402110/07/2018  
samsungA California 35001202122/08/2018  
samsungA Delhi 44001202112/08/2018

我想存储:作为:

Name in string  
City in string  
Price in int  
zipcode in int  
date as date

对如何实现这一点有什么看法吗?

您可以使用一个简单的文件来做到这一点 reader。您的文件以空格分隔;根据您的示例,每行以换行符结尾。

因此,你只需要做一些算术计算索引,因为你有价格,post每行第三部分的代码和日期信息。

public static void main(String...args) throws IOException {
    final File file = new File("/home/william/test.txt");
    final String delimiter = " ";
    final int dateStrLen = 10;
    final int postCodeLen = 6;

    BufferedReader br = new BufferedReader(new FileReader(file));
    String tmp;
    while ((tmp = br.readLine()) != null) {
        String[] values = tmp.split(delimiter);

        String name = values[0];
        String city = values[1];
        int dateStartPos = values[2].length() - dateStrLen;
        int postCodeStartPos = dateStartPos - postCodeLen;

        String date = values[2].substring(dateStartPos);
        String postCode = values[2].substring(postCodeStartPos, dateStartPos);
        String price = values[2].substring(0, postCodeStartPos);
        // do something with the data
        // you could store it with a dto or in arrays, one for each "column"
        System.out.println(String.format("name: %s; city: %s; price: %s; post-code: %s; date: %s", name, city, price, postCode, date));
    }
}

我觉得用不用flatpack不是问题。 如果文件不包含分隔符,则应将 table 视为由数据列构建的文件,并使用字符位置定义来读取它。

然后你应该说在文件的开头你有位置 0 然后下一个字符是位置 1 然后是 2 ... 等等。

那么所有数据在 0 到 7 个字符之间的行都是 "Prod Name" 并且将 return samsungA.

从字符9到18(假设18是最大位置)你应该读取"City"的记录。

所以前提是知道每个数据列有多少个字符宽。 例如,第 1 行有 "London",但随后是 "California",您可以使用更宽的名称。所以你需要知道或者你需要找到结束每个数据列数据的最大位置。

而且你可以在没有扁平包装的情况下做到这一点。

    Well you can use parser, and xml schema to define the length of the required variables that way one can extract the required varaibles. But yes, those variables will have predefined length.
    String data= "samsungA500";
    String schema = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + 
                    "<!-- DTD can be pulled from the Jar or over the web -->\r\n" + 
                    "<!DOCTYPE PZMAP SYSTEM  \"flatpack.dtd\" >\r\n" + 
                    "<!--<!DOCTYPE PZMAP SYSTEM \"http://flatpack.sourceforge.net/flatpack.dtd\"> -->\r\n" + 
                    "<PZMAP>\r\n" + 
                    "   <COLUMN name=\"std_name\" length=\"9\" />\r\n" + 
                    "   <COLUMN name=\"std_price\" length=\"3\" />\r\n" +  
                    "</PZMAP>";

InputStream mapping = new ByteArrayInputStream(schema.getBytes());
        InputStream dataStream = new ByteArrayInputStream(data.getBytes());    
Parser pzparser = DefaultParserFactory.getInstance().newFixedLengthParser(mapping, dataStream);
            DataSet ds = pzparser.parse();
while (ds.next()) {
                System.out.println(ds.getString("std_name"));
                System.out.println(ds.getInt("std_price"));
                System.out.println(ds.getString("std_name"));
            }