我的文字
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;
ı想要改变“。”在仅限unvan列的字符而不是“:”,其中包含A.S. ,LTD.T.,LTD:STI。当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;
}
答案 0 :(得分:1)
如果这是一个CSV文件,我可以从代码中对csv
文件扩展名的引用推断出来,那么你最好使用像opencsv这样的CSV解析库,将为您解析文件的所有艰苦工作。然后,您可以操作CSV文件对象并将结果保存到文件中。
答案 1 :(得分:0)
如果您确定没有其他列包含分隔符;
,则可以使用Java split
函数将列中的文本拆分,然后使用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函数中编写了所有内容,如果需要,可以将其模块化。