HTML/JavaScript小工具

HTML/JavaScript小工具

2013年5月6日 星期一

OpenOptions Parameter

READ and TRUNCATE_EXISTING (or WRITE, APPEND, or DELETE_ON_CLOSE) cannot go together.
 READ and SYNC (or DSYNC) cannot go together either because when a file is opened for READ, there is nothing to synch.


CREATE and READ do not make sense if put together and therefore


new OpenOption[]{StandardOpenOption.READ, StandardOpenOption.APPEND}

new OpenOption[]{StandardOpenOption.APPEND, StandardOpenOption.TRUNCATE_EXISTING}

new OpenOption[]{StandardOpenOption.READ, StandardOpenOption.SYNC}

以上都是無效的配對組合
  • WRITE – Opens the file for write access.
  • APPEND – Appends the new data to the end of the file. This option is used with the WRITE or CREATE options.不可與READ or TRUNCATE_EXISTING options一起使用!

  • TRUNCATE_EXISTING – Truncates the file to zero bytes. This option is used with the WRITE option.//先將檔案清空在寫入
  • CREATE_NEW – Creates a new file and throws an exception if the file already exists.//如建立的檔案已存在會產生Exception
  • CREATE – Opens the file if it exists or creates a new file if it does not.//
  • 使用在write時會將第一筆資料欄位作取代的動作
  • 有相同檔案時不會拋出Exception
  • DELETE_ON_CLOSE – Deletes the file when the stream is closed. This option is useful for temporary files.//當關閉連線時將檔案刪除
  • SPARSE – Hints that a newly created file will be sparse. This advanced option is honored on some file systems, such as NTFS, where large files with data "gaps" can be stored in a more efficient manner where those empty gaps do not consume disk space.
  • SYNC – Keeps the file (both content and metadata) synchronized with the underlying storage device.
  • DSYNC – Keeps the file content synchronized with the underlying storage device.
SYNC requires that all data--SYNC 會同時將meta data跟資料一起寫到硬碟
SYNC makes sure that both - the data and meta data are synchronized with the storage device. Thus, it makes files operations even slower than DSYNC option.

DSYNC -->只會將當案寫到硬碟
DSYNC keeps only the file content and not the file meta data synchronized with the underlying storage device.
用法如下:
Path path = ...
     byte[] bytes = ...
     Files.write(path, bytes, StandardOpenOption.APPEND);

預設使用 StandardOpenOption.CREATE;
StandardOpenOption.TRUNCATE_EXISTING;

1 則留言:

  1. 你好,小弟在做實作產出一個log,部份資料如下
    1af46000: Returning from NtOpenFile (success), handle=110, filename=\??\C:\Program Files\Common Files\Microsoft Shared\office12\mso.dll, OpenOptions=4021
    1af46000: Returning from NtOpenFile (success), handle=110, filename=\??\C:\Program Files\Common Files\Microsoft Shared\office12, OpenOptions=4021
    1af46000: Returning from NtOpenFile (success), handle=110, filename=\??\C:\Program Files\Common Files\Microsoft Shared\office12\mso.dll, OpenOptions=4021
    1af46000: Returning from NtOpenFile (success), handle=110, filename=\??\C:\Program Files\Common Files\Microsoft Shared\office12\*.*, OpenOptions=4021
    其中OpenOptions=4021我不了解是什麼意思,google到您這篇文章,方便請問這是什麼意思嘛? 謝謝!!!

    回覆刪除