0

嗨,伙计们,我正在编写一个以这种格式读取文本文件的代码:

City             |First Name| Second Name|Last Name|

我目前的输出是:

Column 1 is 17--------City
Column 2 is 10--------First Name
Column 3 is 12--------Second Name
Column 4 is 9---------Last Name

我还需要文本文件中每个字段的开始位置,例如:

Column 1 is 17--------City : Position 1
Column 2 is 10--------First Name: Position 18
Column 3 is 12--------Second Name: Position 31
Column 4 is 9---------Last Name: Position 44

这是我目前拥有的代码。有没有办法实现这个?

 package stanley.column.reader;

 import java.io.*;

 public class StanleyColumnReader {

    public static void main(String[] args) throws IOException {
        System.out.println("Developed By Stanley Mungai");       
        File f = new File("C:/File/");
        if (!f.exists()) {
            f.createNewFile();
        } else {
            f.delete();
        }
        String [] files = f.list();
        for (int j = 0; j < files.length; j++){
            FileInputStream fs = new FileInputStream("C:/File/" + files[j]);
            BufferedReader br = new BufferedReader(new InputStreamReader(fs));
            String result = "_result";
            BufferedWriter is = new BufferedWriter(new FileWriter("C:/File/" + files[j] + result + ".txt"));
            for (int i = 0; i < 0; i++) {
                br.readLine();
            }

            String line = br.readLine();
            String[] split = line.split("|");
            for (int i = 0; i < split.length; i++) {
                int k = i + 1;
                System.out.println("Calculating the size of field " + k );
                is.write("Column " + k + " is " + split[i].length());
                is.flush();
                is.newLine();
            }
        }
        System.out.println("Success");
        System.out.println("Output Saved to C:/File");
    }
}
4

3 回答 3

2

您可以使用更高级的正则表达式组匹配来做到这一点并获取组开始索引。但考虑到这个问题,可能是矫枉过正和过于先进。

但是,在您的情况下,一种可能有效的快速简单方法就是indexOf在线使用。那就是将您的输出更改为包括:

" Position "+(line.indexOf(split[i])+1)

只要姓氏、名字和城市不在同一行重复...

顺便说一句,你几乎不需要在每一行上刷新,我建议将它移到循环之外。

正则表达式解决方案:

//first declare the pattern once in the class
static final Pattern pattern = Pattern.compile("\\s*(.*?)\\s*\\|");
...
//instead of the split loop:
String line = "City             |First Name| Second Name|Last Name| Foo |Bar |"; //br.readLine();
Matcher matcher = pattern.matcher(line);
int column = 1;
while (matcher.find(column == 1 ? 0 : matcher.end())) {
    String match = matcher.group(1);
    System.out.println("Column " + column + " is " + match.length() + "---" + match + ": Position " + (matcher.start() + 1));
    column++;
}

可能,根据您想要的确切位置,您可能希望更改(matcher.start()+1)(matcher.start(1)+1)

于 2012-05-31T07:04:41.603 回答
2

这是一个assignment吗?请正确标记。

您还没有说分隔符是否"|"也在数据中,但是看到您的代码,我假设它是。

我不明白的是你提到的第 3 列的位置是 31,第 4 列是 44?第 3 列应为 10+17+1 =28,第 4 列应为 10+17+12+1=40。如果我弄错了,您也需要发布原始数据。

String[] split = line.split("|");
int pos=1; //initial position
for (int i = 0; i < split.length; i++) {
    System.out.println("Calculating the size of field " + (i+1));
    is.write("Column " + (i+1) + " is " + pos+" : Position "+pos);
    pos=pos+split[i].length+1; //starting position for next column data
    is.flush();
    is.newLine();
}

或者您可以使用indexOf方法找到位置:line.indexOf(split[i])+1

于 2012-05-31T07:21:21.810 回答
1

如果我明白你需要什么。也许您可以使用 indexOf 方法。这给你带来了第一个巧合。找到这个后,将管道更改为不同的内容,并在下一次迭代中再次调用 indexOf 管道。

String line = br.readLine();
for (int i = 0; i < split.length; i++) {
        System.out.println("Calculating the position " + line.indexOf("|") );
        line[line.indexOf("|")] = ",";
}
于 2012-05-31T07:06:14.900 回答