遍历目录修改文件后缀名

想要在手机上观看JavaApi,但是找了许久没要找到,只好自己把API转换成word文档查看

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class ChangeFile {
	public static String RootFile;
	public static String RootFilePath;
	public static String NewFilePath;
	public static String RootFileName;
	
	public static void main(String[] args) {
		String FileName = "E:\\APIword"+File.separator;
		File file = new File(FileName);
		long start = System.currentTimeMillis();
		ListFile(file);
		long end = System.currentTimeMillis();
		System.out.print(end-start);
	}

	private static void ListFile(File file) {
		if(file != null){
			//判断是否是文件
			if(file.isFile()){
				ChangeFile(file);
			}
			//遍历所有文件
			if(file.isDirectory()){			
				File[] File = file.listFiles();
				if(File!=null){
					for(int i=0; i<File.length; i++){
						ListFile(File[i]);
					}
				}
			}
		}
		
	}

	private static void DeleteFile(File file) {
		String filename = file.getName();
		if(file.getName().substring(file.getName().lastIndexOf(".")+1).equals("java")){
			file.delete();
			System.out.println(filename+"已成功删除");
		}
		
	}

	private static void ChangeFile(File file) {
		RootFile = file.getAbsolutePath();
		//原文件所在目录
		RootFilePath = RootFile.substring(0,RootFile.length()-file.getName().length());
		//原文件名
		RootFileName =  file.getName().substring(0,file.getName().length()-file.getName().substring(file.getName().lastIndexOf(".")).length());
		File File = new File(RootFilePath+RootFileName+".doc");//后缀名在这
		if(!File.exists()){
			try{
				File.createNewFile();
			}catch(Exception e){
				e.printStackTrace();
			}
		try{
			InputStream input = new FileInputStream(file);
			OutputStream output = new FileOutputStream(File);
			System.out.println("正在转换"+file.getName());
			if(input != null && output != null){
				int temp = 0;
				while((temp = input.read()) != (-1)){
					output.write(temp);
				}
			}
			System.out.println("转换成功!");
			input.close();
			output.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		}
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值