Set file attributes. : File « File « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » File » File 
11. 2. 38. Set file attributes.
import java.io.File;
import java.util.Calendar;
import java.util.Date;

public class Main {
  public static void main(String[] argvthrows Exception {
    File f = new File("name.txt");

    if (!f.exists()) {
      System.out.println("File not found.");
      return;
    }
    if (f.canRead())
      System.out.println("  Readable");
    else
      System.out.println("  Not Readable");

    if (f.canWrite())
      System.out.println("  Writable");
    else
      System.out.println("  Not Writable");
    System.out.println("  Last modified on " new Date(f.lastModified()));

    long t = Calendar.getInstance().getTimeInMillis();
    if (!f.setLastModified(t))
      System.out.println("Can't set time.");

    if (!f.setReadOnly())
      System.out.println("Can't set to read-only.");

    if (f.canRead())
      System.out.println("  Readable");
    else
      System.out.println("  Not Readable");

    if (f.canWrite())
      System.out.println("  Writable");
    else
      System.out.println("  Not Writable");
    System.out.println("  Last modified on " new Date(f.lastModified()));

    if (!f.setWritable(true, false))
      System.out.println("Can't return to read/write.");

    if (f.canRead())
      System.out.println("  Readable");
    else
      System.out.println("  Not Readable");

    if (f.canWrite())
      System.out.println("  Writable");
    else
      System.out.println("  Not Writable");
  }
}
11. 2. File
11. 2. 1. The File Class
11. 2. 2. Testing and Checking File Objects: file name and path
11. 2. 3. Providing a URI for a remote file
11. 2. 4. isAbsolute(): Returns true if the File object refers to an absolute pathname, and false otherwise.
11. 2. 5. getParent(): Returns the name of the parent directory of the file or directory represented
11. 2. 6. getParentFile(): Returns the parent directory as a File object, or null if this File object does not have a parent.
11. 2. 7. toString(): Returns a String representation of the current File object
11. 2. 8. exists(): Returns true if it exists and false otherwise
11. 2. 9. isDirectory(): Returns true if it is an existing directory and false otherwise
11. 2. 10. isFile(): Returns true if it is an existing file and false otherwise
11. 2. 11. isHidden(): Returns true if it is hidden and false otherwise
11. 2. 12. canRead(): Returns true if you are permitted to read the file and false otherwise.
11. 2. 13. canWrite(): Returns true if you are permitted to write to the file and false otherwise.
11. 2. 14. getAbsolutePath(): Returns the absolute path for the directory or file referenced by the current File object
11. 2. 15. getAbsoluteFile(): Returns a File object containing the absolute path for the directory or file referenced by the current File object.
11. 2. 16. list(): Returns a string array containing the children files and directories
11. 2. 17. listFiles(): Returns a File array containing the children files and directories
11. 2. 18. length(): Returns the length of current file in long
11. 2. 19. Create a human-readable file size
11. 2. 20. lastModified(): Returns last modified time in milliseconds since midnight on 1st January 1970 GMT
11. 2. 21. renameTo(File path): Rename a file or directory
11. 2. 22. setReadOnly(): Sets the file as read-only and returns true if the operation is successful
11. 2. 23. Filtering a File List
11. 2. 24. mkdir(): Creates a directory
11. 2. 25. mkdirs(): Creates a directory including any parent directories
11. 2. 26. createNewFile(): Creates a new empty file
11. 2. 27. createTempFile(String prefix, String suffix, File directory): a static method that creates a temporary file
11. 2. 28. delete(): delete the file or directory
11. 2. 29. Display File class constants and test some methods
11. 2. 30. deleteOnExit(): delete file or directory when the program ends
11. 2. 31. File Class Enhancements
11. 2. 32. Creates a file and sets it to read-only.
11. 2. 33. Create a file and change its attribute to readonly
11. 2. 34. List all roots
11. 2. 35. Get the free space
11. 2. 36. Get the usable space
11. 2. 37. Get the total space
11. 2. 38. Set file attributes.
11. 2. 39. Change a file attribute to writable
11. 2. 40. Change a file attribute to read only
11. 2. 41. Getting a Proper URL from a File Object
11. 2. 42. Get icon for file type
11. 2. 43. Get all xml files by file extension
11. 2. 44. Get file extension name
11. 2. 45. File.getCanonicalFile() converts a filename path to a unique canonical form suitable for comparisons.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.