Java:逐行读、写文件、文件目录过滤的用法
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List; public class HelloWord {
public static void main(String[] args) throws IOException {
String filePath = "my File.txt";
File file = new File(filePath);
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file),"utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader); List<String> lineItems=new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
String[] items = line.split(","); String item= items[1];
if(item.indexOf("花园")!=-1){
lineItems.add(line);
}
} bufferedReader.close();
inputStreamReader.close(); for(String my : lineItems){
System.out.println(my);
}
}
}
- 写文件用法:
List<String> lines = new ArrayList<>();
String wfilePath="e:\\1.txt";
File wfile = new File(wfilePath);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(wfile), "utf-8");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
for (String eLine : lines) {
bufferedWriter.write(eLine);
bufferedWriter.newLine();
} bufferedWriter.close();
outputStreamWriter.close(); System.out.println("Complete...");
- 文件目录过滤:

实例:
package com.dx.hibernate5.test; import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class HelloWord {
public static void main(String[] args) throws IOException {
String filePath = "E:\\丽水";
String wfilePath = "E:\\丽水_New";
parserCountyVsCode(filePath); parseCityCounty(filePath, wfilePath); System.out.println("Complete...");
} private static void parserCountyVsCode(String filePath)
throws UnsupportedEncodingException, FileNotFoundException, IOException {
File file = new File(filePath);
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
// 缙云县:5782
// 云和县:5784
// 开发区:578B
// 龙泉市:5786
// 景宁县:5789
// 青田县:5783
// 景宁畲族自治县:5789
// 松阳县:5788
// 庆元县:5785
// 莲都区:5781
// 遂昌县:5787
List<String> countyList = new ArrayList<>();
countyList.add("5782");
countyList.add("5784");
countyList.add("578B");
countyList.add("5786");
countyList.add("5789");
countyList.add("5783");
countyList.add("5788");
countyList.add("5785");
countyList.add("5781");
countyList.add("5787"); Map<String, String> lineItems = new HashMap<String, String>();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
String[] items = line.split(","); String address = items[3];
String county = items[2]; if (address.indexOf("莲都区") != -1) {
lineItems.put("莲都区", county);
} else if (address.indexOf("开发区") != -1) {
lineItems.put("开发区", county);
} else if (address.indexOf("青田县") != -1) {
lineItems.put("青田县", county);
} else if (address.indexOf("缙云县") != -1) {
lineItems.put("缙云县", county);
} else if (address.indexOf("遂昌县") != -1) {
lineItems.put("遂昌县", county);
} else if (address.indexOf("松阳县") != -1) {
lineItems.put("松阳县", county);
} else if (address.indexOf("云和县") != -1) {
lineItems.put("云和县", county);
} else if (address.indexOf("庆元县") != -1) {
lineItems.put("庆元县", county);
} else if (address.indexOf("景宁县") != -1 || address.indexOf("景宁畲族自治县") != -1) {
lineItems.put("景宁县", county);
} else if (address.indexOf("龙泉市") != -1) {
lineItems.put("龙泉市", county);
} else {
Boolean hasContains = false;
for (String coun : countyList) {
if (coun.equals(county)) {
hasContains = true;
}
}
if (!hasContains) {
System.out.println(line);
}
}
} bufferedReader.close();
inputStreamReader.close(); for (Map.Entry<String, String> kvEntry : lineItems.entrySet()) {
System.out.println(kvEntry.getKey() + ":" + kvEntry.getValue());
}
} private static void parseCityCounty(String filePath, String wfilePath)
throws UnsupportedEncodingException, FileNotFoundException, IOException {
Map<String, String> idVsCountry = new HashMap<>();
idVsCountry.put("5782", "缙云县");
idVsCountry.put("5784", "云和县");
idVsCountry.put("578B", "开发区");
idVsCountry.put("5786", "龙泉市");
idVsCountry.put("5789", "景宁县");
idVsCountry.put("5783", "青田县");
idVsCountry.put("5788", "松阳县");
idVsCountry.put("5785", "庆元县");
idVsCountry.put("5781", "莲都区");
idVsCountry.put("5787", "遂昌县"); List<String> lines = new ArrayList<>();
lines.add("abc0");
lines.add("abc1");
lines.add("abc2");
File file = new File(filePath);
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
String[] items = line.split(","); String address = items[3];
String county = items[2];
String newLine = items[0] + ",丽水市," + idVsCountry.get(county) + "," + address;
// System.out.println(newLine);
lines.add(newLine);
} bufferedReader.close();
inputStreamReader.close(); File wfile = new File(wfilePath);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(wfile), "utf-8");
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
for (String eLine : lines) {
bufferedWriter.write(eLine);
bufferedWriter.newLine();
} bufferedWriter.close();
outputStreamWriter.close();
}
}
Java:逐行读、写文件、文件目录过滤的用法的更多相关文章
- 关于使用 Java 分片读\写文件
分片读取文件方法: /** * 分片读取文件块 * * @param path 文件路径 * @param position 角标 * @param blockSize 文件块大小 * @return ...
- java读/写文件
读取文件参考:https://blog.csdn.net/weixin_42129373/article/details/82154471 写入文件参考:https://blog.csdn.net/B ...
- Java基础之写文件——将素数写入文件中(PrimesToFile)
控制台程序,计算素数.创建文件路径.写文件. import static java.lang.Math.ceil; import static java.lang.Math.sqrt; import ...
- java中多种写文件方式的效率对比实验
一.实验背景 最近在考虑一个问题:“如果快速地向文件中写入数据”,java提供了多种文件写入的方式,效率上各有异同,基本上可以分为如下三大类:字节流输出.字符流输出.内存文件映射输出.前两种又可以分为 ...
- C++ 二进制文件 读 写文件
1 #include <iostream> 2 #include <string> 3 #include<fstream> 4 using namespace st ...
- read(),write() 读/写文件
read read()是一个系统调用函数.用来从一个文件中,读取指定长度的数据到 buf 中. 使用read()时需要包含的头文件: <unistd.h> 函数原型: ssize_t re ...
- 在Java中怎样逐行地写文件?
下边是写东西到一个文件里的Java代码. 执行后每一次,一个新的文件被创建,而且之前一个也将会被新的文件替代.这和给文件追加内容是不同的. public static void writeFile1( ...
- Java基础之写文件——从多个缓冲区写(GatheringWrite)
控制台程序,使用单个写操作将数据从多个缓冲区按顺序传输到文件,这称为集中写(GatheringWrite)操作.这个功能的优势是能够避免在将信息写入到文件中之前将信息复制到单个缓冲区中.从每个缓冲区写 ...
- java中IO写文件工具类
以下是一些依据经常使用java类进行组装的对文件进行操作的类,平时,我更喜欢使用Jodd.io中提供的一些对文件的操作类,里面的方法写的简单易懂. 当中jodd中提供的JavaUtil类中提供的方法足 ...
随机推荐
- 大数运算的算法设计和C++实现
1.背景 工作中遇到过需要进行极大数据的存储和运算的场景,当时使用Python解决了这个问题,在Python中,整数没有位数限制,使用起来很方便.但是当程序主体使用C/C++实现时,就比较麻烦.所以考 ...
- 设计模式 --> (10)享元模式
享元模式 运用共享技术有效地支持大量细粒度的对象. 享元对象能做到共享的关键是区分内蕴状态(Internal State)和外蕴状态(External State). 内蕴状态是存储在享元对象内部并且 ...
- Android,资料分享(2015 版)
Java 学习 我要再次强调,一定要有Java 基础(虽然现在使用其他语言也可以开发Android,但毕竟是很小众),也不要认为学习Java 两三周就可以不用管了,这会在以后的深入学习中暴露出问题,所 ...
- sqlite的limit使用
如果我要取11-20的Account表的数据,则为: Select * From Person Limit 9 Offset 10;表示从Person 表获取数据,跳过10行,取9行 .也可以这样 ...
- 关于redis数据库的简单思考
redis数据库中有以下几种数据类型: 字符串,哈希,列表,集合,有序集合 它们应用的场景如下: 字符串用法单一,用于存储一个key的值,用于一一对应的场合 列表作为数组来使用 对于哈希,特别适用于存 ...
- 结合jenkins在Linux服务器搭建测试环境
何时使用: 测试过程中我们需要持续构建一个软件项目,为避免重复的手动下载.解压操作,我们需要搭建一个能够自动构建的测试环境,当代码有更新时,测试人员只需点一下[构建]即可拉取最新的代码进行测试(也可设 ...
- Linux学习--线程概念
线程 我们知道 ,进程在各自独立的地址空间中运行,进程之间共享数据需要用mmap或者进程间通信机制,本节我们学习如何在一个进程的地址空间中执行多个线程.有些情况需要在一个进程中同时执行多个控制流程,这 ...
- verilog学习笔记(4)_有限状态机
有限状态机: 有限状态机是由寄存器组和组合逻辑构成的硬件时序电路: - 其状态(即由寄存器组的1和0的组合状态所构成的有限个状态)只能在同一时钟跳变沿的情况下才能从一个状态转向另一个状态: - 究竟转 ...
- 理解Python迭代对象、迭代器、生成器
作者:zhijun liu链接:https://zhuanlan.zhihu.com/p/24376869来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 本文源自RQ作 ...
- vmware ubuntu蓝屏
ctrl+alt+f4 sudo apt-get update sudo apt-get upgrade sudo apt-get install xserver-xorg-lts-utopic su ...