import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; public class Main { private static String pathname = "E:/java"; /* 0:48 1:49 2:50 */ private static byte yellow = 48; private static byte white = 49; private static byte black = 50; private static int yellowNum = 0; private static int whiteNum = 0; private static int blackNum = 0; static void writeDate(File directoryFile, int flag) { for (File textFile : directoryFile.listFiles()) { // 创建基于文件的输出流 try (FileOutputStream fis = new FileOutputStream(textFile)) { if (textFile.isFile() && textFile.getName().endsWith(".txt")) { // 把数据写入到输出流并计数 switch (flag) { case 0: fis.write(yellow); yellowNum++; break; case 1: fis.write(white); whiteNum++; break; case 2: fis.write(black); blackNum++; break; default: break; } } } catch (IOException e) { } } } public static void main(String[] args) { SimpleDateFormat sdf =new SimpleDateFormat("yyyy年MM月dd日 EEE HH:mm:ss"); Date date = new Date(); System.out.println("program start!date: "+sdf.format(date)); System.out.println("*****************************************"); File f = new File(pathname); if (f.isDirectory()) { for (File directoryFile : f.listFiles()) { if (directoryFile.isDirectory()){ switch (directoryFile.getName()){ case "yellow" : writeDate(directoryFile, 0); break; case "white" : writeDate(directoryFile, 1); break; case "black" : writeDate(directoryFile, 2); break; default: break; } } } } System.out.println("program end!黄色人种图片:"+yellowNum+",白色人种图片:"+whiteNum+",黑人种图片:"+blackNum); } }
测试用
最新推荐文章于 2023-07-05 19:44:38 发布