首页 > 技术文章 > java文件相关的练习

canglongdao 2020-05-30 11:39 原文

//复制
import java.io.*;
public class TestMain{
	public static void main(String[] args){
		FileInputStream fa=null;
		FileOutputStream fb=null;
		try{
			File a=new File("C:\\kms10.zip");//"C:\\kms10.log",可以复制任何格式的文件
			File b=new File("D:\\java\\object\\"+a.getName());
			b.delete();
			fa=new FileInputStream(a);
			fb=new FileOutputStream(b);
			byte[] ba=new byte[1024];
			int na=fa.read(ba);			
			while(na!=-1){				
				fb.write(ba,0,na);//将读取到的有效元素写入;				
				na=fa.read(ba);
			}
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			try{
				if(fa!=null){
					fa.close();
				}
			}catch(IOException e){
				e.printStackTrace();
			}
			try{				
				if(fb!=null){
					fb.close();
				}
			}catch(IOException e){
				e.printStackTrace();
			}
		}
	}
}

  加密,解密 每一次数组的前两个元素位置互换;很多种方式

import java.io.*;
public class TestMain{	
	private byte[] encryption(byte[] a){		
		for(int i=0;i<a.length;i++){
			a[i]=(byte)(a[i]+30);
		}		
		/*
		byte temp=a[0];//数组中前2个字节换一下,记事本不适合;
		a[0]=a[1];
		a[1]=temp;
		
		*/		
		return a;
	}
	private byte[] decrypt(byte[] b){			
		for(int i=0;i<b.length;i++){
			b[i]=(byte)(b[i]-30);
		}				
		/*
		byte temp=b[0];
		b[0]=b[1];
		b[1]=temp;		
		*/
		return b;
	}	
	public static void main(String[] args){		
		TestMain tm=new TestMain();
		File faa=null;
		File fbb=null;
		try{//加密;
			faa=new File("C:\\kms10.log"); //任何格式的文件;//原文件;
			fbb=new File("D:\\java\\object\\"+faa.getName());//加密后文件;
			FileInputStream fa=new FileInputStream(faa);
			FileOutputStream fb=new FileOutputStream(fbb);
			fbb.delete();
			byte[] ba=new byte[1000];
			int na=fa.read(ba);
			while(na!=-1){
				ba=tm.encryption(ba);				
				fb.write(ba,0,na);
				na=fa.read(ba);
			}
		}catch(IOException e){
			e.printStackTrace();
		}
		try{//解密			
			File dfbb=new File("D:\\java\\object\\test\\"+fbb.getName());//解密后文件
			FileInputStream fb=new FileInputStream(fbb);//加密后文件流
			FileOutputStream dfb=new FileOutputStream(dfbb);
			dfbb.delete();
			byte[] ba=new byte[1000];
			int na=fb.read(ba);
			while(na!=-1){
				ba=tm.decrypt(ba);
				dfb.write(ba,0,na);
				na=fb.read(ba);
			}
		}catch(IOException e){
			e.printStackTrace();
		}		
	}
}

 复制文件、文件夹

import java.io.*;
public class TestMain{
	public void getPath(File a){
		File[] ason=a.listFiles();//获取a文件下的所有子文件夹对象;		
		try{			
			//System.out.println(a.getAbsoluteFile());	
			//String ll=a.toString().replace("D:\\java\\object","D:\\java1\\object1");
			//String ll=a.getAbsolutePath().replace("D:\\java\\object","D:\\java1\\object1");
			//System.out.println(ll);
			String ll="D:\\1"+a.getAbsolutePath().split(":")[1];//新的路径;
			
			if(a.isDirectory()){//是文件夹?//复制文件夹			
				File newA=new File(ll);
				newA.mkdirs();
				System.out.println(a.getName()+"复制文件夹完成");
				if(ason.length!=0){//文件夹的话,需要判断是否递归
					for(File v:ason){
						this.getPath(v);
					}
				}				
			}
			if(a.isFile()){	//复制文件
				new File(ll.substring(0,ll.length()-a.getName().length())).mkdirs();//创建目录,不然只有文件时,新的目录文件不存在;
				FileInputStream fa=new FileInputStream(a);//文件流
				FileOutputStream oa=new FileOutputStream(ll);
				byte[] bb=new byte[1024];//1kb-8kb
				int nfa=fa.read(bb);
				while(nfa!=-1){
					oa.write(bb,0,nfa);
					nfa=fa.read(bb);//再读一遍;
				}
				System.out.println(a.getName()+"复制文件完成");
			}
		}catch(FileNotFoundException e){
			e.printStackTrace();
				
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	public static void main(String[] args){
		TestMain tm=new TestMain();
		//tm.getPath(new File("D:\\java\\object\\yic"));//目录下有文件夹,文件
		//tm.getPath(new File("D:\\ddd\\a.zip"));//文件
		tm.getPath(new File("D:\\ddd"));//空文件夹
	}
}

  

 

推荐阅读