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类中提供的方法足 ...
随机推荐
- 可视化:svg相关基础
01.svg的嵌入.html <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- linux 下后台运行python脚本
这两天要在服务器端一直运行一个Python脚本,当然就想到了在命令后面加&符号 $ python /data/python/server.py >python.log &说明: ...
- call of overloaded 'xxx' is ambiguous
这里定义了一个模版函数,功能同STL里的copy函数: #include <vector> #include <list> #include <iostream> ...
- Mycat 分片规则详解--枚举分片
实现方式:切分规则根据文件(partition-hash-int.txt)配置的可能的枚举来进行分片,此种分片规则理解为枚举分区,会比较适合于取值固定的场合,比如说省份(固定值) 优点:适用于按照省份 ...
- Java TimSort算法 源码 笔记
本来准备看Java容器源码的.但是看到一开始发现Arrays这个类我不是很熟,就顺便把Arrays这个类给看了.Arrays类没有什么架构与难点,但Arrays涉及到的两个排序算法似乎很有意思.那顺便 ...
- 【Python】 系统配置/进程等信息查看 psutil
psutil 原以为psutil只是跟进程有关的一个模块,没想到它其实提供了从CPU到内存各种各样的信息,十分IMBA.记录一下 我用了pip install psutil安装的这个模块,不过路中遇到 ...
- Nginx目录浏览功能
要给其他人提供一个patch的下载地址,于是想用nginx的目录浏览功能来做,需要让其他人看到指定一个目录下的文件列表,然后让他自己来选择该下载那个文件:效果如图. 实现步骤:在虚拟主机配置文件里面开 ...
- Java集合:HashMap源码剖析
一.HashMap概述 HashMap基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了不同步和允许使用 null 之外,HashMap ...
- Nginx阻止对不明确主机名的请求
在用户请求头中,有可能会有Host行不明确的情况,如果不想处理这类用户请求,那么可以定义一个默认的server来丢弃这类请求.例如 server{ listen default_server; ser ...
- APK Multi-Tool强大的APK反编译工具终极教程
一.APK Multi-Tool介绍 APK Multi-Tool 是APK Manager的升级版,是一个强大的APK反编译工具,集多种功能于一身,是居家必备.做ROM必选的工具! 这是 ...