java基础之File和IO流案例

这篇博客详细介绍了Java中文件操作的基础应用,包括获取文件信息、使用字节流和缓冲流实现文件上传,以及多文件上传的实现。同时,还展示了不同类型的打印流,如PrintStream、PrintWriter和FileWriter的使用方法,以及字符流的读写操作。通过这些实例,深入理解Java I/O流的运用。
摘要由CSDN通过智能技术生成

1.file对象的基本应用(文件信息的获取)

/**
	 * 1.file对象的基本应用(文件信息的获取)
	 *	1.创建file对象,关联给定路径
	 *	2.获取该路径下的文件数组
	 *	3.进行判断,如果是文件,打印文件名称
	 *		如果是文件夹,递归调用,打印内部文件
	 */
private static void testFile1(String src) {
    //1.通过file对象,关联给定的路径
    File srcFile = new File(src);
    //2.获取该路径下的所有文件或文件夹的信息
    File [] files = srcFile.listFiles();
    //空文件夹中,不需要遍历调用
    if(files!=null) {
        //3.进行遍历
        for (File file : files) {
            if(file.isFile()) {//如果是文件,输出文件名称
                if(file.getName().endsWith(".txt"))//最后是.txt结尾的进行输出文件信息
                    System.out.println(file.getName());
            }
            if (file.isDirectory()) {//如果是文件夹,进行递归调用,进入该文件夹下面进行遍历文件信息
                //					System.out.println("文件夹:"+file.getAbsolutePath());
                testFile1(file.getAbsolutePath());
            }
        }
    }

}

字节流

2.通过 文件流 实现文件上传

/**
	 * 2.通过	文件流	实现文件上传
	 * 思路:
	 * 	1.通过file与上传文件的路径建立联系
	 * 	2.和上传文件的路径建立联系,如果不存在则创建文件夹
	 * 	3.读文件和写文件
	 * 	4.关闭流
	 * @throws IOException
	 */
private static void testFile2(String src) throws IOException {
    //		1.通过file将源文件路径建立联系,读取文件的路径信息
    File srcFile = new File(src);

    //		2.创建上传后的路径,不存在的话进行创建
    String path= "D:\\upload\\";
    File uploadFile = new File(path);
    if(!uploadFile.exists()) {//判断是否存在该文件
        uploadFile.mkdir();//一层文件夹的创建,如果是多层文件都不存在,需要用到mkdirs方法进行创建
    }

    if(srcFile.exists()) {
        //创建输入和输出流
        InputStream is = new FileInputStream(srcFile);
        String distPath = path + UUID.randomUUID()+srcFile.getName();
        OutputStream os = new FileOutputStream(distPath);

        byte [] b = new byte [1024];//提高读取效率
        int len =0;//每次读取的长度
        while((len=is.read(b))!=-1) {
            os.write(b, 0, len);
        }
        os.close();
        is.close();
    }
}

3.通过 缓冲流 实现文件上传

/**
	 * 3.通过	缓冲流	实现文件上传
	 * @throws IOException
	 */
private static void testFile3(String src) throws IOException {
    //		1.通过file将源文件路径建立联系,读取文件的路径信息
    File srcFile = new File(src);

    //		2.创建上传后的路径,不存在的话进行创建
    String path= "D:\\upload\\";
    File uploadFile = new File(path);
    if(!uploadFile.exists()) {//判断是否存在该文件
        uploadFile.mkdir();//一层文件夹的创建,如果是多层文件都不存在,需要用到mkdirs方法进行创建
    }

    if(srcFile.exists()) {
        //创建输入缓冲流	,也叫包装流
        InputStream is = new BufferedInputStream(new FileInputStream(srcFile));

        String distPath = path + UUID.randomUUID()+srcFile.getName();
        OutputStream os = new BufferedOutputStream(new FileOutputStream(distPath));

        //读和写的具体实现
        byte[] buf = new byte[1024];//通过字节数组提高读取效率
        int len =0;//每次读取的长度,如果读取到文件末尾,该值==-1,代表结束
        while ((len=is.read(buf))!=-1) {
            os.write(buf, 0, len);
        }
        os.close();
        is.close();
    }
}

