java对指定目录下文件读写操作介绍
发布时间:2023-01-26 11:53:17
标签:java,读写文件
最近因为项目的国际化的需要,需要对整个项目的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;
}
总结:当我们拿到一个别人给的需求,先不要急于去处理,先分析,再分析,然后做出最优的解决方案,处理好这项工作。


猜你喜欢
- 示例代码如下:namespace SampleListT{ class Program { &
- Java中有两类线程:User Thread(用户线程)、Daemon Thread(守护线程)用户线程即运行在前台的线程,而守护线程是运行
- 获取Android的ROOT权限其实很简单,只要在Runtime下执行命令"su"就可以了。// 获取ROOT权限pub
- WPF下给ComboBox设置绑定字段时可通过如下设置:combobox.SelectedValuePath = "编号"
- 前言spring中解析元素最重要的一个对象应该就属于 BeanDefinition了;这个Spring容器中最基本的内部数据结构;它让xml
- RocketMq消息处理RocketMq消息处理整个流程如下:本系列RocketMQ4.8注释github地址,希望对大家有所帮助,要是觉得
- 本文导读中秋节是中国民间的传统节日,中秋节源自天象崇拜由上古时代秋夕祭月演变而来。中秋节自古便有祭月、赏月、吃月饼等民俗,流传至今,经久不息
- springboot启动失败的问题springboot版本是1.3.0.M1,连接的mysql版本为8,用spring-boot-start
- C++/java 继承类的多态详解学过C++和Java的人都知道,他们二者由于都可以进行面向对象编程,而面向对象编程的三大特性就是封装、继承
- 一、微信官方文档微信支付开发流程(公众号支付)首先我们到微信支付的官方文档的开发步骤部分查看一下需要的设置。[图片上传失败...(image
- Step 1.依赖bannerGradledependencies{ compile 'com.youth.banner
- Mybatis删除多个数据例如:删除数据库中sid=1和sid=2的数据操作步骤如下1.在实体类中创建一个LIst用于存放要删除的sid2.
- 这篇文章主要介绍了Springboot创建子父工程过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要
- 本文实例讲述了C# DataTable中Compute方法用法。分享给大家供大家参考,具体如下:Compute函数的参数就两个:Expres
- 喜欢另辟蹊径的我,在这里废话不多说了,直接上代码和图片了。效果图如下:第一步:MainActivity的代码如下:package net.l
- 最近在学习springmvc,今天把springmvc 参数绑定给整理一下,也算个学习笔记吧!@RequestParam 绑定单个请求Req
- 现在的应用在注册登录或者修改密码中都用到了短信验证码,那在android中是如何实现获取短信验证码并自动填写的呢?首先,需要要在manife
- 本文实例讲述了Android获取设备CPU核数、时钟频率以及内存大小的方法。分享给大家供大家参考,具体如下:因项目需要,分析了一下 Face
- 本文较为详细的分析了Java反射机制。分享给大家供大家参考,具体如下:一、预先需要掌握的知识(java虚拟机) java虚拟机的方法区:ja
- 使用 transient 修饰private transient String noColumn;使用 static 修饰private s