读写文本并仅在一列中更改

时间:2013-08-01 17:12:04

标签: java file-io

我的文字 就像那样

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列。我想把“。”而不是“:”用于仅Tıp列,其中包括A:S,B:S等。我想在更改之前写行,在将更改为csv之后写。我怎样才能做到这一点?我写了一些东西但它有问题

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

        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
    

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您的代码将在行中生成NullPointerExceptionif(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