⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nono.java

📁 初学JAVA时写的代码。 1.复制一个或多个文件夹 2.替换文件夹中所有文本中的某些字符
💻 JAVA
字号:
/**
 *对文件的复制操作和字符替换操作
 *修改时间:2006.05.09(第二次修改)
 *修改内容:1`修改了复制文件及文件夹的目标地址
 *			2`在文本的字符串替换中加入了递归,实现子文件夹中文本字符的替换
 *          3`修改了主函数,去掉了多余的变量,更改了函数的调用
 */
 import java.util.regex.*;
import java.util.ArrayList;
import java.io.*;

public class nono{

public static void main(String[] args)throws IOException{
	String str1,str2,str3,str4;
	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));	
	//输入变量的值
	System.out.print("请输入要复制的源文件夹>>>");  
	str1=br.readLine();
	System.out.print("请输入目标地址>>>");  
	str2=br.readLine();
	System.out.print("请输入源字符串>>>"); 
	str3=br.readLine();
	System.out.print("请输入目标字符串>>>"); 
	str4=br.readLine();
	File n=new File(str1);	
	//判断是是文件夹还是单一文件,分别调用不同的函数
	if(n.isFile())
	changeone(copyone(str1,str2),str3,str4);	
	if(n.isDirectory())
	change(copyFolder(str1,str2),str3,str4);
	System.out.println("操作完成!"); 
	}

//复制单个文件
public static String copyone(String oldPath,String newPath) 
throws IOException {  
      
         int byteread=0;  
         File oldfile=new File(oldPath); 
         //文件存在时    
         if  (oldfile.exists()){ 
         	 //读入原文件   
             InputStream inStream=new  FileInputStream(oldfile); 
             //写入到目标文件
             FileOutputStream  fs=new  FileOutputStream(newPath+File.separator+(oldfile.getName()).toString());         
             byte[] buffer=new byte[4096];                 
             while  ((byteread=inStream.read(buffer))!=-1){  
		     fs.write(buffer,0, byteread);  
             }  
             inStream.close();  
         }
           else  System.out.println("源文件不存在");
           
 return(newPath+File.separator+(oldfile.getName()).toString());
 
   } 

//复制文件夹
public static String copyFolder(String oldPath,String newPath)
throws IOException{      
         //遍历文件夹中的文件   
         File a=new File(oldPath);  
         String[]  file=a.list();  
         File  temp=null;  
         //逐一对每个子成员进行复制
         for  (int i=0;i<file.length;i++)  {  
             //判断是否有路径分隔符
           if(oldPath.endsWith(File.separator)){  
              temp=new File(oldPath+file[i]);  
           }  
           else{  
              temp=new File(oldPath+File.separator+file[i]);  
               }  
           //如果是文件
             if(temp.isFile()){
             	//如果文件夹不存在  则建立新文件夹 
             	(new  File(newPath+File.separator+a.getName())).mkdirs();   
                 FileInputStream input=new FileInputStream(temp);  
                 FileOutputStream output= new FileOutputStream(newPath+File.separator+a.getName()+File.separator+(temp.getName()).toString());  
                 byte[] b=new byte[4096];  
                 int len;  
                 while  ((len=input.read(b))!=-1){  
                 output.write(b,0,len);  
                 }  
                 output.flush();  
                 output.close();  
                 input.close();  
             } 
              //如果是子文件夹  
             if(temp.isDirectory()){
               copyFolder(oldPath+File.separator+file[i],newPath+File.separator+a.getName()+File.separator);  
             }  
         }
          return((newPath+File.separator+a.getName()).toString()); 
       
       
 
   }
 

//对文件中的字符进行替换,再将所有字符写到容器中	
public static String[] copyFile(File inFile,String oldch,String newch)     
    throws IOException
    {
    FileReader fr=null;
    BufferedReader br=null;
    String str="";
    ArrayList copy=new ArrayList();
    try{
    	fr=new FileReader(inFile);
    	br=new BufferedReader(fr);
    	while((str=br.readLine())!=null)
    	{
        //用正则表达式完成特定字符的替换
        String regEx=oldch;
        Pattern p=Pattern.compile(regEx);
        Matcher m=p.matcher(str);
        String s=m.replaceAll(newch);
        copy.add(s);
        }
        br.close();
    }
     catch(IOException e){;}
        //传回字符串
        return(String[])copy.toArray(new String[0]);
	}
        
//单个文本的替换,将copyFile返回的字符重新写入到文本中
public static void  changeone(String  oldPath,String oldch,String newch)   
  
   throws IOException{
   	FileWriter fw=null;
	BufferedWriter bw=null;
	File temp=new File(oldPath);
   //调用copyFile方法
	String out[]=copyFile(temp,oldch,newch);
	//写入
		fw=new FileWriter(temp);
		bw=new BufferedWriter(fw);
		for(int j=0;j<out.length;j++){
			bw.write(out[j]+"\r\n");
		}
		bw.close();

    }

//文件夹中所有文本的替换
public static void change(String toFile,String oldch,String newch)
throws IOException{
	File a=new File(toFile);
	String[] file=a.list();
	File temp=null;
	for(int i=0;i<file.length;i++)
	{
	//判断是否有路径分隔符 
	if(toFile.endsWith(File.separator)){  
		temp=new File(toFile+file[i]);  
       } 
       else{  
       temp=new File(toFile+File.separator+file[i]);  
       }
     if(temp.isFile()){
        
    FileWriter fw=null;
	BufferedWriter bw=null;    
	//调用copyFile方法
	String out[]=copyFile(temp,oldch,newch);
	//写入
		fw=new FileWriter(temp);
		bw=new BufferedWriter(fw);
		for(int j=0;j<out.length;j++){
			bw.write(out[j]+"\r\n");
		}
		bw.close();
		}
	
	 if(temp.isDirectory()){
               change(toFile+File.separator+file[i],oldch,newch);  
             }  
	
    }

    }
    

}	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -