Java 读取和写入文本文件
2024-09-05 10:12:15
package test_java; import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List; public class ReadWrite {
public static void main(String [] args) {
String filePath = "input.txt";
String destFilePath = "output.txt";
readWrite(filePath, destFilePath);
} public static void readWrite(String filePath, String destFilePath) {
List<String> report = Arrays.asList("Prevotella_intermedia", "Enterobius_vermicularis", "Parvimonas_micra", "Protopolystoma_xenopodis");
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath)); // 读取文件
FileOutputStream fileOutputStream = new FileOutputStream(new File(destFilePath)); // 写入文件
String string;
while((string = bufferedReader.readLine()) != null) { // 按行读取
if (string.split("\t")[0].trim().equals("species")) {
fileOutputStream.write((string + "\n").getBytes()); // 将符合条件的行写入文件
}
if (report.contains(string.split("\t")[0].trim())) {
fileOutputStream.write((string + "\n").getBytes());
}
}
} catch (IOException e) {
e.printStackTrace();
} }
}
最新文章
- 一个virtualbox开机即aborted的问题解决
- JSF 与 HTML 标签的联系
- 深入理解C语言的函数调用过程
- puppet安装
- ecshop简单三部实现导航分类二级菜单
- In p = new Fred(), does the Fred memory “leak” if the Fred constructor throws an exception?
- Foundation与Core Foundation内存管理基本原则简述
- Nhibernate 一对一关系映射(主键映射)
- Liunx下Tomcat+MYSQL+Nginx配置
- thymelef 布局 fragment
- 怎么屏蔽F5键刷新功能
- struts开发步骤
- C#属性和字段
- Intellij idea 中修改java web代码 ,网页不同步
- C#添加背景音乐
- 分布式配置管理平台XXL-CONF
- java将一个List赋值给另一个List
- Luogu P4009 汽车加油行驶问题
- 使用js对WGS-84 ,GCJ-02与BD-09的坐标进行转换
- mysql分页优化方法
热门文章
- 在Mac 上搭建Linux虚拟机--MacOS &; VMware10 &; CentOS 7
- GitBook基本使用
- 【Python3爬虫】最新的12306爬虫
- .NET获取实例化对象的部分属性名称
- .net core - 配置管理 - json文件配置
- 完美转发(perfect forwarding)、universal reference、引用折叠(collasping)
- python 双层for循环,在第二层的for循环中的else中的continue,会退出到第一层for循环继续执行
- 2019 学霸君java面试笔试题 (含面试题解析)
- python--进程初识详解
- kubernetes集群内通过endpoint访问外部服务