Wednesday, December 26, 2012

Read Jar from java

Read the jar file from java. It's very usefull if u wanna get some file content and this file is packed inti the jar. See this example.


public static void main(String args[]) {
  String relativeFilePath = "style/someCSSFile.css";
  String zipFilePath = "/someDirectory/someWarFile.war";
  String contents = readZipFile(zipFilePath, relativeFilePath);
  System.out.println(contents);
}

public static String readZipFile(String zipFilePath, String relativeFilePath) {
  try {
    ZipFile zipFile = new ZipFile(zipFilePath);
    Enumeration<? extends ZipEntry> e = zipFile.entries();
    
    while (e.hasMoreElements()) {
      ZipEntry entry = (ZipEntry) e.nextElement();
      // if the entry is not directory and matches relative file then
      // extract it
      if (!entry.isDirectory() && entry.getName().equals(relativeFilePath)) {
        BufferedInputStream bis = new BufferedInputStream(
          zipFile.getInputStream(entry));
        // Read the file
        // With Apache Commons I/O
        String fileContentsStr = IOUtils.toString(bis, "UTF-8");
        
        // With Guava
        // String fileContentsStr = new
        // String(ByteStreams.toByteArray(bis),Charsets.UTF_8);
        // close the input stream.
        bis.close();
        return fileContentsStr;
      } else {
       continue;
      }
    }
  } catch (IOException e) {
   logger.error("IOError :" + e);
   e.printStackTrace();
  }
  return null;
}

Monday, December 3, 2012

Read file content with Java (+ Maven )

Simple way to get the file content using java, get it now:

1. Standard Java way to read the file content. There are two methods for different Java versions. readFileJav6 is appropriate for Java 7 as well, but I'd like to recommend you to improve it with new try-with-resources block (see more: http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)

public List<String> readFileJava7(String filename) {
 List<String> content = new LinkedList<String>();
 Path filePath = Paths.get(filename);
 try {
  content = Files.readAllLines(filePath, Charset.defaultCharset());
  //OR
  // String contents = new String(Files.readAllBytes(
Paths.get("alice.txt")), StandardCharsets.UTF_8);
 } catch (IOException e) {
  System.out.println("Unable to read the file " + filename);
 }
 return content;
}

public List<String> readFileJava6(String filename) {
 List<String> content = new LinkedList<String>();
 FileReader fr;
 BufferedReader br = null;
 try {
  fr = new FileReader(filename);
  br = new BufferedReader(fr);
  String output;
  while ((output = br.readLine()) != null) {
   content.add(output);
  }
 } catch (FileNotFoundException e) {
  System.out.println("Unable to find the file " + filename);
 } catch (IOException e) {
  System.out.println("Unable to read the file " + filename);
 } finally {
  try {
   br.close();
  } catch (IOException e) {
   System.out.println("Couldn't close the file " + filename);
  }
 }
 return content;
}

2. Apache Commons IO

String content = IOUtils.toString(new FileReader("file.txt"), "utf-8");


<dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-io</artifactId>
 <version>1.3.2</version>
</dependency>


 

3. Google Guava

String content = Files.toString(new File("file.txt"), Charsets.UTF_8);


<dependency>
 <groupId>com.google.guava</groupId>
 <artifactId>guava</artifactId>
 <version>r05</version>
</dependency>