Results 1 to 1 of 1
-
How to read a text file from a Java Archive File
The code snippet below shows how one can read a text file packed in a JAR file.
Java Code:public class FileUtils{ public static List<String> readTextFromJar(String s) { InputStream is = null; BufferedReader br = null; String line; ArrayList<String> list = new ArrayList<String>(); try { is = FileUtils.class.getResourceAsStream(s); br = new BufferedReader(new InputStreamReader(is)); while (null != (line = br.readLine())) { list.add(line); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); if (is != null) is.close(); } catch (IOException e) { e.printStackTrace(); } } return list; } public static void main(String args[]) throws IOException{ List<String> list = FileUtils.readTextFromJar("/datafile1.txt"); Iterator<String> it = list.iterator(); while(it.hasNext()) { System.out.println(it.next()); } list = FileUtils.readTextFromJar("/test/datafile2.txt"); it = list.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
Similar Threads
-
help to read .pst file through java
By umadas in forum Advanced JavaReplies: 2Last Post: 12-19-2010, 04:32 AM -
How to print text file in java(dotmatrix printer)
By yoganeethi in forum Advanced JavaReplies: 4Last Post: 12-01-2010, 01:45 PM -
count character in text file as input file
By aNNuur in forum New To JavaReplies: 7Last Post: 03-25-2010, 04:01 PM -
Extract Text from PDF File using java
By TSW1016 in forum Advanced JavaReplies: 5Last Post: 01-06-2008, 11:03 PM -
Converting text file(.txt) to JPG file(.jpg) in java
By javadeveloper in forum Advanced JavaReplies: 0Last Post: 11-09-2007, 04:22 PM
Bookmarks