Skip to content

Instantly share code, notes, and snippets.

@joakime
Last active December 10, 2015 04:48
Show Gist options
  • Save joakime/4383379 to your computer and use it in GitHub Desktop.
Save joakime/4383379 to your computer and use it in GitHub Desktop.
package os;
import java.io.File;
import java.io.IOException;
public class FileNullCheck
{
public static void main(String[] args)
{
File tmp = new File("a.jsp");
try
{
tmp.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
return;
}
String a = "a.jsp";
System.out.printf("a.jsp exists: %b (len=%d)%n",new File(a).exists(),a.length());
String anull = new String(new byte[] { 'a', '.', 'j', 's', 'p', 0x00 });
System.out.printf("a.jsp (null) exists: %b (len=%d)%n",new File(anull).exists(),anull.length());
String anullx = new String(new byte[] { 'a', '.', 'j', 's', 'p', 0x00, 'x' });
System.out.printf("a.jsp (nullx) exists: %b (len=%d)%n",new File(anullx).exists(),anullx.length());
}
}
/* returns the following
a.jsp exists: true (len=5)
a.jsp (null) exists: true (len=6)
a.jsp (nullx) exists: true (len=7)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment