赞
踩
如果是读取xml文件,可以读取inputStream,再用XMLConfiguration处理
- import java.io.InputStream;
- import org.apache.commons.configuration.XMLConfiguration;
-
-
- // 由path获取inputStream
- InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
-
- // 由inputStream创建xml处理器
- XMLConfiguration xmlConfiguration = new XMLConfiguration();
- xmlConfiguration.load(in);
或则使用InputStreamReader逐行处理
- // 读
- InputStreamReader inReader = new InputStreamReader(new FileInputStream(path), "UTF8");
-
- BufferedReader buReader = new BufferedReader(inReader);
-
- try {
- String fromLine = null;
- while ((fromLine = buReader.readLine()) != null) {
- // 使用formLine
- }
-
- } finally{
- buReader.close();
- inReader.close();
- }
-
-
-
- // 写
- OutputStreamWriter outWriter = new OutputStreamWriter(new FileOutputStream(path),
- "UTF8");
-
- BufferedWriter buWriter = new BufferedWriter(outWriter);
-
- String toLine = "something to write";
-
- try {
- buWriter.write(toLine);
-
- } finally {
- buWriter.close();
- outWriter.close();
- }
- Enumeration<URL> dirs = Thread.currentThread().getContextClassLoader().getResources(dirPath);
-
- recursive = true;
-
- if (dirs.hasMoreElements()) {
- URL url = dirs.nextElement();
- String protocol = url.getProtocol();
-
- if ("file".equals(protocol)) {
- String filePath = URLDecoder.decode(url.getFile(), "UTF-8");
-
- File dir = new File(filePath);
- if (!dir.exists() || !dir.isDirectory()) {
- return;
- }
-
- File[] subDirs = dir.listFiles(new FileFilter() {
- @Override
- public boolean accept(File file) {
- return (recursive && file.isDirectory()) || (file.getName().endsWith(".class"));
- }
- });
-
- if (subDirs == null || subDirs.length == 0) {
- return;
- }
-
- for (File f : subDirs) {
- if (f.isDirectory()) {
- // 递归处理文件夹
- continue;
- }
-
- className = f.getName();
- className = packageName + "." + className.substring(0, className.length() - 6);
-
- // 处理文件
- Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
- }
- }
- }
或则递归访问所有文件及子文件
- handle(File file) {
- if (file.isFile) {
- // 只处理.java结尾的代码文件
- if (!file.getPath().endsWith(".java")) {
- return;
- }
-
- // 处理逻辑
-
- } else {
- File[] files = file.listFiles();
- if (files == null) {
- return;
- }
- for (File subFile : files) {
- handle(subFile);
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。