Skip to content

Instantly share code, notes, and snippets.

@joakime
Created January 2, 2013 23:08
Show Gist options
  • Save joakime/4439207 to your computer and use it in GitHub Desktop.
Save joakime/4439207 to your computer and use it in GitHub Desktop.
package os;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileNullCheck
{
public static void main(String[] args) throws Exception
{
File tmp = new File("a.txt");
try
{
tmp.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
return;
}
String a = "a.txt";
testExists("a.txt", a);
String anull = new String(new byte[] { 'a', '.', 't', 'x', 't', 0x00 }, "UTF-8");
testExists("a.txt (null)", anull);
String anullx = new String(new byte[] { 'a', '.', 't', 'x', 't', 0x00, 'x' }, "UTF-8");
testExists("a.txt (nullx)", anullx);
}
private static void testExists(String label, String filename) throws IOException
{
File file = new File(filename);
File canonical = file.getCanonicalFile();
boolean normalizedName = filename.length() != file.getCanonicalFile().getName().length();
boolean canonName = file.getName().equals(canonical.getName());
System.out.printf("%s exists: %b%n", label, file.exists());
System.out.printf(" filename.length = %d%n", filename.length());
System.out.printf(" normalized changed filename length = %b%n", normalizedName);
System.out.printf(" canonName = %b%n",canonName);
Path path = Paths.get(file.toURI());
boolean symlink = Files.isSymbolicLink(path);
System.out.printf(" nio issymlink = %b%n",symlink);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment