网站页面设计策划书音乐接单推广app平台
文章目录
- 一、 需求概述
- 二、Java实现
- 1、添加依赖
- 2、源码实现
- 三、运行结果
一、 需求概述
指定文件目录,搜索某类型文本文件,随机选择几个做内容交换。
思路:
将原始数据看做内齿轮,复制的内容数据看做外齿轮,将外部齿轮顺时针挪一步后写入,便实现了数据的腾挪!
二、Java实现
1、添加依赖
pom.xml
<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>2.0.16</version>
</dependency>
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.5</version>
</dependency>
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.15.0</version>
</dependency>
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version><scope>provided</scope>
</dependency>
<dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-engine</artifactId><version>5.5.2</version>
</dependency>
2、源码实现
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;import lombok.extern.slf4j.Slf4j;/*** 随机选择文件,向后写入文件内容*/
@Slf4j
public class FileExchange
{static String path = System.getenv().get("USERPROFILE") + "\\Pictures\\";File directory = new File(path);/*** 创建测试文件*/@BeforeAllpublic static void init(){IntStream.rangeClosed(0, 10).forEach(i -> {try{File file = new File(path + i + ".txt");FileUtils.writeStringToFile(file, Objects.toString(i), StandardCharsets.UTF_8);}catch (IOException e){log.error(e.getMessage(), e);}});}@Testpublic void test(){List<File> files = FileUtils.listFiles(directory, new String[] {"txt"}, true).stream().collect(Collectors.toList());// 随机选择多个List<File> chooseFiles = new Random().ints(5, 0, files.size()).mapToObj(index -> files.get(index)).distinct().peek(f -> log.info("{}", f.getAbsolutePath())) // 打印.collect(Collectors.toList());int length = chooseFiles.size();if (length > 1){List<String> texts = chooseFiles.stream().map(file -> readText(file)).collect(Collectors.toList());if (texts.stream().allMatch(text -> StringUtils.isNotBlank(text))){AtomicInteger index = new AtomicInteger();texts.forEach(text -> {try{File dest = chooseFiles.get(index.incrementAndGet() % length);FileUtils.writeStringToFile(dest, text, StandardCharsets.UTF_8);log.info("{} ===> {}", text, dest.getAbsolutePath());}catch (IOException e){log.error(e.getMessage(), e);}});}}}/*** 读取文件文本内容* * @param file* @return*/private String readText(File file){try{return FileUtils.readFileToString(file, StandardCharsets.UTF_8);}catch (IOException e){log.error(e.getMessage(), e);return null;}}
}
三、运行结果
2025-03-04 18:44:56.204 INFO 27280 --- [main] c.f.f.FileExchange : C:\Users\DELL\Pictures\6.txt
2025-03-04 18:44:56.206 INFO 27280 --- [main] c.f.f.FileExchange : C:\Users\DELL\Pictures\3.txt
2025-03-04 18:44:56.206 INFO 27280 --- [main] c.f.f.FileExchange : C:\Users\DELL\Pictures\8.txt
2025-03-04 18:44:56.206 INFO 27280 --- [main] c.f.f.FileExchange : C:\Users\DELL\Pictures\9.txt
2025-03-04 18:44:56.206 INFO 27280 --- [main] c.f.f.FileExchange : C:\Users\DELL\Pictures\5.txt
2025-03-04 18:44:56.213 INFO 27280 --- [main] c.f.f.FileExchange : 6 ===> C:\Users\DELL\Pictures\3.txt
2025-03-04 18:44:56.214 INFO 27280 --- [main] c.f.f.FileExchange : 3 ===> C:\Users\DELL\Pictures\8.txt
2025-03-04 18:44:56.214 INFO 27280 --- [main] c.f.f.FileExchange : 8 ===> C:\Users\DELL\Pictures\9.txt
2025-03-04 18:44:56.215 INFO 27280 --- [main] c.f.f.FileExchange : 9 ===> C:\Users\DELL\Pictures\5.txt
2025-03-04 18:44:56.215 INFO 27280 --- [main] c.f.f.FileExchange : 5 ===> C:\Users\DELL\Pictures\6.txt