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();
}

または、方法を使用して位置を見つけることができますindexOfline.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 に答える