KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > io > File

java.io
Class File

java.lang.Object
  extended by java.io.File
All Implemented Interfaces:
Serializable, Comparable<File>
See Also:
Top Examples, Source Code, separatorChar, separator

public boolean canRead()
See Also:
SecurityManager.checkRead(java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[11]Display content of a directory
By Anonymous on 2005/04/13 07:51:49  Rate
File directory = new File ( "C:\\JavaSource" ) ; 
 File [  ]  filesInDir = directory.listFiles (  ) ; 
  
  
 if  ( filesInDir != null )   {  
     int length = filesInDir.length; 
  
  
     for  ( int i = 0; i  <  length; ++i )   {  
         File f = filesInDir [ i ] ; 
         if  ( f.isFile (  )  )   {  
            if  (  f.canRead (  )   )  
               System.out.println (  "Can Read File: " + f.getName (  )  ) ; 
            else 
               System.out.println (  "Can NOT Read File: " + f.getName (  )  ) ; 
          }  
         else if  ( f.isDirectory (  )  )   {  
            System.out.println (  "Directory: " + f.getName (  )  ) ; 
          }  
  
  
      }  
  } 


public boolean canWrite()
See Also:
SecurityManager.checkWrite(java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[848]Check to see if a file is writable
By Praveen Chithari on 2004/09/28 17:17:25  Rate
public class ff  {  
  
  
   public static void main ( String [  ]  args )  throws Exception {  
     File af_FileToTest = new File ( "C:/my.JPG" ) ; 
     boolean lb_IsWritable = false; 
  
  
     if  ( af_FileToTest.exists (  )  )   {  
       lb_IsWritable = af_FileToTest.canWrite (  ) ; 
      }  else  {  
       throw new FileNotFoundException ( af_FileToTest + " not exists." ) ; 
      }  
  
  
     System.out.println ( lb_IsWritable ) ; 
  
  
    }  
  }  
 


public int compareTo(File pathname)
See Also:
Comparable, compareTo
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1707]use of method compareTo() in java.io.Files
By kaleem_bms { at } rediffmail { dot } com on 2006/02/07 03:42:45  Rate
 
 File fp1=new File ( "C:/Abdul/Kaleem/test1" ) ; 
 File fp2=new File ( "C:/Abdul/Kaleem/test2" ) ; 
  
  
 int value=fp1.compareTo ( fp2 ) ; 
  
  
 System.out.println ( "value of comparison between two test files is : " +value ) ; 
  
  
 


public int compareTo(Object o)
See Also:
Comparable, compareTo(File)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean createNewFile()
                      throws IOException
See Also:
SecurityManager.checkWrite(java.lang.String), SecurityException, FileLock
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[959]Create new file
By Raja Rizwan Khan (rajarizwan { at } hotmail { dot } com) on 2004/10/21 00:38:10  Rate
  public static void main  ( String [  ]  args )  
    {  
     String filePath="c:/tmp/foo.txt"; 
     File newFile = new File ( filePath ) ; 
     try 
      {  
       if ( newFile.createNewFile (  )  )  
        {  
         System.out.println ( "New File Created." ) ; 
        }  
       else 
        {  
         System.out.println ( "File already exists." ) ; 
        }  
      }  
     catch  ( IOException ioe )  
      {  
       System.out.println ( "IOException = " + ioe.getMessage (  )  ) ; 
      }  
    }  
 


public static File createTempFile(String prefix,
                                  String suffix)
                           throws IOException
See Also:
SecurityManager.checkWrite(java.lang.String), SecurityException, IllegalArgumentException, createTempFile(prefix, suffix, null)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[256]Exception handling
By Anonymous on 2003/05/17 13:24:16  Rate
//The code contains three methods that can all throw an exception. Each exceptional condition is handled by displaying a message. In the first case, the exception is rethrown for a second message for that problem to be displayed. 
  
  
  
 import java.io.*; 
  
  
 public class Exceptions  {  
  
  
   private static void fileAccess (  )  throws IOException  {  
     // Fails because prefix is too short 
     File f = File.createTempFile ( "x", "y" ) ; 
    }  
  
  
   private static void divZero (  )   {  
     System.out.println ( 1/0 ) ; 
    }  
  
  
   private static void arrayAccess ( String array [  ]  )   {  
     System.out.println ( "First: " + array [ 0 ]  ) ; 
    }  
  
  
   public static void main ( String args [  ]  )   {  
     try  {  
       try  {  
         fileAccess (  ) ; 
        }  catch  ( Exception e )   {  
         System.err.println ( "Prefix too short" ) ; 
         throw e; 
        }  
      }  catch  ( Exception cause )   {  
       System.err.println ( "Cause: " + cause ) ; 
      }  
     try  {  
       divZero (  ) ; 
      }  catch  ( Exception e )   {  
       System.err.println ( "Division by Zero" ) ; 
       e.printStackTrace (  ) ; 
      }  
     try  {  
       arrayAccess ( args ) ; 
      }  catch  ( Exception e )   {  
       System.err.println ( "No command line args" ) ; 
      }  
    }  
  } 


[1517]Delete temp file when program exits
By Anonymous on 2005/08/16 14:05:41  Rate
// Create temp file. 
 File temp = File.createTempFile ( "prefix", ".suffix" ) ; 
  
  
 // Delete temp file when program exits. 
 temp.deleteOnExit (  ) ; 
  
  
 


public static File createTempFile(String prefix,
                                  String suffix,
                                  File directory)
                           throws IOException
See Also:
SecurityManager.checkWrite(java.lang.String), SecurityException, IllegalArgumentException, deleteOnExit()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean delete()
See Also:
SecurityManager.checkDelete(java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[771]Delete all files and subdirectories
By JavaJeffG on 2005/04/07 07:07:26  Rate
// If infile is a directory, this deletes 
     // all files and subdirectories, too. 
     private static void deleteFiles ( File infile )   {  
         if  ( infile.isDirectory (  )  )   {  
             String [  ]  files = infile.list (  ) ; 
              
             for  ( int i = 0; i  <  files.length; i++ )   {  
                 deleteFiles ( new File ( infile, files [ i ]  )  ) ; 
              }  
             infile.delete (  ) ; 
          }  
         else  {  
             if  ( infile.delete (  )  )   {  
                 // you may want to keep track of deleted files here 
              }  
             else 
              {  
                 // you may want to provide error logging here 
              }  
          }  
      }  


[934]_
By Anonymous on 2005/05/06 12:58:14  Rate
You always check the return code, it might return false without throwing any exception, for example, if a file is opened and delete (  )  is called without closeing it first, this situation will occurs.

public void deleteOnExit()
See Also:
delete(), SecurityManager.checkDelete(java.lang.String), SecurityException, FileLock
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1528]Simple mutex implementation
By Anonymous on 2005/09/07 08:36:09  Rate
//simple mutex implementation 
  
  
   File lock = new File ( "mutex.lock" ) ; 
   if  ( !lock.createNewFile (  )  )   {  
    System.err.println ( "Another instance is already running." ) ; 
    System.exit ( 1 ) ; 
    }  
   lock.deleteOnExit (  ) ;


public boolean equals(Object obj)
See Also:
Hashtable, Object.hashCode()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean exists()
See Also:
SecurityManager.checkRead(java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1114]How to use different File methods
By flyfree21 { at } yahoo { dot } co { dot } uk on 2004/11/12 06:28:23  Rate
import java.io.*; 
 import java.util.*; 
  
  
 public class IOMethodTry  {  
  
  
   public static void main ( String [  ]  args )   {  
     String path = "C:/NewFile.txt"; 
     String abspath ; 
     File f = new File ( path ) ; 
     boolean b; 
     try 
      {  
     if  ( f.createNewFile (  ) ==true )  
      {  
       System.out.println ( "File Made" ) ; 
        
      }  
     else 
      {  
     System.out.println ( "Already Existing" ) ; 
     abspath = f.getAbsolutePath (  ) ; 
     System.out.println ( " Abs Path is " + abspath ) ; 
       
      
      }  
     if  ( f.canRead (  )  && f.canWrite (  ) ==false )  
      {  
      System.out.println ( "cannot Read or Write" ) ; 
      }  
     else 
      {  
       System.out.println (  f.getName (  ) +" is a readable and Writable both " ) ; 
       System.out.println (  f.getParent (  ) +" is Parent File " ) ; 
       f.delete (  ) ; 
       System.out.println ( "File Deleted" ) ; 
      }  
      
     if  ( f.exists (  ) ==true )  
      {  
        
     System.out.println ( "Existing" ) ;   
      }  
     else 
      {  
        
     System.out.println ( " File does not exist" ) ;   
        
      }  
      }  
     catch  ( IOException exp )  
      {  
       System.err.println ( "Error " + exp.getMessage (  )  ) ; 
      }  
      
    }  
  }  
 


public File(File parent,
            String child)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1464]Copy file
