财高八斗Java的后缀分类_怎么⽤JAVA实现根据⽂件后缀名分类⽂件,并且将⽂件复制到不同的⽂件夹...
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import javax.swing.JFileChoor;public class FileDemo { public static void main(String[] args) { File sourcePath = getSourcePath(); File outputPath = getOutputPath(); handlePath(sourcePath, outputPath); } private static void handlePath(File sourcePath, File outputPath) { if (sourcePath == null || outputPath == null) return; for (File file : sourcePath.listFiles()) { if (file.isDirectory()) { handlePath(file, outputPath); } el { String fileName = Name(); if (ains(".")) { String suffix = fileName.substring(fileName.lastIndexOf('.') + 1); copy(file, new File(outputPath, suffix)); } el { copy(file, new File(outputPath, "nosuffix")); } } } } private static void copy(File sourceFile, File targetDir) { System.out.println("copying " + sourceFile); if (!ists()) { targetDir.mkdir(); } try { FileInputStream fis = new FileInputStream(sourceFile); FileOutputStream fos = new FileOutputStream(new File(targetDir, Name())); byte[] buf = new byte[102400]; int available = 0; while ((available = fis.available()) > buf.length) { ad(buf); fos.write(buf); }
e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private static File getSourcePath() { JFileChoor choor = new JFileChoor(); choor.tFileSelectionMode(JFileChoor.DIRECTORIES_ONLY); if
(choor.showOpenDialog(null) == JFileChoor.APPROVE_OPTION) { SelectedFile(); } return null; } private static File getOutputPath() { JFileChoor choor = new JFileChoor();
choor.tFileSelectionMode(JFileChoor.DIRECTORIES_ONLY); if (choor.showSaveDialog(null) ==系统崩溃
长方体图片JFileChoor.APPROVE_OPTION) { SelectedFile(); } return null; }}先选择来源⽂件夹,然后选择输出⽂件夹。
赚钱投资
然后等待即可。程序会遍历⼦⽬录。保卫胜利果实
花用英语怎么读取消
地怎么用
评论
天气英语