4.通过file实现多文件上传

/**
	 * 4.文件流 多文件上传
	 * 思路:
	 * 	1.通过file将源文件路径建立联系,读取文件列表的信息
	 * 	2.创建上传后的路径,不存在的话进行创建
	 * 	3.读文件和写文件的过程
	 * 	4.关闭流
	 * @throws IOException 
	 */
private static void testFile4(String src) throws IOException {
    //		1.通过file将源文件路径建立联系,读取文件的路径信息
    File srcFile = new File(src);

    //		2.创建上传后的路径,不存在的话进行创建
    String path= "D:\\upload\\";
    File uploadFile = new File(path);
    if(!uploadFile.exists()) {//判断是否存在该文件
        uploadFile.mkdir();//一层文件夹的创建,如果是多层文件都不存在,需要用到mkdirs方法进行创建
    }
    //获取file的文件列表
    File [] files = srcFile.listFiles();
    if(files!=null) {//进行判断是否存在文件列表
        for (File file : files) {//进行遍历,每次是一个文件
            //				3.读文件和写文件的过程
            //获取输入和输出流
            InputStream is = new FileInputStream(file);
            //目标路径
            String distPath = path+System.currentTimeMillis()+file.getName();
            OutputStream os = new FileOutputStream(distPath);

            //读和写的具体实现
            byte[] buf = new byte[1024];//通过字节数组提高读取效率
            int len =0;//每次读取的长度,如果读取到文件末尾,该值==-1,代表结束
            while ((len=is.read(buf))!=-1) {
                os.write(buf, 0, len);
            }

            //				4.关闭流
            os.close();
            is.close();
        }
    }
}

5.打印流

/**
	 * 5.打印流
	 */
private static void testPrintStream() throws FileNotFoundException, UnsupportedEncodingException {
    PrintStream ps = new PrintStream("aaa.txt","UTF-8");
    ps.append("测试打印流");//追加
    ps.println("打印流");//输出后本行结束,之后录入的换行
    ps.append("你好 ....打印流");
    ps.close();
}

字符流

6.PrintWriter打印流

/**
	 * 6.PrintWriter打印流
	 */
private static void testPrintWriter() throws FileNotFoundException, UnsupportedEncodingException {
    PrintWriter pw = new PrintWriter("bbb.txt","UTF-8");
    pw.write("测试打印流");
    pw.println();
    pw.write("你好 ....");
    pw.close();
}

7.FileWriter打印流

/**
	 * 7.FileWriter打印流
	 */
private static void testFileWriter() throws IOException {
    //第二个参数如果是true,在文本后面追加,false文本内容清空,重头开始录入
    FileWriter fw = new FileWriter("ccc.txt",true);
    fw.write("3333测试 fileWriter...");
    fw.write("4444测试 fileWriter...\n");
    fw.write("4444测试 fileWriter...");
    fw.close();
}

8.字符流 读写操

/**
	 * 8.字符流   读写操
	 * 思路:
	 * 	1.创建	字符流 	读写对象
	 * 	2.读取传入的文件信息,通过写对象拷贝一份到指定位置
	 * @throws Exception 
	 */
private static void testBufferedReader(String src) throws Exception {
    //		1.创建	字符流 	读写对象
    //		Reader br = new BufferedReader(new FileReader(src));
    BufferedReader br = new BufferedReader(new FileReader(src));
    BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\圣墟2.txt"));

    //读取文本有两种方式,
    //1.按照字符读取

    char [] c = new char[200];
    int len=0;
    while((len=br.read(c))!=-1) {
        if(len!=200) {
            System.out.println(len);
            for (char d : c) {
                System.out.print(d);
            }
        }
        bw.write(c, 0, len);
    }
    //2.每次读取一行
    //		String line;//行
    //		while((line=br.readLine())!=null) {
    //	        byte[] b3 = line.getBytes("UTF-8");
    //			System.out.println(new String(b3));
    //			bw.write(line);
    //			bw.newLine();
    //		}

    bw.close();
    br.close();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

自由自在1039

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

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

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

打赏作者

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

抵扣说明:

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

余额充值