0

我的文字是这样的

SEHiR;iL;iLCE;Tip;22356
S SI n;ISTA;ANK;A:S;22356
K K n;IS:TA;BB;B:S;22356
A A b;IS.TA;CC;DK;22356
G S b;ISTA;DD;O:P;22356

我想更改 TIP 列。我想把“。” 而不是“:”仅用于包含 A:S、B:S 等的Tıp 列。我想在更改之前更改为 csv 之后写一行。我怎样才能做到这一点?我写了一些东西,但它有问题

  1. if(eD.tip.contains(":"))部分,因为它不会继续到hS.Add(eD)
  2. endeks.put("", hS); 我不想使用“”字符串。
  3. 我不必使用 HasMap 我无法写出我想要的输出..

我期望这个输出

    S SI n;ISTA;ANK;A:S;22356
    S SI n;ISTA;ANK;A.S;22356
    K K n;IS:TA;BB;B:S;22356
    K K n;IS:TA;BB;B.S;22356
    G S b;ISTA;DD;O:P;22356
    G S b;ISTA;DD;O.P;22356




public class MaliyeVknmDegil {


       static class EndeksDegeri {
             String sirket ;
             String sehir;
             String ilce;
             String tip;
             int numara;
       }



       static HashMap<String,HashSet<EndeksDegeri>> endeks = new HashMap<String, HashSet<EndeksDegeri>>();
       static PrintWriter pW;
       static EndeksDegeri eD = new EndeksDegeri();
       static String satır;
       private static PrintWriter pW2;



          public static void main(String[] args) {

                 FileInputStream fIS;
                 FileOutputStream fOS;
                 try {
                        fIS = new FileInputStream("C:\\deneme\\DENEME.csv");
                        Reader r = new InputStreamReader(fIS, "UTF-8");
                        BufferedReader bR = new BufferedReader(r);
                        fOS = new FileOutputStream("c:\\yazdirilan\\deneme.csv");
                        Writer w = new OutputStreamWriter(fOS, "UTF-8");
                        pW2 = (new PrintWriter(w));

                        String satır;
                        String[] eleman;

                        while ((satır = bR.readLine()) != null) {     
                               eleman = satır.split(";");
                               if(satır.contains(":")){
                             pW2.write(satır);
                               }

                               HashSet<EndeksDegeri> hS = new HashSet<EndeksDegeri>();
                               for (int i = 0; i < eleman.length; i++) {
                               //     alteleman=eleman[i].split("       ");
                                      EndeksDegeri eD = new EndeksDegeri();
                                      eD.sirket = eleman[0];
                                      eD.sehir = eleman[1];
                                      eD.ilce = eleman[2];
                                      if(eD.tip.contains(":")){
                                          eD.tip.replaceAll(":", ".");
                                          eD.tip = eleman[3]; 
                                      } 
                                      eD.numara = Integer.parseInt(eleman[4]);
                                      hS.add(eD);                                                                                        
                               }                        
                               endeks.put("", hS);                         
                        }
                        bR.close();       

                        // yazdir   
                        HashSet<EndeksDegeri> hS;
                        for (String s : endeks.keySet()) {

                               hS = endeks.get(s);         

                        }
                 } catch (FileNotFoundException e) {
                        e.printStackTrace();
                 } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                 } catch (IOException e) {
                        e.printStackTrace();
                 }  catch (ArrayIndexOutOfBoundsException e) {

                        e.printStackTrace();
                 }

           }// main end 
    }// clas end
4

2 回答 2

0

您的代码将NullPointerException在该行中生成一个:if(eD.tip.contains(":")){

这是因为当EndeksDegeri创建一个新实例时,它的所有字段都null不能调用contains()空字符串。

检查下面的示例代码(它写入控制台,但它应该让你继续)

static class EndeksDegeri {
    String sirket;
    String sehir;
    String ilce;
    String tip;
    int numara;

    // I have added this method for convenience to write to the output 
    public String toString() {
        return sirket + ":" + sehir + ":" + ilce + ":" + tip + ":" + numara;
    }
}

while ((satir = bR.readLine()) != null) {
    eleman = satir.split(";");

    boolean found = false;
    EndeksDegeri eD = new EndeksDegeri();

    // first set all fields to not get exception
    eD.sirket = eleman[0];
    eD.sehir = eleman[1];
    eD.ilce = eleman[2];
    eD.tip = eleman[3];
    eD.numara = Integer.parseInt(eleman[4]);

    // check if the line contains ":"
    if (eD.tip.contains(":")) {
        // If yes, write the original line first
        System.out.println(eD.toString());

        // Change the record
        eD.tip = eD.tip.replaceAll(":", ".");
        found = true;
    }

    if (found) {
        // write the corrected line now
        System.out.println(eD.toString());
    }
}

// Will print only the lines with ":" and its correct version
SSI:ISTA:ANK:A:S:22356   
SSI:ISTA:ANK:A.S:22356
KK:IS:TA:BB:B:S:22356
KK:IS:TA:BB:B.S:22356
GS:ISTA:DD:O:P:22356
GS:ISTA:DD:O.P:22356
于 2013-08-01T18:16:45.563 回答
0
package stackoverflow;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;

public class TipChange {

private static String inputPath = "input.csv";
private static String outputPath = "output.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, "UTF-8");
        bufferedReader = new BufferedReader(reader);

        FileOutputStream outputStream = new FileOutputStream(outputPath);
        Writer writer = new OutputStreamWriter(outputStream, "UTF-8");
        printWriter = new PrintWriter(writer);

        String line;

        while ((line = bufferedReader.readLine()) != null) {
            EndeksDegeri 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;
}

private static class EndeksDegeri {
    String sirket ;
    String sehir;
    String ilce;
    String tip;
    int numara;
}
}

样本输入:

SSI;ISTA;ANK;A:S;22356
KK;IS:TA;BB;B:S;22356
AA;IS.TA;CC;DK;22356
GS;ISTA;DD;O:P;22356

生成的输出:

SSI;ISTA;ANK;A:S;22356
SSI;ISTA;ANK;A.S;22356
KK;IS:TA;BB;B:S;22356
KK;IS:TA;BB;B.S;22356
GS;ISTA;DD;O:P;22356
GS;ISTA;DD;O.P;22356
于 2013-08-01T18:03:06.140 回答