java-指定目录下文件读写操作的详细介绍

来源:爱站网时间:2018-11-14编辑:网友分享
不管你学习的课程是否专业,在互联网这个世界,每时每刻都需要不断的学习,说到编程,其实最痛苦的应该是程序员了,为了大家的方便和节省工作量,必须要写出一个程序的处理方法,下文是爱站技术频道小编为大家整理的java-指定目录下文件读写操作的详细介绍。

不管你学习的课程是否专业,在互联网这个世界,每时每刻都需要不断的学习,说到编程,其实最痛苦的应该是程序员了,为了大家的方便和节省工作量,必须要写出一个程序的处理方法,下文是爱站技术频道小编为大家整理的java-指定目录下文件读写操作的详细介绍。
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 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-指定目录下文件读写操作的详细介绍,其实当我们要达到别人的要求时,我们不应该急于处理它,我们应该首先分析它,然后做出最好的解决办法来处理它,而代码也是一样的。

上一篇:java注解接口的操作步骤

下一篇:移动指定文件夹内的全部文件

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载