实验八 文件IO处理实验

一、实验目的

1.理解基于IO流文件操作的过程及意义

2.掌握基于字节数组缓冲区,实现IO操作方法

3.掌握基于NIO文件目录的创建方法

4.掌握基于NIO文件创建/删除的创建方法

二、实验内容

所有异常,全部在调用方法内捕获处理

将以下IOTest类的全部方法实现

package Test;
import java.util.*;
import java.math.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

public class Test {
    public static void main(String[] args) throws Exception {
        String fileName = "D:/example/from.txt";
        System.out.println("----- 创建文件 ------");
        createFile(fileName);

        System.out.println("-----  将字符串写入文件 -------");
        // \r\n在txt文本中换行
        String str =
                "白日依山尽\r\n" +
                        "黄河入海流\r\n" +
                        "欲穷千里目\r\n" +
                        "更上一层楼\r\n";
        writeToFile(fileName,str);

        System.out.println("--------- 基于基本IO流实现文件的复制 ----------");
        String toFile = "D:/example/to.txt";
        copyByIO(fileName, toFile);

        System.out.println("--------- 基于NIO实现文件的复制 ----------");
        String toFile2 = "D:/example/nio/to.txt";
        copyByNIO(fileName, toFile2);

        System.out.println("---------- 删除指定文件 -------------");
        deleteFile(toFile);
        System.out.println("---------- 遍历指定目录文件 -------------");
        String dir = "D:/example";
        walkDirectories(dir);
    }

    /*
     * 基于指定文件名称创建目录及文件
     * 如果文件已经存在,则忽略
     */
    private static void createFile(String fileName) {
    	Path path=Paths.get(fileName);
    	try {
    		if(!Files.exists(path)) {
    			Files.createDirectories(path.getParent());			
    			Files.createFile(path); 			
    		}
    	}
    	catch(Exception e){
    		e.printStackTrace();
    	}
    }

    /*
     * 提示:文件以字节操作,因此可以
     * 字符串,转字节数组,直接基于Files写入文件
     */
    private static void writeToFile(String fileName, String content) throws Exception{
    	System.out.println(fileName);
    	FileOutputStream out=new FileOutputStream(fileName);	
    	try {
    		byte[] bytes=content.getBytes();
    		out.write(bytes);
    	}
    	catch (Exception e) {
			e.printStackTrace();
    	}
    	finally {
				try {
					out.close();
				} 
				catch (Exception e) {
					e.printStackTrace();
				}
    	}
}
    /*
     * 基于基本IO,以及字节数组缓冲区,复制文件
     * 打印显示循环读写循环次数
     */
    private static void copyByIO(String sourceFile, String targetFile)throws Exception {
    	createFile(targetFile);
    	int flag=0;
    	FileInputStream in=null;
    	FileOutputStream out=null;
    	try {
    		in=new FileInputStream(sourceFile);
    		out=new FileOutputStream(targetFile);
    		byte[] str=new byte[100];
    		in.read(str);
    		out.write(str);
    		flag++;
    		System.out.println("循环读写次数为:"+flag);
    	}
    	catch (Exception e) {
			e.printStackTrace();
    	}
    	finally {
				try {
					in.close();
					out.close();
				} 
				catch (Exception e) {
					e.printStackTrace();
				}
    	}
    }

    /*
     * 基于NIO,实现文件的复制
     */
    private static void copyByNIO(String sourceFile, String targetFile) throws Exception{
    	createFile(targetFile);
    	Path sourcePath=Paths.get(sourceFile);
    	Path targetPath=Paths.get(targetFile);
    	try {
    		Files.copy(sourcePath, targetPath,StandardCopyOption.REPLACE_EXISTING);
    	}
    	catch(Exception e){
    		e.printStackTrace();
    	}
    }

    /*
     * 删除文件
     */
    private static void deleteFile(String fileName) throws Exception{
    	Path path=Paths.get(fileName);
    	try {
			Files.delete(path);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

    /*
     * 遍历打印指定目录下全部目录/文件名称
     */
    private static void walkDirectories(String dir) throws Exception{
    	Path path=Paths.get(dir);
    	try {
			Files.walk(path).forEach(s->System.out.println(s));
		} 
    	catch (Exception e) {
			e.printStackTrace();
		}
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

烟芜~镜

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

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

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

打赏作者

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

抵扣说明:

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

余额充值