源码手机阅读工具

今天想在手机上看Apache一些项目的源码的,发现手机自带的软件不支持查看java文件,又不想浪费手机内存空间装其他阅读器,就自己写了个把java项目中的源码文件转成text文件的程序,把一个项目的所有java文件转化好后发到手机上,坐公交的时候看看。道理很简单,IO流+字符串处理+递归。注意原来的代码一定要备份!!!

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author 细肥尸丁
 * @version V1.0
 * @Title: FileConverter
 * @Description: 文件转化器
 * @date 2018/3/31 下午 3:15
 */
public class FileConverter {

	private final static String SUFFIX_JAVA = "java";
	private final static String SUFFIX_JS = "js";
	private final static String SUFFIX_CSS = "css";
	private final static String SUFFIX_XML = "xml";
	private final static String SUFFIX_JSP = "jsp";
	private final static String SUFFIX_SQL = "sql";
	private final static String SUFFIX_PROPERTIES = "properties";

	private final static int DEFAULT_BUFFER_SIZE = 1024;

	/**
	 * 将以上文件类型转化为text文件
	 * @Title convertAllFile 
	 * @Description  
	 * @param file
	 * @return void 
	 * @throws IOException
	 */
	public static void convertAllFile(File file) throws IOException {
		convertFile(file, SUFFIX_JAVA, SUFFIX_JS, SUFFIX_CSS, SUFFIX_XML, SUFFIX_JSP, SUFFIX_SQL, SUFFIX_PROPERTIES);
	}

	/**
	 * 将java文件转化为text文件
	 * @Title convertJavaFile 
	 * @Description  
	 * @param file
	 * @return void 
	 * @throws IOException
	 */
	public static void convertJavaFile(File file) throws IOException {
		convertFile(file, SUFFIX_JAVA);
	}

	/**
	 * 将js文件转化为text文件
	 * @Title convertJavaFile 
	 * @Description  
	 * @param file
	 * @return void 
	 * @throws IOException
	 */
	public static void convertJSFile(File file) throws IOException {
		convertFile(file, SUFFIX_JS);
	}

	/**
	 * 将css文件转化为text文件
	 * @Title convertJavaFile 
	 * @Description  
	 * @param file
	 * @return void 
	 * @throws IOException
	 */
	public static void convertCSSFile(File file) throws IOException {
		convertFile(file, SUFFIX_CSS);
	}

	/**
	 * 将xml文件转化为text文件
	 * @Title convertJavaFile 
	 * @Description  
	 * @param file
	 * @return void 
	 * @throws IOException
	 */
	public static void convertXMLFile(File file) throws IOException {
		convertFile(file, SUFFIX_XML);
	}

	/**
	 * 将jsp文件转化为text文件
	 * @Title convertJavaFile 
	 * @Description  
	 * @param file
	 * @return void 
	 * @throws IOException
	 */
	public static void convertJSPFile(File file) throws IOException {
		convertFile(file, SUFFIX_JSP);
	}

	/**
	 * 将sql文件转化为text文件
	 * @Title convertJavaFile 
	 * @Description  
	 * @param file
	 * @return void 
	 * @throws IOException
	 */
	public static void convertSQLFile(File file) throws IOException {
		convertFile(file, SUFFIX_SQL);
	}

	/**
	 * 将properties文件转化为text文件
	 * @Title convertJavaFile 
	 * @Description  
	 * @param file
	 * @return void 
	 * @throws IOException
	 */
	public static void convertPropertiesFile(File file) throws IOException {
		convertFile(file, SUFFIX_PROPERTIES);
	}

	public static void convertFile(File file, String... fileSuffix) throws IOException {

		String name = file.getAbsolutePath();
		// 如果是目录,就递归转化
		if (file.isDirectory()) {
			String[] list = file.list();
			for (String string : list) {
				convertFile(new File(name + "/" + string), fileSuffix);
			}
		} else {
			// 如果是java文件就转化成txt文件
			int i = name.lastIndexOf(".");
			if (i > 0) {
				String suffix = name.substring(i + 1);
				for (String string : fileSuffix) {
					if (string.equals(suffix)) {
						File tarFile = new File(name + ".txt");
						copyFile(file, tarFile);
						file.delete();
					}
				}
			}
		}
	}

	private static void copyFile(File srcFile, File destFile) throws IOException {
		if (destFile.exists() && destFile.isDirectory()) {
			throw new IOException("目标文件 '" + destFile + "' 存在但却是一个目录");
		}

		FileInputStream input = new FileInputStream(srcFile);
		FileOutputStream output = new FileOutputStream(destFile);
		byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
		int n = 0;
		while (-1 != (n = input.read(buffer))) {
			output.write(buffer, 0, n);
		}
		input.close();
		output.close();

		if (srcFile.length() != destFile.length()) {
			throw new IOException("复制失败");
		}
	}

	public static void main(String[] args) {
		try {
			FileConverter.convertAllFile(new File("E:\\apache"));// 填写项目的根目录或文件
			System.out.println("所有该格式的文件已转化成txt文件");
		} catch (IOException e) {
			System.out.println("无此文件或文件夹");
		} catch (IndexOutOfBoundsException e) {
			System.out.println("有的文件名瞎起");
		} catch (Exception e) {
			System.out.println("未知错误");
		}
	}
}

把文件路径改一下就能直接用了,支持多种格式

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值