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);

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(
        // 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.
        return fileContentsStr;
      } else {
  } catch (IOException e) {
   logger.error("IOError :" + e);
  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:

public List<String> readFileJava7(String filename) {
 List<String> content = new LinkedList<String>();
 Path filePath = Paths.get(filename);
 try {
  content = Files.readAllLines(filePath, Charset.defaultCharset());
  // 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) {
 } 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 {
  } 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");



3. Google Guava

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


Tuesday, October 30, 2012

Mercurial: change the commit message

I used to type the commit message for my mercurial operation with mistake typing incorrect modifier of my activity. If the server doesn't know this activity Id, my push will be rejected, Sad but true.

There is an article below about how to change the commit message and make developer happy.
It helps me to get rid of my promlems. Although, author didn't mention in this article that the user name must be also specified in hgrc. Without username I was surprised with the message: abort: no username supplied (see "hg help config")

Thursday, October 11, 2012

Proxy settings for Ubuntu, Mint and others

I established the external connetion to the web for my virtual OS twice, and every time I had to set up proxy settings. That's a reminder which will be helpful to get rid of another one search in the web with a question like "ubuntu update proxy settings" or "mint configure proxy settings".

Wednesday, January 11, 2012

ASN.1, 3GPP notice and what we can do with that. PART 1

I'd like to describe the basic principles of ASN.1 Message structure processing. I used to work with it a long time ago when I was a member of a team working with messages extracted from UMTS Radio Network Controllers (RNC). Me and my collegaues developed the desktop application that provides an information obtained from RNC in user-friendly format such as graphics, tables, some views that aggregate the info and many many more.