文件操作

File 文件:


 *         FilterInputStream: 文件输入流
     *         FileReader:读取字符流 数据
     * 
     *         BufferedReader: 通过缓存读取
     *         BufferedInputStream:通过缓存方式 获取输入流数据
 * 
 *         FileOutputStream: 文件输出流
 *             FileWriter:将文件 写入到指定位置
 *             BufferedWriter:
 *             BufferedOutputStream:

 

//创建文件夹
            dir.mkdir();

if (!dir2.exists()) {
            //创建文件夹 多层级 目录创建
            dir2.mkdirs();
        }

 

System.out.println("文件名称:"+declaryFile.getName());
                System.out.println("文件路径:"+declaryFile.getAbsolutePath());
                System.out.println("文件大小(byte):"+declaryFile.length());
                System.out.println("文件是否可写:"+declaryFile.canExecute());
                System.out.println("文件是否可读:"+declaryFile.canRead());
                System.out.println("文件是否隐藏:"+declaryFile.isHidden());
                System.out.println("文件最后修改日期:"+declaryFile.lastModified());

 

       //读取文件内容

 

public static void readFileInfo()throws Exception {
        File fi =    new File("d:/temp/test1.doc");
        //读取文件内容
        FileInputStream fin = new FileInputStream(fi);
        //定义读取文件的 大小
        byte[] bt = new byte[1024];
        //定义长度,每次读取的实际内容长度
        int n=0;
        while((n=fin.read(bt))!=-1){
            //读取 内容 后的操作
            System.out.println("读取的内容为:===>"+new String(bt,0,n));
        }
    }

 

 

操作文件的内容  文件写入类


        FileOutputStream fout = new FileOutputStream(declaryFile);
        String str = "扫描二维码登录微信. 登录手机微信. 手机上安装并登录微信. 从“发现”,进入“扫一扫”,扫码登录微信网页版. 扫描成功. 请在手机上点击确认以登录.";
        
        //转换对应的 数据格式
        byte[] bt = str.getBytes();
        //开始写入
        fout.write(bt);
        //关闭输入流
        fout.close();
        System.out.println("文件写入成功!!!!");
        
    }

 

序列化和反序列化对象

 

public static void main(String[] args) throws Exception {
        
        ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream("D:/temp/goodsInfo.txt"));
        //创建对象
        GoodsEnt goodEnt = new GoodsEnt(10212, "华为手机", "三C产品", 5999.99);
        objOut.writeObject(goodEnt);
        System.out.println("将实体对象 已序列号 的方式 存入 文档文本中...");
        
        //反序列化
        ObjectInputStream objin = new ObjectInputStream(new FileInputStream("D:/temp/goodsInfo.txt"));
        GoodsEnt goods = (GoodsEnt)objin.readObject();
        //输出 转换 后对象 的信息
        System.out.println(goods.toString());;
    }

 

复制文件操作

public class BufferFileDemo_01 {
	public static void main(String[] args) throws Exception {
		long beginTime = System.currentTimeMillis();
		CopyFile();
		long endTiem= System.currentTimeMillis();
		System.out.println("BufferedInputStream:拷贝文件 花费时间:"+(endTiem-beginTime)+"毫秒。");
		
		long beginTime2 = System.currentTimeMillis();
		CopyFile2();
		long endTiem2= System.currentTimeMillis();
		System.out.println("FileInputStream:拷贝文件 花费时间:"+(endTiem2-beginTime2)+"毫秒。");
	}
	/**
	 * 普通 文件操作类
	 * @throws Exception
	 */
	public static void CopyFile2() throws Exception {
		FileInputStream in = new FileInputStream("D:\\123班提交作业\\Java核心\\第一讲.zip");
		byte[] bt =	new byte[1024];
		int n=0;
		
		FileOutputStream out = new FileOutputStream("D:/temp/第一讲_2.zip");
		
		while((n=in.read(bt))!=-1){
			out.write(bt,0,n);
			out.flush();
		}
		out.close();
		in.close();
		
	}
	
	/**
	 * Buffered 文件操作类
	 * @throws Exception
	 */
	public static void CopyFile() throws Exception {
		//文件缓冲流
		BufferedInputStream bin =  new BufferedInputStream(new FileInputStream("D:\\123班提交作业\\Java核心\\第一讲.zip"));
		//通过 缓冲方式将 文件流写入另外一个文件中
		BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("D:/temp/第一讲.zip"));
		
		byte[] bt = new byte[1024];
		int n=0;
		while((n=bin.read(bt))!=-1){
			bout.write(bt,0,n);
			bout.flush();
		}
		bin.close();
		bout.close();
	}
}

 

删除文件操作

 

public class FileDelete {
	//文件夹操作
	public static void main(String[] args) {
		//deleteFile();
		findFile("d:\\temp");
	}
	/**
	 * 删除 存在子文件的文件夹;
	   File file3 = new File("d:\\temp");
	 */
	public static void findFile(String path){
		//创建一个文件目录
		File parentPath = new File(path);
		//获取文件夹中的子文件
		File[] files = parentPath.listFiles();
		//判断是否 :文件 /文件夹
		if(files!=null && files.length>0){
			for (File file : files) {
				System.out.println(file);
				//开始判断
				if(file.isDirectory()){
					//递归调用 进入内部继续遍历
					findFile(file.getAbsolutePath());
					//删除文件夹
					// file.delete();
				}else{
					//如果是文件直接删除
					file.delete();
				}
				
			}
		}
		System.out.println(parentPath);
		//直接删除 外部文件夹
		parentPath.delete();
	}
	
	/*  file.createNewFile();
		file.mkdir();
		file.mkdirs();
		file.listFiles();//变量文件中有哪些文件
		file.delete();
		file.exists();//判断是否存在
	 */		
	public static void deleteFile(){
		File file = new File("d:\\temp\\a\\b\\c\\d\\ff");
		boolean flag = file.delete();
		System.out.println("====》"+flag);
			
		File filed = new File("d:\\temp\\a\\");
		boolean flagd = filed.delete();//删除的文件 夹必须是空文件 夹 否则 不允许删除
		System.out.println("====》"+flagd);
		
		//删除 存在子文件的文件夹;
		File file3 = new File("d:\\temp");
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值