0

我的文字这个

sirket;sehir;ilce;unvan;numara
AKSA SAT VE PAAMA ANM RKET KADY BES;STANBUL;KADIKY;A.S.;1230488;
KER BV SANAY;STANBUL;ZEYTNBURNU;A:S.;308133;
C.E.S.COSKUN MH.N.ELK.DAY.TK.MAL.VE GIDA TC.LTD.T.;HATAY;SKENDERUN;LTD. T.;14947;
ANLU GAZ TCARET VE SANY ANNM RKET KIRIKKALE UBES;KIRIKKALE;KIRIKKALE;A.S.;12519;
BVR GIDA MARİ NAAT TURZM SANAY VE TCARET LTD.T.;STANBUL;LO;LTD:STİ.;239;
ASA AKRLK KMYA SANAY (YALOVA UBES);YALOVA;FTLKKY;A.S.;551484;
WNCOR NXDORF LGSAYR SSTEER A.S.-ESNLER SUES;STANBUL;ESENLER;-;1027;
DESA DER SAN. VE TC.;STANBUL;BAKIRKY;A.S.;1480;
SNTM BLŞM TEKJLER SAN. VE TC;DYARBAKIR;YENEHR;A.S.;1439;

我要变化 ”。” 字符而不是“:”在包含 AS ,LTD.T.,LTD:STİ 的 unvan 列中。等等。当java读取文本时我删除第一行。我给第一行不稳定。我想在改变之前写改变之后但是我的代码有问题它不能写文本,因为我认为sirket列的空间。我怎么写。

我想要这个输出

KER BV SANAY;STANBUL;ZEYTNBURNU;A:S.;3083;
KER BV SANAY;STANBUL;ZEYTNBURNU;A.S.;308133;
BULVAR GIDA MAMLLERİ NAAT TURZM SANAY VE TCARET LTD.T.;STANBUL;LO;LTD:STİ.;2149;
BULVAR GIDA MAMLLERİ NAAT TURZM SANAY VE TCARET LTD.T.;STANBUL;LO;LTD.STİ.;239;

我的代码是

public class MaliyeBir {

    private static String inputPath = "C:\\deneme\\Maliye.csv";
    private static String outputPath = "C:\\yazdirilan\\YazdirilanMaliye.csv";
    private static BufferedReader bufferedReader;
    private static PrintWriter printWriter;

    public static void main(String[] args) {
       try {
           FileInputStream inputStream = new FileInputStream(inputPath);
           Reader reader = new InputStreamReader(inputStream, "ISO-8859-9");
           bufferedReader = new BufferedReader(reader);

           FileOutputStream outputStream = new FileOutputStream(outputPath);
           Writer writer = new OutputStreamWriter(outputStream, "ISO-8859-9");
           printWriter = new PrintWriter(writer);

           String line;

           while ((line = bufferedReader.readLine()) != null) {
               EndeksDegeri eD;
                eD = lineToClass(line);
                if (shouldOutput(eD)) {
                       printWriter.append(classToLine(eD, true));
                       printWriter.append(classToLine(eD, false));       
                   }               
           }

       } catch (IOException e) {
           e.printStackTrace();
       }
       finally {
           try {
               bufferedReader.close();
               printWriter.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }

    }

    private static boolean shouldOutput(EndeksDegeri eD) {
       if (!eD.tip.contains(":")) {
           return false;
       }

       return true;
    }

    private static String classToLine(EndeksDegeri eD, boolean original) {
       if (!original) {
           eD.tip = eD.tip.replace(":", ".");
       }

       return eD.sirket.concat(";")
               .concat(eD.sehir).concat(";")
               .concat(eD.ilce).concat(";")
               .concat(eD.tip).concat(";")
               .concat(String.valueOf(eD.numara)
                .concat("\r\n")); 
    }

    private static EndeksDegeri lineToClass(String line) {
       String[] element = line.split(";");

       EndeksDegeri endeksDegeri = new EndeksDegeri();
       endeksDegeri.sirket = element[0];
       endeksDegeri.sehir = element[1];
       endeksDegeri.ilce = element[2];
       endeksDegeri.tip = element[3];
       endeksDegeri.numara = Integer.valueOf(element[4]);

       return endeksDegeri;
    }
4

2 回答 2

1

如果这是一个 CSV 文件,我可以从csv代码中对文件扩展名的引用推断出,那么最好使用 CSV 解析库,例如opencsv,它将为您完成解析文件的所有艰苦工作. 然后,您可以操作 CSV 文件对象并将结果保存到文件中。

于 2013-08-02T07:43:27.233 回答
0

如果您确定没有其他列包含 delimiter ;,您可以使用 Javasplit函数将文本拆分为列,然后使用replace函数将冒号替换为点,如下所示:

while ( (line = bufferedReader.readLine()) != null){
    String[] elements = line.split(";");
    if(elements[3].contains(":")){ // If the value in column "ulvan" contains ":"
        printWriter.append(line); // Print the original
        for(int i=0; i<elements.length; i++){
            if(i>0){
                printWriter.append(";"); // Print the delimiter
            }
            if(i==3){ // For column index 3, which is "ulvan"
                printWriter.append(elements[i].replace(":",".")); // Replace ':' with '.'
            } else { // For other columns, just print
                printWriter.append(elements[i]);
            }
        }
    }
}

为简单起见,我在 main 函数中编写了所有内容,您可以根据需要对其进行模块化。

于 2013-08-02T07:49:43.000 回答