1. 演示通过字节流实现文件复制功能。提示:使用FileInputStream类/FileOutputStream类。

该博客展示了如何使用Java实现文件和文件夹的各种操作,包括新建目录和文件、删除文件和文件夹、读取文件夹内容、复制单个文件和整个文件夹、以及移动文件夹。通过FileInputStream和FileOutputStream完成文件复制,使用File类进行文件和目录的管理。
摘要由CSDN通过智能技术生成

题目:

1. 演示通过字节流实现文件复制功能。提示:使用FileInputStream类/FileOutputStream类。

除了实现文件复制功能还写了其他功能。

有哪些功能:

新建目录、新建文件、删除文件、删除文件夹、删除文件夹里面所有文件、读取文件夹下面所有的文件、复制单个文件、复制整个文件夹内容、移动文件夹到指定目录

一共是个文件:⑴FileOperator.java ⑵FileOperatorTest.java

代码如下:

⑴FileOperator.java 

package EighthPackage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.PrintWriter;

public class FileOperator {
	/**author:bw*/
	/**新建目录
	 * */
	public void newFolder(String folderPath){
		try {
			String filePath=folderPath;
			filePath=filePath.toString();
			File myFilePath=new File(filePath);//将给定路径名字符串转换为抽象路径名,创建一个新的File实例
			if(!myFilePath.exists()){//判断抽象路径名表示的文件或目录是否存在
				myFilePath.mkdir();//mkdir创建抽象路径名指定的目录
			}
		} catch (Exception e) {
			System.out.println("新建目录操作出错");
			e.printStackTrace();
		}
		System.out.println("新建目录操作成功");
	}
	
	/**新建文件
	 * */
	public void newFile(String filePathAndName,String fileContent){
		try {
			String filePath=filePathAndName;
			filePath=filePath.toString();
			File myFilePath=new File(filePath);//将给定路径名字符串转换为抽象路径名,创建一个新的File实例
			if(!myFilePath.exists()){//判断抽象路径名表示的文件或目录是否存在
				myFilePath.createNewFile();//createNewFile当具有次抽象路径名指定名称的文件不存在时,创建一个新的空文件
			}
			FileWriter resultFile=new FileWriter(myFilePath);
			PrintWriter myFile =new PrintWriter(resultFile);
			String strContent=fileContent;
			myFile.println(strContent);
			resultFile.close();//关闭指针
		} catch (Exception e) {
			System.out.println("新建文件操作出错");
			e.printStackTrace();
		}
	}
	
	/**删除文件
	 * */
	public void delFile(String filePathAndName){
		try {
			String filePath=filePathAndName;
			filePath=filePath.toString();
			File mydelFile=new File(filePath);//将给定路径名字符串转换为抽象路径名,创建一个新的File实例
			mydelFile.delete();
		} catch (Exception e) {
			System.out.println("删除文件操作出错");
			e.printStackTrace();
		}
		System.out.println("删除文件操作成功");
	}
	/**删除文件夹
	 * */
	public void delFolder(String folderPath){
		try {
			delAllFile(folderPath);
			String filePath=folderPath;
			filePath=filePath.toString();
			File myFilePath=new File(filePath);//将给定路径名字符串转换为抽象路径名,创建一个新的File实例
			myFilePath.delete();
		} catch (Exception e) {
			System.out.println("删除文件夹操作出错");
			e.printStackTrace();
		}
	}
	
	/**删除文件夹里面所有文件
	 * */
	public void delAllFile(String path){
		File file=new File(path);
		if(!file.exists()){
			return;
		}
		String[] tempList=file.list();
		File temp=null;
		for(int i=0;i<tempList.length;i++){
			if(path.endsWith(file.separator)){//separator表示与系统有关的默认名称分隔符
				temp=new File(path+tempList[i]);
			}else{
				temp=new File(path+file.separator+tempList[i]);
			}
			if(temp.isFile()){//判断抽象路径名表示的文件是否是一个标准文件
				temp.delete();
			}
			if(temp.isDirectory()){//判断抽象路径名表示的文件是否是一个目录
				delAllFile(path+"/"+tempList[i]);//先删除文件夹里面的文件
				delFolder(path+"/"+tempList[i]);//再删除空文件夹
			}
		}
	}
	
