KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > nio > channels > FileChannel

java.nio.channels
Class FileChannel

java.lang.Object
  extended by java.nio.channels.spi.AbstractInterruptibleChannel
      extended by java.nio.channels.FileChannel
All Implemented Interfaces:
Closeable, ByteChannel, Channel, GatheringByteChannel, InterruptibleChannel, ReadableByteChannel, ScatteringByteChannel, WritableByteChannel
See Also:
Top Examples, Source Code, FileOutputStream(File,boolean), close, locked, vice versa, to some other channel, forced out, mapped, written, read, truncated, size, modified, queried, FileInputStream.getChannel(), FileOutputStream.getChannel(), RandomAccessFile.getChannel()

protected FileChannel()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract void force(boolean metaData)
                    throws IOException
See Also:
ClosedChannelException, force, map
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final FileLock lock()
                    throws IOException
See Also:
tryLock(long,long,boolean), tryLock(), lock(long,long,boolean), NonWritableChannelException, NonReadableChannelException, OverlappingFileLockException, FileLockInterruptionException, AsynchronousCloseException, ClosedChannelException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[98]Acquire a FileChannel FileLock
By Anonymous on 2005/02/02 14:59:48  Rate
import java.io.*; 
 import java.nio.*; 
 import java.nio.channels.*; 
  
  
 public class Locking  {  
 public static void main ( String arsg [  ]  )  throws IOException  {  
  RandomAccessFile raf = new RandomAccessFile ( "junk.dat", "rw" ) ;    
  FileChannel channel = raf.getChannel (  ) ;  
  FileLock lock = channel.lock (  ) ;  
  try  {  
     System.out.println ( "Got lock!!!" ) ; 
     System.out.println ( "Press ENTER to continue" ) ; 
     System.in.read ( new byte [ 10 ]  ) ; 
   }  finally  {  
     lock.release (  ) ; 
   }  
  }  
  } 


public abstract FileLock lock(long position,
                              long size,
                              boolean shared)
                       throws IOException
See Also:
tryLock(long,long,boolean), tryLock(), lock(), NonWritableChannelException, NonReadableChannelException, OverlappingFileLockException, FileLockInterruptionException, AsynchronousCloseException, ClosedChannelException, IllegalArgumentException, isShared, Long.MAX_VALUE
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract MappedByteBuffer map(FileChannel.MapMode mode,
                                     long position,
                                     long size)
                              throws IOException
See Also:
IllegalArgumentException, PRIVATE, READ_WRITE, NonWritableChannelException, READ_ONLY, NonReadableChannelException, Integer.MAX_VALUE, write, read, ReadOnlyBufferException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract long position()
                       throws IOException
See Also:
ClosedChannelException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract FileChannel position(long newPosition)
                              throws IOException
See Also:
IllegalArgumentException, ClosedChannelException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract int read(ByteBuffer dst)
                  throws IOException
See Also:
ClosedByInterruptException, AsynchronousCloseException, ClosedChannelException, ReadableByteChannel
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract int read(ByteBuffer dst,
                         long position)
                  throws IOException
See Also:
ClosedByInterruptException, AsynchronousCloseException, ClosedChannelException, NonReadableChannelException, IllegalArgumentException, read(ByteBuffer)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final long read(ByteBuffer[] dsts)
                throws IOException
See Also:
ClosedByInterruptException, AsynchronousCloseException, ClosedChannelException, ScatteringByteChannel
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract long read(ByteBuffer[] dsts,
                          int offset,
                          int length)
                   throws IOException
See Also:
ClosedByInterruptException, AsynchronousCloseException, ClosedChannelException, ScatteringByteChannel
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract long size()
                   throws IOException
See Also:
ClosedChannelException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract long transferFrom(ReadableByteChannel src,
                                  long position,
                                  long count)
                           throws IOException
See Also:
ClosedByInterruptException, AsynchronousCloseException, ClosedChannelException, NonWritableChannelException, NonReadableChannelException, IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1193]Struts File Upload using FileChannel
By Moshe Beeri on 2005/01/07 05:41:47  Rate
UploadPictureForm form =  ( UploadPictureForm )  actionForm; 
         InputStream stream = form.getFile (  ) .getInputStream (  ) ; 
         ReadableByteChannel byteChannel = Channels.newChannel ( stream ) ; 
         FileChannel fileChannel = new FileOutputStream ( "c:/temp/name.xxx" ) .getChannel (  ) ; 
         //max 100k 
         fileChannel.transferFrom ( byteChannel,0,1024*100 ) ; 
 


