• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Set offline attribute of a file in Java 7

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I change offline attribute of a file in Java 7?
I’m able to get attribute field using the following code:

But when I try to set it using the following code, it gives error ('dos:attributes' not recognized] exception):


Any help would be appreciated.
 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose it is safe to read attributes in bulk but unsafe to set them in bulk... which leads to setting specific attributes as a single operation such as:



For a full coverage of dealing with I/O in Java I recommend reading this book Pro Java 7 NIO.2
 
javed bhatti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm only interested in setting the offline attribute of a file. It is ntfs file attribute. Any method to achieve this?
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May I ask what attribute of the file would you like to set?

- lastModifiedTime
- lastAccessTime
- creationTime
- hidden
- readonly
- system
- archive
 
javed bhatti
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
File Attribute Constants:
http://msdn.microsoft.com/en-us/library/windows/desktop/gg258117%28v=vs.85%29.aspx

This document also lists FILE_ATTRIBUTE_OFFLINE 4096 (0x1000) that I’m interested in.

I can get this attribute with:


And then combining it with FILE_ATTRIBUTE_OFFLINE (0x00001000) using bitwise operator tells whether the file is offline or online:


Other file properties like readonly, system, hidden can also be obtained by combining appropriate constants with the file attribute field.

The follow code demonstrate the use of this field in getting different file properties:



It gave me following results:

file: D:\MyDir\ac.pdf
------------------
lastModifiedTime: 2014-01-16T06:56:00Z
lastAccessTime: 2014-04-16T13:56:11.674773Z
isSymbolicLink: false
size: 927608
system: false
fileKey: null
isDirectory: false
hidden: false
isOther: false
isRegularFile: true
creationTime: 2014-04-16T13:56:11.674773Z
attributes: 33
archive: true
readonly: true
------------------
dos:attributes: 33
offline: false
online: true
readonly: true
hidden: false
system: false
directory: false
archive: true

You can see I am able to correctly translate attributes field to get different file properties.

Now only if I am able to set/modify the file attributes field (basically only interested in offline/online flag) that would be great.

Thanks.
 
author & internet detective
Posts: 41861
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if you can. Java has a DosFileAttributes view of attributes you can set and offline isn't one of them.

Files.getAttribute(file.toPath(), "dos:attributes")

This question was crossposted. Sharing the link in case someone there knows the answer so we can all learn something.
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic