java的bio和nio写入及读取txt文件
一.bio的写入及读取
1.采用bio之BufferedWriter 写入文件
public static void main(String[] args) throws IOException {
//创建目录的方法
String yh_path="D:\\XXXX\\9001\\XXXX\\"; //所创建文件目录
File f = new File(yh_path);
if(!f.exists()){
f.mkdirs(); //创建目录
}
FileUtils.makeDirectory("D://XXXX//9001//XXXX/1000_XXXX_20200723_1.txt");//建立目录,目录建到XXXX为止了
FileUtils.fileWrite("D://XXXX//9001//XXXX/1000_XXXX_20200723_1.txt", CryptManager.encode(String.valueOf(126000), Md5Crypt.md5("111111").toUpperCase()));//第一次加密写入文件
FileUtils.fileWrite("D://XXXX//9001//XXXX/1000_XXXX_20200723_2.txt", "126000");//第一次非加密写入文件
//往文件中写内容的方法
for(int i = 0;i<100;i++){
String inTxt_line = "123"+i;//写入的加密前的内容
String inTxt = CryptManager.encode(inTxt_line, Md5Crypt.md5("密码").toUpperCase());//md5加密
BufferedWriter bw = new BufferedWriter(new FileWriter("D://XXXX//9001//XXXX/1000_XXXX_20200723_2.txt", true));
bw.write(inTxt + System.getProperty("line.separator"));//System.getProperty("line.separator") 换行
bw.close();
}
//复制文件的方法
FileUtils.copyFile("D://XXXX//9001//XXXX/1000_XXXX_20200723_2.txt", "D://XXXX//9001//XXXX/1000_XXXX_20200723_3.txt");
//三目运算符及%,/
//每个txt文件的首行数据
int ncount = 120600;
int fileCount = ncount % 60000 > 0 ? ncount / 60000 + 1 : ncount / 60000;
//生成文件
for (int i = 1; i <= fileCount; i++) {
System.out.println("fileCount=="+fileCount);
System.out.println("ncount=="+ncount);
System.out.println("i=="+i);
System.out.println("ncount % 60000========"+(ncount % 60000));
int new_ncount = (fileCount == 1) ? ncount : (i == fileCount) ? ncount % 60000 : 60000;
System.out.println("new_ncount==============="+new_ncount);
System.out.println("=======================================================");
}
}
2.采用nio之BigFileWR 读取文件,
public static void main(String[] args) throws UnsupportedEncodingException {
//某个盘下的多个txt文件的解析
//[D:\XXXX\RESYH\1001_mzxcfk_20201024_1.txt, D:\XXXX\RESYH\1002_mzxcfk_20201024_1.txt, D:\XXXX\RESYH\1003_mzxcfk_20201024_1.txt,
//D:\XXXX\RESYH\1004_mzxcfk_20201024_1.txt, D:\XXXX\RESYH\1005_mzxcfk_20201024_1.txt, D:\XXXX\RESYH\3105_mzxcfk_20201024_1.txt]
String path = "D:\\XXXX\\RESYH\\";
File[] files = FileUtils.listAll(path);
for (int i = 0; i < files.length; i++) {
String a[] = files[i].getName().split("_");//[1001, mzxcfk, 20201024, 1.txt]
log.info("String a[]============"+a);
if (a.length < 4) {//如果文件名不符合规则,则解析下一个文件
continue;
}
String lines;
BigFileWR bfw = null;
bfw = new BigFileWR(path + files[i].getName());
bfw.seek(0); // 49235
String key = "密码";//解密密钥
//bfw.read()是一行行往下读写的
while ((lines = bfw.read()) != null) {
// 文本第一行不计入循环,第一行为银行反馈统计数据
log.info("解析秘钥"+key);
log.info("解析前:"+lines);
lines = CryptManager.decode(lines, Md5Crypt.md5(key).toUpperCase());//md5解密方法
log.info("解析后:"+lines);//24cf8aa4ca784eb88d326df967dda4b6|0||||||||||无数据|2020-10-24|潘芬|2020-10-24|
if (lines.indexOf("|") == -1) {
continue;
}
String[] line = lines.split("\\|");//[24cf8aa4ca784eb88d326df967dda4b6, 0, , , , , , , , , , 无数据, 2020-10-24, 潘芬, 2020-10-24]
log.info("line====="+line);
String[] slxcbh = line[0].split("~");//[24cf8aa4ca784eb88d326df967dda4b6]
log.info("slxcbh====="+slxcbh);
}
}
String key = "111111";//解密密钥
String lines = CryptManager.decode("XWA=", Md5Crypt.md5(key).toUpperCase());//md5解密方法
log.info("解析后:"+lines);//24cf8aa4ca784eb88d326df967dda4b6|0||||||||||无数据|2020-10-24|潘芬|2020-10-24|
}
package tcc.test.likefiletest; /** * @author tcc: * @version 创建时间:2020年12月7日 上午9:27:16 * 类说明 */ import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class likefiletest { /** * 获取文件夹下所有文件的名称 + 模糊查询(当不需要模糊查询时,queryStr传空或null即可) * 1.当路径不存在时,map返回retType值为1 * 2.当路径为文件路径时,map返回retType值为2,文件名fileName值为文件名 * 3.当路径下有文件夹时,map返回retType值为3,文件名列表fileNameList,文件夹名列表folderNameList */ public static HashMap<String, Object> getFilesName(String folderPath , String queryStr) { HashMap<String, Object> map = new HashMap<>(); List<String> fileNameList = new ArrayList<>();//文件名列表 List<String> folderNameList = new ArrayList<>();//文件夹名列表 File f = new File(folderPath); if (!f.exists()) { //路径不存在 map.put("retType", "1"); }else{ boolean flag = f.isDirectory(); if(flag==false){ //路径为文件 map.put("retType", "2"); map.put("fileName", f.getName()); }else{ //路径为文件夹 map.put("retType", "3"); File fa[] = f.listFiles();//[D:\XXXX\1.jpg, D:\XXXX\1001, D:\XXXX\1002, D:\XXXX\1003, D:\XXXX\1004, D:\XXXX\1005, D:\XXXX\2001, D:\XXXX\2002, D:\XXXX\2003, D:\XXXX\2004, D:\XXXX\2006, D:\XXXX\2007, D:\XXXX\2008, D:\XXXX\2009, D:\XXXX\2010, D:\XXXX\2011, D:\XXXX\2012, D:\XXXX\2014, D:\XXXX\2015, D:\XXXX\2017, D:\XXXX\2018, D:\XXXX\2019, D:\XXXX\2020, D:\XXXX\3002, D:\XXXX\3003, D:\XXXX\3023, D:\XXXX\3026, D:\XXXX\3032, D:\XXXX\3033, D:\XXXX\3035, D:\XXXX\3050, D:\XXXX\3052, D:\XXXX\3060, D:\XXXX\3062, D:\XXXX\3063, D:\XXXX\3066, D:\XXXX\3071, D:\XXXX\3087, D:\XXXX\3088, D:\XXXX\3089, D:\XXXX\3093, D:\XXXX\3094, D:\XXXX\3095, D:\XXXX\3096, D:\XXXX\3097, D:\XXXX\3098, D:\XXXX\3099, D:\XXXX\3100, D:\XXXX\3101, D:\XXXX\3102, D:\XXXX\3103, D:\XXXX\3104, D:\XXXX\3105, D:\XXXX\6001, D:\XXXX\6002, D:\XXXX\6003, D:\XXXX\6004, D:\XXXX\6005, D:\XXXX\6006, D:\XXXX\6007, D:\XXXX\6008, D:\XXXX\6009, D:\XXXX\6010, D:\XXXX\6011, D:\XXXX\6012, D:\XXXX\6013, D:\XXXX\6014, D:\XXXX\6015, D:\XXXX\6016, D:\XXXX\6017, D:\XXXX\6018, D:\XXXX\7001, D:\XXXX\9000, D:\XXXX\9001, D:\XXXX\9002, D:\XXXX\BAK, D:\XXXX\FTPFS, D:\XXXX\FTPJS, D:\XXXX\XXXX, D:\XXXX\RESYH, D:\XXXX\XXXX_TEMP] System.out.println("fa[]================="+fa); queryStr = queryStr==null ? "" : queryStr;//若queryStr传入为null,则替换为空(indexOf匹配值不能为null) for (int i = 0; i < fa.length; i++) { File fs = fa[i]; if(fs.getName().indexOf(queryStr)!=-1){ if (fs.isDirectory()) { folderNameList.add(fs.getName()); } else { fileNameList.add(fs.getName()); } } } map.put("fileNameList", fileNameList); map.put("folderNameList", folderNameList); } } return map; } public static void main(String[] args) { //HashMap<String,Object> filesName = getFilesName("D:\\XXXX\\RESYH", "1001"); HashMap<String,Object> filesName = getFilesName("D:\\XXXX",null); System.out.println(filesName); List filelistname = (List) filesName.get("fileNameList"); for(int i = 0;i<filelistname.size();i++){ System.out.println(filelistname.get(i)); } } }