java对指定目录下文件读写操作介绍
时间:2023-01-26 11:53:17
最近因为项目的国际化的需要,需要对整个项目的100来个插件做国际化,这是一件痛苦的事情,因为纯体力劳动。为了省点工作量,想着能不能写个程序批处理了,减少点工作量,于是就有了下面的代码。
1.读取指定的(.java)文件:
public static String readFile(String path) throws IOException {
File f = new File(path);
StringBuffer res = new StringBuffer();
String filePathStr = f.getPath();
System.out.println("获取文件的路径:::::::"+filePathStr);
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis,Charset.forName("GBK")); //以gbk编码打开文本文件
BufferedReader br = new BufferedReader(isr, 8192 * 8);
String line = null;
int linenum = 0;
while((line=br.readLine())!=null) {
linenum ++;
res.append(line+"此处可以添加你自己的字符串处理逻辑"+"\r\n");
}
br.close();
return res.toString();
}
2.读取的文件内容信息写到指定的(.java)文件
public static boolean writeFile(String cont, String path) {
try {
File dist = new File(path);
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(dist),"GBK");
writer.write(cont);
writer.flush();
writer.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
3.查找指定目录下所有符合条件的.java文件,并更新文件信息
/**
* 查找文件
* @param f
* @throws IOException
*/
public static void findFile(File f) throws IOException {
if(f.exists()) {
if(f.isDirectory()) {
for(File fs:f.listFiles(ff)) {
findFile(fs);
}
} else {
updateFile(f);
}
}
}
/**
* 逐行读java文件
* @param f
* @throws IOException
*/
private static void updateFile(File f) throws IOException {
String filePathStr = f.getPath();
System.out.println("开始读取文件的路径:::::::"+filePathStr);
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis,Charset.forName("GBK")); //以gbk编码打开文本文件
BufferedReader br = new BufferedReader(isr, 8192 * 8);
String line = null;
int linenum = 0;
StringBuffer res = new StringBuffer();
while((line=br.readLine())!=null) {
String updateStr= updateStr(line);
res.append(updateStr+"\r\n");
if(!line.trim().equals(updateStr.trim())) {
linenum ++;
}
}
br.close();
//如果文件有修改,则修改后的文件,覆盖原有文件
if(linenum>0) {
System.out.println("=============================");
System.out.println("filePathStr:"+filePathStr);
System.out.println("文件修改了:"+linenum+"处。");
System.out.println("=============================");
String cont = res.toString();
ReadWriteFile.write(cont, filePathStr);
}
}
/**
* 验证读取的字符串信息
* 和更新字符串信息
* @param str
*/
private static String updateStr(String str) {
//判断字符串是否是需要更新的字符串
boolean isok = filterStr(str);
int strNum = StringValidation.strNum(str, StringValidation.ch);
if(isok || strNum == 0) {
return str;
} else {
String temp = "";
for(int i=1;i<=strNum/2;i++) {
temp += " //$NON-NLS-"+i+"$"; //需要添加的字符
}
str = str+temp;
}
return str;
}
//过滤文件类型
private static FileFilter ff = new FileFilter() {
public boolean accept(File pathname) {
String path = pathname.getName().toLowerCase();
logger.info("FileFilter path::::"+path);
//只匹配 .java 结尾的文件
if (pathname.isDirectory() || path.endsWith(".java")) {
return true;
}
return false;
}
};
/**
* 过滤掉不需要处理的字符串
* @param str
* @return
*/
public static boolean filterStr(String str) {
boolean isok = false;
//过滤字符串
isok = (str.indexOf("import ")>=0)
|| (str.indexOf("package ")>=0)
|| (str.indexOf(" class ")>=0)
|| (str.indexOf("//$NON-NLS")>=0)
|| (str.indexOf("//")==0)
|| (str.indexOf("/*")>=0)
|| (str.indexOf("*")>=0)
|| (str.trim().indexOf("@")==0)
|| (str.indexOf("\"")==-1)
|| ("".equals(str))
|| isCh(str);
return isok;
}
/**
* 验证字符串是否含有中文字符
* @param str
* @return
*/
public static boolean isCh(String str) {
Pattern pa = Pattern.compile("[\u4E00-\u9FA0]");
Matcher m = pa.matcher(str);
boolean isok = m.find();
return isok;
}
总结:当我们拿到一个别人给的需求,先不要急于去处理,先分析,再分析,然后做出最优的解决方案,处理好这项工作。
标签:java,读写文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
聊聊@RequestMapping和@GetMapping @PostMapping的区别
2021-07-26 19:20:43
![](https://img.aspxhome.com/file/2023/7/65167_0s.png)
Java中实现简单的Excel导出
2021-10-21 07:23:25
![](https://img.aspxhome.com/file/2023/0/68730_0s.png)
关于Kotlin写界面时诸多控件的点击事件
2023-01-28 12:34:51
![](https://img.aspxhome.com/file/2023/5/137585_0s.jpg)
c# 关闭窗体时提示的小例子
2021-11-10 19:08:18
Android使用TextView,设置onClick属性无效的解决方法
2022-06-27 11:32:39
C#接口(Interface)用法分析
2022-10-18 10:59:17
Java日常练习题,每天进步一点点(3)
2023-07-12 14:24:14
C语言实现简单版三子棋
2023-03-01 22:36:30
Fluent Mybatis实际开发中的优势对比
2022-07-09 14:29:34
Java SpringBoot实现带界面的代码生成器详解
2023-09-28 11:54:14
![](https://img.aspxhome.com/file/2023/0/63000_0s.png)
python gstreamer实现视频快进/快退/循环播放功能
2022-07-06 10:09:20
详解JS与APP原生控件交互
2022-11-21 21:28:15
![](https://img.aspxhome.com/file/2023/3/108303_0s.png)
Kotlin中ListView与RecyclerView的应用讲解
2023-01-24 01:26:44
Android WebP 图片压缩与传输
2022-05-30 16:18:16
![](https://img.aspxhome.com/file/2023/8/130328_0s.jpg)
详解Java ES多节点任务的高效分发与收集实现
2021-08-03 13:59:02
springcloud微服务之Eureka配置详解
2021-06-15 14:35:28
Java集合框架ArrayList源码分析(一)
2022-05-12 19:32:50
C#实现移除字符串末尾指定字符的方法
2023-02-09 13:32:21
![](https://img.aspxhome.com/file/2023/8/102158_0s.png)
关于Assert.assertEquals报错的问题及解决
2023-11-03 04:56:10
![](https://img.aspxhome.com/file/2023/6/77136_0s.jpg)
C#有效防止同一账号多次登录(附三种方法)
2023-05-23 10:32:45