By bhavani { at } emacmillan { dot } com on 2005/06/27 21:11:19  Rate
import java.io.*; 
  
  
 class filereadwrite 
  {  
 public static void main ( String args [  ]  )  throws IOException 
  {  
  
  
 File fread = new File (  "G:\\applettest","test.txt" ) ; 
  
  
 File fwrite = new File ( "G:\\applettest","write.txt" ) ; 
 FileReader in = new FileReader ( fread ) ; 
 FileWriter out = new FileWriter ( fwrite ) ; 
  
  
 int c;  
  
  
 while (  ( c=in.read (  )  )  != -1 )  
  {  
 out.write ( c ) ; 
  }  
 in.close (  ) ; 
 out.close (  ) ; 
  }  
  
  
  }  
 


public File(String pathname)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public File(String parent,
            String child)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public File(URI uri)
See Also:
toURI(), IllegalArgumentException, NullPointerException, getAbsoluteFile
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public File getAbsoluteFile()
See Also:
SecurityException, getAbsolutePath()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getAbsolutePath()
See Also:
isAbsolute(), SecurityException, getPath()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public File getCanonicalFile()
                      throws IOException
See Also:
SecurityManager.checkRead(java.io.FileDescriptor), SecurityException, getCanonicalPath()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getCanonicalPath()
                        throws IOException
See Also:
SecurityManager.checkRead(java.io.FileDescriptor), SecurityException, getAbsolutePath()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getName()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[746]written by indian legend
By bipkumar { at } yahoo { dot } com on 2004/04/23 06:29:34  Rate
//written by indian legend 
 import java.io.*; 
 public class MyJava 
  {  
   //create a directory c:/temp and create a file db1.xml 
   public static void main ( String s [  ]  ) throws Exception 
  {  
   File directory = new File ( "C:/temp" ) ;  
   File [  ]  filesInDir = directory.listFiles (  ) ;  
   if  ( filesInDir != null )   
    {   
       int length = filesInDir.length;  
        System.out.println ( length ) ; 
       for  ( int i = 0; i  <  length; ++i )   
        {     
           File f = filesInDir [ i ] ;  
           if  ( f.isFile (  )  )   
            {   
                System.out.println (  "Your files is" + f.getName (  )  ) ;  
          }   
      }  
    }  
  }  
    
  } 


public String getParent()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1988]Gerparent
By abdulrahuman { dot } s { at } gmail { dot } com on 2009/01/23 04:18:53  Rate
import java.io.File; 
  
  
 class  Getparent  {  
 // C'est programation de getParent (  )  
 // bon apprendre 
  
  
 public static void main ( String args [  ]   )   {  
  
  
 File f = new File ( "C:// bounjour" ) ; 
 File f1 = new File ( "C://bounjour//bon apprendre" ) ; 
  
  
  System.out.println (  f.mkdir (  )  ) ; 
  System.out.println (  f1.mkdir (  )  ) ; 
  
  
  
 System.out.println (  f.getParent (  )  ) ; 
 System.out.println (  f1.getParent (  )  ) ; 
   }  
  }  
   


