标签:
package cn.edu.test; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class Writer { public static void main(String[] args) throws IOException { System.out.println(read()); } public static String read() { // resource/a/b/test.txt在src目录下 InputStream resourceAsStream = Writer.class.getClassLoader().getResourceAsStream("resource/a/b/test.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream)); String readLine = null; try { readLine = br.readLine(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) try { br.close(); } catch (IOException e) { e.printStackTrace(); } } System.out.println(readLine); return readLine; } } |
package cn.edu.test; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; public class Writer { public static void main(String[] args) throws IOException { System.out.println(read()); } public static String read() { // resource/a/b/test.txt存放在src目录下 String path = Writer.class.getClassLoader().getResource("").getPath(); path += "resource/a/b/test.txt"; System.out.println(path); String readFileToString = null; try { readFileToString = FileUtils.readFileToString(new File(path)); } catch (IOException e) { e.printStackTrace(); } return readFileToString; } } |
Java Project打包,发布到Web Project下之后,运行Web Project出现找不到Java Project中资源的解决办法
标签:
原文地址:http://blog.csdn.net/shijiebei2009/article/details/42246705