public abstract long transferTo(long position,
                                long count,
                                WritableByteChannel target)
                         throws IOException
See Also:
ClosedByInterruptException, AsynchronousCloseException, ClosedChannelException, NonWritableChannelException, NonReadableChannelException, IllegalArgumentException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1909]Transfer files > than 2^31-1 bytes.
By Anonymous on 2007/08/05 20:21:26  Rate
In win2000 , the transferTo (  )  does not transfer files  >  than 2^31-1 bytes. it throws an exception of "java.io.IOException: The parameter is incorrect" 
 In solaris8 , Bytes transfered to Target channel are 2^31-1 even if the source channel file is greater than 2^31-1 
 In LinuxRH7.1 , it gives an error of java.io.IOException: Input/output error


public abstract FileChannel truncate(long size)
                              throws IOException
See Also:
IllegalArgumentException, ClosedChannelException, NonWritableChannelException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final FileLock tryLock()
                       throws IOException
See Also:
tryLock(long,long,boolean), lock(long,long,boolean), lock(), OverlappingFileLockException, ClosedChannelException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract FileLock tryLock(long position,
                                 long size,
                                 boolean shared)
                          throws IOException
See Also:
tryLock(), lock(long,long,boolean), lock(), OverlappingFileLockException, ClosedChannelException, IllegalArgumentException, isShared, Long.MAX_VALUE
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract int write(ByteBuffer src)
                   throws IOException
See Also:
ClosedByInterruptException, AsynchronousCloseException, ClosedChannelException, WritableByteChannel
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract int write(ByteBuffer src,
                          long position)
                   throws IOException
See Also:
ClosedByInterruptException, AsynchronousCloseException, ClosedChannelException, NonWritableChannelException, IllegalArgumentException, write(ByteBuffer)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final long write(ByteBuffer[] srcs)
                 throws IOException
See Also:
ClosedByInterruptException, AsynchronousCloseException, ClosedChannelException, GatheringByteChannel
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public abstract long write(ByteBuffer[] srcs,
                           int offset,
                           int length)
                    throws IOException
See Also:
ClosedByInterruptException, AsynchronousCloseException, ClosedChannelException, GatheringByteChannel
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1908]Copy a file using the java.nio package
By Anonymous on 2007/08/05 20:20:41  Rate
Old Way 
 ======= 
  
  
 import java.io.*; 
  
  
 public class JCopy {  
   public static void main ( String args [  ]  )  {  
     try  {  
       JCopy j = new JCopy (  ) ; 
       j.copyFile ( new File ( args [ 0 ]  ) ,new File ( args [ 1 ]  )  ) ; 
        }  
     catch  ( Exception e )   {  
       e.printStackTrace (  ) ; 
        }  
      }  
  
  
   public void copyFile ( File in, File out )  throws Exception  {  
     FileInputStream fis = new FileInputStream ( in ) ; 
     FileOutputStream fos = new FileOutputStream ( out ) ; 
     byte [  ]  buf = new byte [ 1024 ] ; 
     int i = 0; 
     while (  ( i=fis.read ( buf )  ) !=-1 )   {  
       fos.write ( buf, 0, i ) ; 
        }  
     fis.close (  ) ; 
     fos.close (  ) ; 
      }  
  }  
  
  
 New Way  ( JDK1.4 using the java.nio package ) import java.nio.channels.*; 
 import java.io.*; 
  
  
 public class JCopy2 {  
   public static void main ( String args [  ]  )  {  
     try  {  
       JCopy2 j = new JCopy2 (  ) ; 
       j.copyFile ( new File ( args [ 0 ]  ) ,new File ( args [ 1 ]  )  ) ; 
      }  
     catch  ( Exception e )   {  
       e.printStackTrace (  ) ; 
      }  
   }  
  
  
   public void copyFile ( File in, File out )  throws Exception  {  
      FileChannel sourceChannel = new 
           FileInputStream ( in ) .getChannel (  ) ; 
      FileChannel destinationChannel = new 
           FileOutputStream ( out ) .getChannel (  ) ; 
      sourceChannel.transferTo ( 0, sourceChannel.size (  ) , destinationChannel ) ; 
      // or 
      // destinationChannel.transferFrom 
      //        ( sourceChannel, 0, sourceChannel.size (  )  ) ; 
      sourceChannel.close (  ) ; 
      destinationChannel.close (  ) ; 
       }  
  }  
 ======= 
  
  
 

Popular Tags