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

java.io.Console support in Eclipse IDE

I assume you want to be able to use step-through debugging from Eclipse. You can just run the classes externally by setting the built classes in the bin directories on the JRE classpath. java -cp workspace\p1\bin;workspace\p2\bin foo.Main You can debug using the remote debugger and taking advantage of the class files built in your project. … Read more

How to list the files inside a JAR file?

CodeSource src = MyClass.class.getProtectionDomain().getCodeSource(); if (src != null) { URL jar = src.getLocation(); ZipInputStream zip = new ZipInputStream(jar.openStream()); while(true) { ZipEntry e = zip.getNextEntry(); if (e == null) break; String name = e.getName(); if (name.startsWith(“path/to/your/dir/”)) { /* Do something with this entry. */ … } } } else { /* Fail… */ } Note that … Read more

Reading a simple text file

Place your text file in the /assets directory under the Android project. Use AssetManager class to access it. AssetManager am = context.getAssets(); InputStream is = am.open(“test.txt”); Or you can also put the file in the /res/raw directory, where the file will be indexed and is accessible by an id in the R file: InputStream is … Read more

java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException

Just implement Serializable If you’re getting a NotSerializableException like follows, java.io.NotSerializableException: bean.ProjectAreaBean then it simply means that the class as identified by the fully qualified name in the exception message (which is bean.ProjectAreaBean in your case) does not implement the Serializable interface while it is been expected by the code behind. Fixing it is relatively … Read more

How to get the current working directory in Java?

Code : public class JavaApplication { public static void main(String[] args) { System.out.println(“Working Directory = ” + System.getProperty(“user.dir”)); } } This will print the absolute path of the current directory from where your application was initialized. Explanation: From the documentation: java.io package resolve relative pathnames using current user directory. The current directory is represented as … Read more