Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

How to open, read, write, close file(s) in Java? Examples on move, rename and de

JavaFAQ Home » General Java Go to all tips in General Java

Bookmark and Share

Question: How can I open, read, write and close files in Java? Can I move, rename and delete file in Java? Examples? Can I filter files by file's extension?

Answer: Yes you can do all usual operations with files in Java: open, read, write, close, move, rename and delete.

You can even filter files by file's extension. All the file operations most easy to do in Java application. By default it is not for applets. If you try to open a file by applet you will get such or similar error (depends on Java version):

java.security.AccessControlException: access denied (java.io.FilePermission yourfilename read) at java.security.AccessControlContext. checkPermission(AccessControlContext.java:270) at java.security.AccessController. checkPermission(AccessController.java:401) at java.lang.SecurityManager. checkPermission(SecurityManager.java:542) at java.lang.SecurityManager. checkRead(SecurityManager.java:887)

It is possible to open files with applet but user must explicitly permit it to Security manager (you write this functionality). So if you write a program which runs locally it is easier to write an application instead of applet.

file read java code can be buggy

File operations are often OS dependant. You code which works on Windows can stop to work on UNIX-like OS's due to higher security. Files which are in mounted directories (look like they are here on local file system, but are far away on the other side of network) also can cause many troubles.. So be ready to test all the cases if you write your code for different OS's and file systems.

During my latest work as Java programmer I collected a lot of useful examples for files related operations. They all working well and can give you good idea how to use file operations in Java.

By the way, I have more than 1000 Java examples on other areas of Java as well. My principle is: only working Java Code Examples. Some examples require few more classes, you can find them by powerful Java Code Search - supports boolean search, exact pattern and more.

So please take a look at File related code examples below:

Determine If a Filename Path is a File or a Directory
Converting Between Filename Path and a URL
Getting an Absolute Filename Path from a Relative Filename Path
Determining If Two Filename Paths Refer to the Same File
Getting the Parents of a Filename Path
Traversing a Directory
Listing the File System Roots
List the Files or Subdirectories in a Directory
Deleting a Directory
Creating a Directory
Getting and Setting the Modification Time of a File or Directory
Moving a File or Directory to Another Directory
Renaming a File or Directory
Creating a Temporary File
Getting the Size of a File
Creating a File
Determining If a File or Directory Exists
Using a Random Access File
Reading a File into a Byte Array
CLOB Example
BLOB Example
BFILE File Example
JNDI - File Systems Demo 1
Security Manager / Access Controller
Simple (Save / Load) Serialization Example
Property Files
Calculating the Checksum of a File / CRC-32
Calculating the Checksum of a File / Adler-32
Uncompressing File in GZIP Format
Compress File to GZIP Format
JAXP Example (Uses default parser - which is Crimson)
Crimson XML Parser Example (DOM)
Xerces2 XML Parser Example (DOM)
Oracle XML Parser Example (DOM)
SAX Example (Generic)
DOM Example (Generic)
Java Statement Runner
Compare File Dates
PrintFile -- Print a file named onthe command line
Create a file with holes in it.
A file searching class, similar to grep, which returns information about lines matched in the specified files.
Test behavior of Memory mapped buffer types.
DOMSerializer will take a DOM tree and serialize that tree.
UpdateItemServlet demonstrates how the DOM Level 2 Range module can be used for handling range within a do*****ent.
estSAXParsing is a simple class that demonstrates how JAXP can be used to work with SAX (the Simple API for XML).
ItemSearcher shows how the DOM Level 2 Traversal module can be used for searching through a do*****ent.

You can also look at examples for FileReader, FileWriter, FileDescriptor, FileFilter, FilenameFilter, FileInputStream, FileOutputStream, FileNotFoundException




 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy