使用IO流完成文件操作

本文将通过三个实例详细讲解Java IO流的使用方法,包括如何统计文件夹中不同类型文件的数量、如何读取文件内容、如何将图片文件从一个文件夹复制到另一个文件夹。

题目一:统计一个文件夹中每种文件的个数并打印

**需求:**统计指定文件夹中每种类型文件的数量,并打印统计结果,例如:

txt:3个doc:4个jpg:6个

**代码示例:**javaimport java.io.File;import java.io.IOException;

public class FileCount { public static void main(String[] args) { File dir = new File('文件夹路径'); File[] files = dir.listFiles(); int txtCount = 0; int docCount = 0; int jpgCount = 0; for (File file : files) { if (file.isFile()) { String fileName = file.getName(); String extension = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase(); switch (extension) { case 'txt': txtCount++; break; case 'doc': docCount++; break; case 'jpg': jpgCount++; break; } } } System.out.println('txt:' + txtCount + '个'); System.out.println('doc:' + docCount + '个'); System.out.println('jpg:' + jpgCount + '个'); }}

题目二:在计算机的一个盘根目录下创建一个文件,并读取文件内容

需求:

  1. 在计算机的D盘根目录下创建一个名为file01.txt的文件,并写入以下内容(要求是单字节字符,比如字母或者数字):

1 abcdef123456

  1. 使用字节输入流一次读取一个字节的方法将file01.txt文件读取并打印。

  2. 使用字节输入流一次读取多个字节的方法将file01.txt文件读取并打印。

**代码示例:**javaimport java.io.File;import java.io.FileInputStream;import java.io.IOException;

public class ByteInput { public static void main(String[] args) { File file = new File('D:\file01.txt'); FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); int b; while ((b = fileInputStream.read()) != -1) { System.out.print((char) b); } System.out.println(); byte[] bytes = new byte[1024]; int len; while ((len = fileInputStream.read(bytes)) != -1) { System.out.print(new String(bytes, 0, len)); } } catch (IOException e) { e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }}

题目三:将一个文件夹中的图片文件复制到另一个文件夹

需求:

  1. 在D盘中创建两个文件夹,img1img2

  2. img1文件夹中放置一个图片文件。

  3. 使用字节流将img1文件夹中的图片文件复制到img2文件夹中。

**代码示例:**javaimport java.io.*;

public class CopyImage { public static void main(String[] args) { File srcDir = new File('D:\img1'); File destDir = new File('D:\img2'); if (!destDir.exists()) { destDir.mkdir(); } File[] files = srcDir.listFiles(); for (File file : files) { if (file.isFile()) { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream(file); fileOutputStream = new FileOutputStream(destDir + '' + file.getName()); byte[] bytes = new byte[1024]; int len; while ((len = fileInputStream.read(bytes)) != -1) { fileOutputStream.write(bytes, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } } }}

总结:

本文通过三个简单的例子介绍了Java IO流的基本使用,包括文件操作、读取文件、复制文件等。IO流是Java编程中非常重要的概念,掌握IO流的使用可以帮助我们进行各种文件操作

Java IO流实战:统计文件数量、读取文件内容、复制图片

原文地址: https://www.cveoy.top/t/topic/ogh2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录