JAva中新行中字符串的连接

时间:2017-10-18 02:48:28

标签: java string concatenation

当满足条件时,我试图在新行中连接字符串。这是我的意见:

Concept
soft top cove
tonneau cove
interior persennin
Concept
Innen
Innenraum
Platz im Inneren

我想做的就是在字符串概念之后连接所有字符串并获得以下输出:

lemma, surface
soft top cove, tonneau cove|interior persennin
Innen, Innenraum|Platz im Inneren

我知道如果一个字符串值是相等的概念我想转到另一行并在逗号之前写下一行的字符串,而不是由" |"分隔的其他行的字符串。例如软顶海湾,tonneau海湾|室内设施 到目前为止这是我的代码。欢迎任何建议! 谢谢你:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class Converter {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BufferedReader inputcsv = null;
             List <String> zeilencsv = new ArrayList<String>();
        try {
            inputcsv = Files.newBufferedReader(Paths.get("ErsteDatei.csv"));
            String content;
            while ((content = inputcsv.readLine()) != null) {
                zeilencsv.add(content);

                System.out.println(content);
            }
            File outputcsv = new File("TwoColumnsResult.csv");
            //creates new file
            outputcsv.createNewFile();
            FileWriter csvFilewriter = new FileWriter(outputcsv);
            //arraylist loop
            int counter_a=0;
            int counter = 1;
            for (String zeile:zeilencsv){
                String concept = "Concept";

                //check string value =concept?
                        if(zeile.toString().equals(concept)){

                            zeile="lemma,surface";
                            for(String zeile2:zeilencsv){
                                //here I don't know how to say give me the next line, write it as a word , put comma and than concatenate with a |
                            }



                        }
                        else {

                        counter++;

}
                csvFilewriter.write(zeile+"\n");
                counter++;
            }
            //write
            csvFilewriter.flush();
            //closes the file
            csvFilewriter.close();


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

1 个答案:

答案 0 :(得分:0)

所以如果我理解正确你想要在找到“Concept”后将下一行保存为文本,连接一个逗号,然后保存下一行并连接一个|然后保存下一个?

你正在使用的代码有点奇怪,但我会做的是下一个:

zeile="lemma,surface";

// I'll put a counter to distinguish where to put "," and "|"
int counter = 0;

// Then I need a string variable to save the line
String line = "";

for(String zeile2:zeilencsv){
    if (count == 0){ //First iteration
        line = zeile2.toString();
    }
    if(count == 1){ . //Second iteration add the comma
        line = line + "," + zeile2.toString();
    }
    if(count == 2){ . //Second iteration add the pipe
        line = line + "|" + zeile2.toString();
    }
    count++;
}

我希望这就是你要找的东西。如果您想优化代码,请随时给我发消息,我们可以一起工作。