@Test public void testCreateFile() throws IOException { if(!isWindows()) return; File f = File.createTempFile("testCreateFile", ""); f.deleteOnExit(); File d = new File(f.getParentFile(), f.getName()+".dir"); d.mkdir(); d.deleteOnExit(); File asciiFile = new File(d, "test"); String asciiFileName = asciiFile.getAbsolutePath(); int desiredAccess = Windows.GENERIC_WRITE; int shareMode = 0; Windows.SECURITY_ATTRIBUTES securityAttributes = null; int creationDisposition = Windows.CREATE_NEW; int flagsAndAttributes = 0; long templateFileHandle = 0L; long handle = Windows.CreateFile(asciiFileName, desiredAccess, shareMode, securityAttributes, creationDisposition, flagsAndAttributes, templateFileHandle); assertFalse(handle == Windows.INVALID_HANDLE_VALUE); assertTrue(asciiFile.exists()); boolean couldCloseHandle = Windows.CloseHandle(handle); assertTrue(couldCloseHandle); long handle2 = Windows.CreateFile(asciiFileName, desiredAccess, shareMode, securityAttributes, creationDisposition, flagsAndAttributes, templateFileHandle); assertTrue(handle2 == Windows.INVALID_HANDLE_VALUE); int error = Windows.GetLastError(); assertTrue(error == Windows.ERROR_FILE_EXISTS); File unicodeFile = new File(d, "test\u00e0"); String unicodeFileName = unicodeFile.getAbsolutePath(); long handle3 = Windows.CreateFile(unicodeFileName, desiredAccess, shareMode, securityAttributes, creationDisposition, flagsAndAttributes, templateFileHandle); assertFalse(handle3 == Windows.INVALID_HANDLE_VALUE); assertTrue(unicodeFile.exists()); unicodeFile.delete(); asciiFile.delete(); }
static native long CreateFile(String fileName, int desiredAccess, int shareMode, SECURITY_ATTRIBUTES securityAttributes, int creationDisposition, int flagsAndAttributes, long templateFileHandle);
Windows { static native long CreateFile(String fileName, int desiredAccess, int shareMode, SECURITY_ATTRIBUTES securityAttributes, int creationDisposition, int flagsAndAttributes, long templateFileHandle); }
Windows { static native long CreateFile(String fileName, int desiredAccess, int shareMode, SECURITY_ATTRIBUTES securityAttributes, int creationDisposition, int flagsAndAttributes, long templateFileHandle);  }
Windows { static native long CreateFile(String fileName, int desiredAccess, int shareMode, SECURITY_ATTRIBUTES securityAttributes, int creationDisposition, int flagsAndAttributes, long templateFileHandle);   }
Windows { static native long CreateFile(String fileName, int desiredAccess, int shareMode, SECURITY_ATTRIBUTES securityAttributes, int creationDisposition, int flagsAndAttributes, long templateFileHandle);   static final int GENERIC_READ; static final int GENERIC_WRITE; static final int GENERIC_EXECUTE; static final int GENERIC_ALL; static final int FILE_LIST_DIRECTORY; static final int FILE_READ_DATA; static final int FILE_ADD_FILE; static final int FILE_WRITE_DATA; static final int FILE_ADD_SUBDIRECTORY; static final int FILE_APPEND_DATA; static final int FILE_CREATE_PIPE_INSTANCE; static final int FILE_READ_EA; static final int FILE_READ_PROPERTIES; static final int FILE_WRITE_EA; static final int FILE_WRITE_PROPERTIES; static final int FILE_EXECUTE; static final int FILE_TRAVERSE; static final int FILE_DELETE_CHILD; static final int FILE_READ_ATTRIBUTES; static final int FILE_WRITE_ATTRIBUTES; static final int FILE_SHARE_READ; static final int FILE_SHARE_WRITE; static final int FILE_SHARE_DELETE; static final int CREATE_NEW; static final int CREATE_ALWAYS; static final int OPEN_EXISTING; static final int OPEN_ALWAYS; static final int TRUNCATE_EXISTING; static final int FILE_ATTRIBUTE_READONLY; static final int FILE_ATTRIBUTE_TEMPORARY; static final int FILE_ATTRIBUTE_SYSTEM; static final int FILE_ATTRIBUTE_OFFLINE; static final int FILE_ATTRIBUTE_NORMAL; static final int FILE_ATTRIBUTE_HIDDEN; static final int FILE_ATTRIBUTE_ENCRYPTED; static final int FILE_ATTRIBUTE_ARCHIVE; static final int FILE_FLAG_BACKUP_SEMANTICS; static final int FILE_FLAG_DELETE_ON_CLOSE; static final int FILE_FLAG_NO_BUFFERING; static final int FILE_FLAG_OPEN_NO_RECALL; static final int FILE_FLAG_OPEN_REPARSE_POINT; static final int FILE_FLAG_OVERLAPPED; static final int FILE_FLAG_POSIX_SEMANTICS; static final int FILE_FLAG_RANDOM_ACCESS; static final int FILE_FLAG_SEQUENTIAL_SCAN; static final int FILE_FLAG_WRITE_THROUGH; static final long INVALID_HANDLE_VALUE; static final int ERROR_ALREADY_EXISTS; static final int ERROR_FILE_NOT_FOUND; static final int ERROR_ACCESS_DENIED; static final int ERROR_FILE_EXISTS; static final int ERROR_INVALID_FUNCTION; static final int ERROR_OPERATION_ABORTED; static final int ERROR_IO_INCOMPLETE; static final int ERROR_IO_PENDING; }