「Tomcat」ファイル読み込みいろいろ
-
-
-
- -
-
-
「Tomcat」ファイル読み込みいろいろ
-
-
-
- -
-
-
Tomcat でファイル読み込みやってみました。その時のメモ。
[ 環境情報 ]
CentOS 6.2
Apache Tomcat 6.0.32
Java SE 6 Update 34
■ テキストファイルの読み込みサンプル ( WEB-INF/file/test.txt の読み込み )
package read.file; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ReadFileServlet extends HttpServlet { private static final long serialVersionUID = 1L; public ReadFileServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext context = getServletContext(); String path = context.getRealPath("WEB-INF/file/test.txt"); BufferedReader br = new BufferedReader(new FileReader(path)); StringBuffer sb = new StringBuffer(); String line = null; while ((line = br.readLine()) != null) { sb.append(line); sb.append("<br>"); } br.close(); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<body>"); out.println(sb.toString()); out.println("</body>"); out.println("</html>"); } }
■ プロパティファイルの読み込みサンプル
( パッケージ read.prop 内の test1.properties、および、WEB-INF/prop/test2.properties の読み込み )
package read.prop; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ReadPropServlet extends HttpServlet { private static final long serialVersionUID = 1L; public ReadPropServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { InputStream is1 = ReadPropServlet.class.getClassLoader() .getResourceAsStream("read/prop/test1.properties"); Properties prop1 = new Properties(); prop1.load(is1); InputStream is2 = getServletContext().getResourceAsStream( "WEB-INF/prop/test2.properties"); Properties prop2 = new Properties(); prop2.load(is2); is1.close(); is2.close(); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<body>"); out.println("***** read/prop/test1.properties *****"); out.println("<br>"); out.println("key1 : " + prop1.getProperty("key1")); out.println("<br>"); out.println("key2 : " + prop1.getProperty("key2")); out.println("<br>"); out.println("key3 : " + prop1.getProperty("key3")); out.println("<br>"); out.println("***** read/prop/test2.properties *****"); out.println("<br>"); out.println("key4 : " + prop2.getProperty("key4")); out.println("<br>"); out.println("key5 : " + prop2.getProperty("key5")); out.println("<br>"); out.println("key6 : " + prop2.getProperty("key6")); out.println("<br>"); out.println("</body>"); out.println("</html>"); } }
test1.properties
key1=value1 key2=value2 key3=value3
test2.properties
key4=value4 key5=value5 key6=value6
以上です。