.txt文件内容,修改满足第一位是11的集合的最后一位 改为3
[[11,12,13,2], [21,22,23,1]]
代码块
package com.sangeng;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import com.alibaba.fastjson.JSON;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Test1 {
public static void main(String[] args) throws IOException {
//需要修改的元素集合
ArrayList<Long> longs = new ArrayList<>();
longs.add(11L);
longs.add(21L);
// boolean contains = longs.contains(11);
String fileName="./../../../2.txt";
String readUtf8Lines = FileUtil.readString(fileName, CharsetUtil.CHARSET_UTF_8);
//结果转为List集合
ArrayList<List<Long>> arrayList = JSON.parseObject(readUtf8Lines, ArrayList.class);
/**
* 根据条件修改集合中的某个属性,然后写回文件操作
*/
List<List<Long>> collect = arrayList.stream()
.map(list -> {
Object start_index = list.get(0);
String start_index_str = start_index.toString();
long start_index_str_long = Long.parseLong(start_index_str);
//判断条件
boolean contains1 = longs.contains(start_index_str_long);
if (contains1) {
//定位到需要修改的位置
list.set(3, 3L);//修改参数
}
return list;
})
.collect(Collectors.toList());
//写回文件操作
System.out.printf(String.valueOf(collect));
FileUtil.writeString(String.valueOf(collect),fileName, CharsetUtil.CHARSET_UTF_8 );
}
}
标签:index,java,String,util,start,import,txt,Steam,符合条件
From: https://www.cnblogs.com/chengqiang521/p/16967687.html