题目一:

import 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 + "个"); } }

题目二:

import 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(); } } } } }

题目三:

import 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(); } } } } } }

使用io流完成题目一统计一个文件夹中每种文件的个数并打印。打印格式如下:txt3个doc4个jpg6个…题目二在你计算机的一个盘比如D盘根目录下创建一个文件名字叫做file01txt。保存以下内容要求是单字节字符比如字母或者数字file01txt1 abcdef123456要求:1 使用字节输入流一次读取一个字节的方法将file01txt的文件读取并打印2 使用字节输入流一次读取多个字节的方法将f

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

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