public File getParentFile()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String getPath()
See Also:
default name-separator character
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int hashCode()
See Also:
Hashtable, Object.equals(java.lang.Object)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean isAbsolute()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean isDirectory()
See Also:
SecurityManager.checkRead(java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean isFile()
See Also:
SecurityManager.checkRead(java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1419]Random Access File/IP Address of system/File Copy, Rename, Check properties
By hemant_blue_2000 { at } yahoo { dot } com on 2005/05/10 01:06:31  Rate
 
 Random Access File 2377 - 401 2a 
 import java.io.*; 
  
  
 public class randomAccess  {  
  
  
     public static void main ( String [  ]  arg )  throws IOException  {  
         RandomAccessFile raf = new RandomAccessFile ( "foo.txt", "rw" ) ; 
  
  
         try  {  
         raf.seek ( raf.length (  )  ) ; //goes at the end of file 
         raf.writeBytes ( arg [ 0 ]  ) ; //writes string 
         System.out.println ( "Appended "+arg [ 0 ]  ) ; 
         raf.seek ( raf.length (  )  ) ; 
         raf.writeBytes ( " "+arg [ 1 ]  ) ; 
         System.out.println ( "Appended "+arg [ 1 ]  ) ; 
  
  
          }  
         finally  {  
             raf.close (  ) ; 
          }  
      }  
  }  
  
  
  
 IP Address of system: 2685 ??? 401 4a    
  
  
 import java.io.*; 
 import java.net.*; 
  
  
 public class IPAddress 
  {  
  
  
   public static void main ( String arg [  ]  )  throws UnknownHostException 
    {  
      InetAddress addr = InetAddress.getLocalHost (  ) ; 
      System.out.println ( "Host name/ip address = " + addr.getByName ( addr.getHostName (  )  )  ) ; 
  
  
     InetAddress ar [  ] =addr.getAllByName ( "www.nba.com" ) ; 
     for ( int i=0;i < ar.length;i++ )  
      {  
       System.out.println ( ar [ i ]  ) ; 
      }  
  
  
  
    }  
  }  
  
  
 /*Output : 
  
  
 D:\Java Files\practice\Hemant > java IPAddress 
 Host name/ip address = hgurav/192.168.10.27 
 www.nba.com/60.254.129.32 
 www.nba.com/60.254.129.30 
 */
 
  
  
  
  
  
  
  
 File Copy/ Rename /Check properties 2377-401 2c, 
  
  
 import java.io.*; 
 public class CopyFile 
  {  
     CopyFile ( String fSrc, String fDest )  
      {  
         String fsrc,fdest; 
         fsrc=fSrc; 
         fdest=fDest; 
         File f=null,f1=null; 
         try 
          {  
  
  
                 f=new File ( fsrc ) ; 
                 f1=new File ( fdest ) ; 
  
  
         if ( f.isDirectory (  )  )  
          {  
           System.out.println ( "You have provided Directory name" ) ; 
           System.exit ( 0 ) ; 
          }  
         if ( f.exists (  ) ==true )  
          {  
           System.out.println ( "File exists." ) ; 
  
  
          }  
         if ( f.isFile (  ) ==true )  
          {  
           System.out.println ( "File format is correct" ) ; 
  
  
          }  
         if ( f.canRead (  ) ==true )  
          {  
           System.out.println ( "File is Readable" ) ; 
  
  
          }  
         if ( f1.canWrite (  ) ==true )  
          {  
           System.out.println ( "File is Writable" ) ; 
  
  
          }  
  
  
 InputStream fin=new FileInputStream ( fsrc ) ;  
 OutputStream fout=new FileOutputStream ( fdest,true ) ; // true is to append 
        System.out.println ( "Copying." ) ; 
             int i=0; 
             i=fin.read (  ) ; 
                 while ( i!=-1 )  
                  {  
             i=fin.read (  ) ; 
                     fout.write ( i ) ; 
  
  
  
                  }  
             fin.close (  ) ; 
             fout.close (  ) ; 
  
  
             f=new File ( "OutputFile.txt" ) ; //File Rename 
             f1.renameTo ( f ) ; //File object as parameter 
          }  
         catch ( FileNotFoundException fe )  
      {  
           System.out.println ( "Source File Not Found" ) ; 
          }  
         catch ( Exception e )  
          {  
             e.printStackTrace (  ) ; 
          }  
  
  
    }  //Close Constructor 
  
  
     public static void main ( String arg [  ]  )  
      {  
         String fsrc,fdest; 
         if ( arg.length < 2 )  
          {  
             System.out.println ( "Usage: Filecopy SourceFile DestingationFile" ) ; 
             System.exit ( 0 ) ; 
          }  
         fsrc=arg [ 0 ] ; 
         fdest=arg [ 1 ] ; 
         CopyFile f=new CopyFile ( fsrc,fdest ) ; 
      }  
  }  
 


public boolean isHidden()
See Also:
SecurityManager.checkRead(java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public long lastModified()
See Also:
SecurityManager.checkRead(java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public long length()
See Also:
SecurityManager.checkRead(java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String[] list()
See Also:
SecurityManager.checkRead(java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String[] list(FilenameFilter filter)
See Also:
SecurityManager.checkRead(java.lang.String), SecurityException, FilenameFilter.accept(java.io.File, java.lang.String), list()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[809]Get the list of your desired files
By Khaled Mahmud Shams on 2005/02/07 03:10:52  Rate
 
 import java.io.*; 
  
  
 public class File2  {  
  
  
 public static void main ( String [  ]  args )   {  
  
  
 //specify the pathname of the directory of which 
 //you would like to get the file names you desire 
  
  
   File b = new File ( "C:\\JavaProg\\OnlyClass" ) ; 
  
  
 //define the accept method of the interface 
 //FilenameFilter. The method will return the 
 //files the way you ask to return them. 
 //Choose the method from the String class 
 //like endsWith ( String suffix ) ; 
  
  
   FilenameFilter ff = new FilenameFilter (  )   {  
     public boolean accept ( File b, String name )   {  
          return name.endsWith ( ".java" ) ; 
          }  
      } ; 
  
  
  
 //Get the list of your desired files 
  
  
     String [  ]  ch = b.list ( ff ) ; 
  
  
      for ( int i = 0; i  <  ch.length; i++ )  
  
  
   System.out.println ( "The list here: " + ch [ i ]  ) ; 
  
  
  }  
  } 


public File[] listFiles()
See Also:
SecurityManager.checkRead(java.lang.String), SecurityException, File(File, String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public File[] listFiles(FileFilter filter)
See Also:
SecurityManager.checkRead(java.lang.String), SecurityException, FileFilter.accept(java.io.File), listFiles()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public File[] listFiles(FilenameFilter filter)
See Also:
SecurityManager.checkRead(java.lang.String), SecurityException, FilenameFilter.accept(java.io.File, java.lang.String), listFiles()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[330]Implement FilenameFilter
By Marco on 2003/10/21 04:56:54  Rate
// implement FilenameFilter 
   class PhilePhilter implements FilenameFilter {  
     public boolean accept ( File dir, String file )  {  
       return  ( file.toUpperCase (  ) .indexOf ( ".TXT" )  != -1 ) ; 
      }  
    }  
  
  
   // in some code block where you want to test it 
   File f = new File ( "C:" ) ; 
   if ( f.exists (  )  )  {  
     File [  ]  files = f.listFiles ( new PhilePhilter (  )  ) ; 
     if ( files != null && files.length  >  0 )  {  
       System.out.println ( "number of files="+files.length ) ; 
      }  
     else {  
       System.out.println ( "no files found" ) ; 
      }  
    } 


[2006]filenamefilter
By shashicp_16 { at } yahoo { dot } com on 2010/02/01 22:05:58  Rate
please explain about accept method in filenamefilter 
 


public static File[] listRoots()
See Also:
SecurityManager.checkRead(java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1425]List all the roots in Windows
By Anonymous on 2005/05/13 05:41:23  Rate
 
 This will list all the roots in Windows.e.g A:\ C:\ etc 
  
  
 import java.io.*; 
  
  
 class listdir 
  {  
      
   public static void main ( String args [  ]  )  
      {         
        
                     File [  ]  fobj=File.listRoots (  ) ;      
                     int i=0; 
                     for ( i=0;i < fobj.length;i++ )  
                      {  
                         System.out.println ( fobj [ i ] .toString (  )  ) ; 
                      }  
  
  
      }  
  }     


public boolean mkdir()
See Also:
SecurityManager.checkWrite(java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[561]Create directory
By samy arbid on 2005/06/06 03:25:22  Rate
File newDir = new File ( "C:\temp\" ) ; 
 if  ( newDir.mkdir (  ) ==true )  
 System.out.println ( "Directory was created" ) ; 
 else 
 System.out.println ( "Directory already existed" ) ; 
  
  
 


[1866]/ not \
By justus { at } tmrmusic { dot } com on 2007/02/12 09:51:12  Rate
I think the "\" need to be changed to "/" or you will get a systax error because of an unclosed string.

public boolean mkdirs()
See Also:
SecurityManager.checkWrite(java.lang.String), SecurityManager.checkRead(java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[807]Creates a new directory and inside it creates a new file
By Khaled Mahmud Shams on 2004/06/16 10:21:33  Rate
//This program creates a new directory 
 // and inside it creates a new .txt file 
  
  
 import java.io.*; 
  
  
 public class File1  {  
 public static void main ( String [  ]  args )  throws IOException {  
  
  
  
 //write the pathe name in the file constructor 
  
  
 File ab = new File ( "C:\\NewPath\\New" ) ; 
  
  
 //call the File??s boolean method mkdirs (  )  
 //to create a new directory for you 
 //in your specied path and get the path 
 //by calling the getAbsolutePath (  )  method 
  
  
 if ( ab.mkdirs (  )  )   {  
   System.out.println ( "New directory is created at: " 
           + ab.getAbsolutePath (  )  ) ; 
  }  
 else  {  
    System.out.println ( "Already exists" ) ; 
  }  
  
  
 //Now create a .txt file in the above created directory 
  
  
 File bc = new File ( ab, "uni.txt" ) ; 
  
  
 if ( bc.createNewFile (  )  )   {  
   System.out.println ( "New text file is created at: " 
                         + bc.getAbsolutePath (  )  ) ; 
  }  
 else {  
   System.out.println ( "Not created" ) ; 
  }  
  
  
  }  
  } 


public static final String pathSeparator
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final char pathSeparatorChar
See Also:
System.getProperty(java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean renameTo(File dest)
See Also:
NullPointerException, SecurityManager.checkWrite(java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[888]Rename a file or a directory
By Anuja Piplapure on 2005/05/26 08:00:33  Rate
File fle = new File  (  "C:\\path\\filename.txt"  ) ; 
  
  
 boolean done = fle.renameTo  (  "C:\\path\\newfilename.txt"  ) ; 
  
  
 if  (  done )  
  { System.out.println  ( "renamed successfully" ) ; 
  }  
 else 
  { System.out.println  ( "not renamed successfully" ) ; 
  
  
  } 


[915]_
By Anonymous on 2004/09/21 10:52:36  Rate
File file = new File  (  "C:\\filename.txt"  ) ;  
 File newFile = new File  (  "C:\\newfilename.txt"  ) ;  
  
  
 if  (  file.renameTo  (  newFile  )   )   
  {  
   //!!! Very Important here, the following statement output 
   // is still the OLD file name, namely filename.txt 
   System.out.println ( file.getName (  )  ) ;   
  
  
   System.out.println  ( "renamed successfully" ) ;  
  }   
 else  
  {  
   System.out.println  ( "not renamed successfully" ) ;  
  }     


[933]_
By Anonymous on 2004/09/27 11:18:12  Rate
NOTE: The File object is immutable

[1112]_
By Gius ;) on 2004/11/10 05:18:38  Rate
// File  ( or directory )  with old name 
     File file = new File ( "oldname" ) ; 
      
     // File  ( or directory )  with new name 
     File file2 = new File ( "newname" ) ; 
      
     // Rename file  ( or directory )  
     boolean success = file.renameTo ( file2 ) ; 
     if  ( !success )   {  
         // File was not successfully renamed 
      }  
 


[1776]Can not move image file
By bthuankhanh on 2006/06/13 14:58:35  Rate
I have a problem that I need yours help me. 
  
  
 I coding a program by Java with code segment for move file image form a  
 folder to another such as: 
  
  
 File fileMove=new File ( \"Source pathname/filename.jpg\" ) ; 
 File dirDest = new File ( \"Dest pathname/filename.jpg\" ) ; 
 fileMove.renameTo ( dir ) ; 
  
  
 But It run not good. files doesn\'t move to dest folder.


public static final String separator
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final char separatorChar
See Also:
System.getProperty(java.lang.String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean setLastModified(long time)
See Also:
SecurityManager.checkWrite(java.lang.String), SecurityException, IllegalArgumentException, lastModified()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1410]Change a file timestamp (touch)
By Anonymous on 2005/05/02 05:49:07  Rate
public static boolean touch ( String pDateiName, Date pDatum  )  
    {  
     try  {  
  
  
       File touchFile = new File (  pDateiName  ) ; 
       return touchFile.setLastModified (  pDatum.getTime (  )   ) ; 
  
  
      }  catch  ( Throwable e )   {  
      }  
     return false; 
    }  
 


public boolean setReadOnly()
See Also:
SecurityManager.checkWrite(java.lang.String), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String toString()
See Also:
Object, getPath()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public URI toURI()
See Also:
URI.toURL(), File(java.net.URI), getAbsoluteFile
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1491]Open a URI file
By ritttu2000 { at } yahoo { dot } com on 2005/07/21 06:29:41  Rate
URI uri = null; 
       String url="http://localhost:8080/xml/testxml/car.xml"; 
  
  
       try  {  
             uri = new URI ( url ) ; 
          }  catch  ( URISyntaxException e )   {  
          }  
  
  
          if ( uri.isAbsolute (  )  )  
           {  
         // File file = new File ( uri.toURI (  ) .equals 
 The proble m is that i have to save the xml file 
  
  
  ( uri.getAbsoluteFile (  )  )  ) ; 
  
  
            File file = new File ( uri ) ; 
        if (  file.isDirectory (  )  )  
          {  
           System.out.println ( "is Directory" ) ; 
             }  
          }  
  
  
     // File f = new File ( uri ) ; 
       System.out.println ( "File" +"--- > "+ uri ) ; 
 


public URL toURL()
          throws MalformedURLException
See Also:
URI, toURI()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags