您的位置:首页 > 职场人生

黑马程序员—这是一个能够键盘录入路径去批量修改指定文件夹下所有指定文件内容的方法

2013-12-29 21:23 986 查看
------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

package xiugaiqi;

import java.io.*;

public class wenbenxiugai {

public static void main(String[] args) throws Exception{

System.out.println("请输入文件所在的路径");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String path=br.readLine();

System.out.println("请输入替换前的内容和替换后的内容,回车区分。");

BufferedReader thnr =new BufferedReader(new InputStreamReader(System.in));

String
nr=thnr.readLine();

String
tr=thnr.readLine();

cf(path,nr,tr);

}

public static void cf(String path,String nr ,String tr)throws Exception

{

File f=null;

File root = new File(path);

//判断路径

if(! root.exists() && root.isDirectory()){

System.out.println("路径有误!");

}

File[] files = root.listFiles();

System.out.println("总路径"+root.getAbsolutePath());

for(int i=0;i<files.length;i++)

{

f = files[i];

String name = f.getName();

if(f.isDirectory())

{

System.out.println(name+"................"+f.getAbsolutePath()); //文件名

ga.gaiming(f.getAbsolutePath(),nr,tr);

cf(f.getAbsolutePath(),nr,tr);

}

}

}

}

class ga {

public static void gaiming(String ypath,String nr,String tr)throws Exception {

//String huancunpath=br.readLine();

File root = new File(ypath+"\\中哥");

if(!root.exists())

{

root.mkdir();

}

String huancunpath=root.getAbsolutePath();

cf(ypath,huancunpath,nr,tr);

copyy(huancunpath,ypath);//"D:\\File\\c"预存路径

deletefile(huancunpath);

}

//查找文件

public static File cf(String ph,String hc,String nr,String tr)throws Exception

{

File f=null;

//String path=br.readLine();

File root = new File(ph);

//判断路径

if(! root.exists() && root.isDirectory()){

System.out.println("路径有误!");

}

File[] files = root.listFiles(

new FilenameFilter(){

//文件名称过滤

public boolean accept(File dir, String name) {

return name.endsWith(".txt");

}

}

);

for(int i=0;i<files.length;i++)

{

f = files[i];

String name = f.getName();

// System.out.println(name); //文件名

gaiFile(files[i],name,hc,nr,tr);

}

return f;

}

//替换文件的内容

public static void gaiFile(File ff, String name,String hc,String nr ,String tr)throws Exception

{

String line;

BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(ff))); //"D:\\File\\b"原路径。

BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(hc+"\\"+name))); //加\\是因为文件夹无法读取这些字符。

//就不会有最后一个字符加到name的前头的情况了。

StringBuilder sb=new StringBuilder();

while((line=br.readLine())!=null)

{

System.out.println(name+".........."+line);//显示文件名及文件名的内容。

sb.append(line+"\r\n");//SB没有换行就加载进去了,所以加一个换行"\r\n"。否则结果在一行里。

}

xn( sb,nr,tr);//新内容。

bw.write(sb.toString(),0,sb.length());

bw.newLine();

sb.delete(0, sb.length());

br.close();

bw.flush();

bw.close();

}

//新内容

public static void xn( StringBuilder sb,String nr ,String tr)throws Exception

{

int index=0;

while((index=sb.indexOf(nr,index))!=-1)

{

sb.replace(index,index+nr.length(),tr);

index+=tr.length();

}

}

//将暂存位置的文件复制到原来的文件夹中。

public static void copyy(String originDirectory,String targetDirectory){

File origindirectory = new File(originDirectory); //源路径File实例"D:\\file\\c"缓存路径。

File targetdirectory = new File(targetDirectory); //目标路径File实例"D:\\file\\b"

if(!origindirectory.isDirectory() || !targetdirectory.isDirectory()){ //判断是不是正确的路径

System.out.println("不是正确的目录!");

return;

}

File[] fileList = origindirectory.listFiles(); //目录中的所有文件

for(File file : fileList){

if(!file.isFile()) //判断是不是文件

continue;

System.out.println(file.getName());

try{

FileInputStream fin = new FileInputStream(file);

BufferedInputStream bin = new BufferedInputStream(fin);

PrintStream pout = new PrintStream(targetdirectory.getAbsolutePath()+"/"+file.getName());

BufferedOutputStream bout = new BufferedOutputStream(pout);

int count;

while((count = bin.available())!= 0){

int c = bin.read(); //从输入流中读一个字节

bout.write(((char)c)); //将字节(字符)写到输出流中

}

bout.close();

pout.close();

bin.close();

fin.close();

}catch(IOException e){

e.printStackTrace();//这种异常在控制台上输出非常详细的异常信息。

}

}

System.out.println("复制成功啦哈哈哈哈哈哈");

}

public static boolean deletefile(String delpath) throws Exception {

try {

File file = new File(delpath);

// 当且仅当此抽象路径名表示的文件存在且 是一个目录时,返回 true

if (!file.isDirectory()) {

file.delete();

} else if (file.isDirectory()) {

String[] filelist = file.list();

for (int i = 0; i < filelist.length; i++) {

File delfile = new File(delpath + "\\" + filelist[i]);

if (!delfile.isDirectory()) {

delfile.delete();

System.out

.println(delfile.getAbsolutePath() + "删除文件成功");

} else if (delfile.isDirectory()) {

deletefile(delpath + "\\" + filelist[i]); //如果这不是文件,而是一个文件夹,那么这个文件夹就按照文件的程序来处理,删除。

}

}

System.out.println(file.getAbsolutePath()+"删除成功");

file.delete();

}

} catch (FileNotFoundException e) {

System.out.println("deletefile() Exception:" + e.getMessage());

}

return true;

}

}

------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