	/**读取文件夹下面所有的文件
	 * 
	 * */
	public void readFile(String path){
		File file=new File(path);
		
		if(!file.isDirectory()){
			System.out.println("文件名:"+file.getName());
		}else if(file.isDirectory()){
			System.out.println("文件夹");
			String[] filelist=file.list();
			for(int i=0;i<filelist.length;i++){
				
				//System.out.println("名称"+"\n");
				File readfile=new File(path+"//"+filelist[i]);
				if(!readfile.isDirectory()){
					System.out.println("文件名:"+readfile.getName());
				}else if(readfile.isDirectory()){
					readFile(path+"//"+filelist[i]);
				}
			}
		}
	}
	
	/**
	 * 复制单个文件*/
	public void copyFile(String oldPath,String newPath){
		try{
			int bytesum=0;
			int byteread=0;
			File oldfile=new File(oldPath);
			if (oldfile.exists()) {
				InputStream inStream =new FileInputStream(oldPath);//读入原文件
				FileOutputStream fs=new FileOutputStream(newPath);
				byte[] buffer=new byte[1444];
				int length;
				while((byteread=inStream.read(buffer))!=-1){
					bytesum+=byteread;//字节数,文件大小
					//System.out.println(bytesum);
				}
				inStream.close();
			}
		}catch (Exception e){
			System.out.println("复制单个文件操作出错");
			e.printStackTrace();
		}
		System.out.println("复制单个文件操作成功");
	}
	
	
	/**
	 * 复制整个文件夹内容*/
	public void copyFolder(String oldPath,String newPath){
		try{
			(new File(newPath)).mkdir();//如果文件夹不存在则建立新文件夹
			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()){
					FileInputStream input=new FileInputStream(temp);
					FileOutputStream output=new FileOutputStream(newPath+"/"+(temp.getName()).toString());
					byte[] b=new byte[1024*5];
					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[i], newPath+"/"+file[i]);
				}
			}
		}catch(Exception e){
			System.out.println("复制整个文件夹内容操作出错");
			e.printStackTrace();
		}
		System.out.println("复制整个文件夹内容操作成功");
	}
	
	
	/**
	 * 移动文件夹到指定目录*/
	public void moveFile(String oldPath,String newPath){
		copyFile(oldPath, newPath);
		delFile(oldPath);
	}
}

 ⑵FileOperatorTest.java

package EighthPackage;

public class FileOperatorTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FileOperator fileOperator=new FileOperator();
		//新建文件夹
		fileOperator.newFolder("D:\\bw\\mo");
		
		//新建bw.txt文件,写入内容author-bw
		fileOperator.newFile("D:\\bw\\bw.txt", "author-bw");
		//新建bw.txt文件,写入内容絔宝
		fileOperator.newFile("D:\\bw\\mo.txt", "絔宝");
		
		fileOperator.newFile("D:\\bw\\mo\\copyFileTest.txt", "copyFileTest");
		
//		//读文件夹
//		System.out.println("=============");
//		fileOperator.readFile("D:\\bw");
		
		//复制文件bw.txt给mo22.txt
		System.out.println("=============");
		fileOperator.copyFile("D:\\bw\\bw.txt", "D:\\bw\\mo22.txt");
		
		
		//复制D:\\bw\\mo文件夹的全部文件给D:\\bw---文件的内容会一同复制过来
		System.out.println("=============");
		fileOperator.copyFolder("D:\\bw\\mo", "D:\\bw");
		
		//删除文件
		System.out.println("=============");
		fileOperator.delFile("D:\\bw\\mo\\copyFileTest.txt");
	}

}

运行结果:

控制台:

系统中: 

记得点赞哦!

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_62024104/article/details/124290759
————————————————
版权声明:本文为CSDN博主「絔宝」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_62024104/article/details/124290759

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

絔宝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值