Java 常用工具方法

1.java获取到两个list中的不相同的项

public class Test {
    public static void main(String args[]){
        List<String> list1 = new ArrayList<>();
        list1.add("a");
        list1.add("b");
        list1.add("c");
        List<String> list2 = new ArrayList<>();
        list2.add("b");
        list2.add("c");
        list2.add("d");

        for(String s1 : list1){
            if(!list2.contains(s1)){
                System.out.println("list2不包括:" + s1);
            }
        }
        for(String s2 : list2){
            if(!list1.contains(s2)){
                System.out.println("list1不包括:" + s2);
            }
        }
    }
}

输出:

list2不包括:a
list1不包括:d

2.去除文件中的空行:

public class QuKongHang {
    public static void main(String[] args) throws Exception {
        File f1 = new File("E:\\1.txt");// 打开文件
        FileInputStream in = new FileInputStream(f1);
        BufferedReader read = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        String line = "";
        while ((line = read.readLine()) != null) {
            if (!line.equals("")) {
                System.out.println(line);
            }
        }
        read.close();
    }
}

3.将整个文件中的某个字符替换成另一个:

public class 文件字符转换 {
    public static void main(String[] args) {
        try {
            BufferedReader bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\\1.txt"))));//数据流读取文件
            StringBuffer strBuffer = new StringBuffer();
            for (String temp = null; (temp = bufReader.readLine()) != null; temp = null) {
                if ((temp.indexOf("for") == -1) && (temp.indexOf("if") == -1)) {
                    if (temp.indexOf("《") != -1) { //判断当前行是否存在想要替换掉的字符 -1表示存在
                        temp = temp.replace("《", "<");
                    }
                    if (temp.indexOf("》") != -1) { //判断当前行是否存在想要替换掉的字符 -1表示存在
                        temp = temp.replace("》", ">");
                    }
                }
                strBuffer.append(temp);
                strBuffer.append(System.getProperty("line.separator"));//行与行之间的分割
            }
            bufReader.close();
            PrintWriter printWriter = new PrintWriter("E:\\1.txt");//替换后输出的文件位置
            printWriter.write(strBuffer.toString().toCharArray());
            printWriter.flush();
            printWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3.List去重

public class Test {
    public static void main(String args[]) {
        List<String> oldList = new ArrayList<>();
        oldList.add("a");
        oldList.add("b");
        oldList.add("c");
        oldList.add("b");
        List<String> newList = new ArrayList<>();
        for (String cd : oldList) {
            System.out.print(cd + " ");
            if (!newList.contains(cd)) {
                newList.add(cd);
            }
        }
        System.out.println();
        for (String cd : newList){
            System.out.print(cd + " ");
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值