把list集合中的各元素按指定的字符分隔拼接成一个字符串操作

public static void main(String[] args) {
        List<String> lists=new  ArrayList<String>();
        BufferedReader br = null;
        String path="D:\\test\\src\\001.txt";
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(
                    new File(path)), "GBK"));
            String line = null;
            while ((line = br.readLine()) != null) {
                lists.add(line);
            }

            //各元素按“---”分隔开拼接成一个字符串
            String combiner=StringUtils.join(lists, "---");
            System.out.println(combiner);
            br.close();
        } catch (Exception e) {
            System.err.println("errors :" + e);
        }
    }

注意StringUtils用的包为import org.apache.commons.lang.StringUtils;

maven中央仓库可以下载到。

猜你喜欢

转载自my.oschina.net/u/3197158/blog/1626402