把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);
            }apache

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

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

maven中央倉庫能夠下載到。字符串

相關文章
相關標籤/搜索