how to File.listFiles in alphabetical order?

The listFiles method, with or without a filter does not guarantee any order. It does, however, return an array, which you can sort with Arrays.sort(). File[] files = XMLDirectory.listFiles(filter_xml_files); Arrays.sort(files); for(File _xml_file : files) { … } This works because File is a comparable class, which by default sorts pathnames lexicographically. If you want to … Read more

Converting transparent gif / png to jpeg using java

For Java 6 (and 5 too, I think): BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); g = bufferedImage.createGraphics(); //Color.WHITE estes the background to white. You can use any other color g.drawImage(image, 0, 0, bufferedImage.getWidth(), bufferedImage.getHeight(), Color.WHITE, null);

How to get just the parent directory name of a specific file

Use File‘s getParentFile() method and String.lastIndexOf() to retrieve just the immediate parent directory. Mark’s comment is a better solution thanlastIndexOf(): file.getParentFile().getName(); These solutions only works if the file has a parent file (e.g., created via one of the file constructors taking a parent File). When getParentFile() is null you’ll need to resort to using lastIndexOf, … Read more

Getting java.net.SocketTimeoutException: Connection timed out in android

I’ve searched all over the web and after reading lot of docs regarding connection timeout exception, the thing I understood is that, preventing SocketTimeoutException is beyond our limit. One way to effectively handle it is to define a connection timeout and later handle it by using a try-catch block. Hope this will help anyone in … Read more

Streaming large files in a java servlet

When possible, you should not store the entire contents of a file to be served in memory. Instead, aquire an InputStream for the data, and copy the data to the Servlet OutputStream in pieces. For example: ServletOutputStream out = response.getOutputStream(); InputStream in = [ code to get source input stream ]; String mimeType = [ … Read more