KickJava   Java API By Example, From Geeks To Geeks.

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

java.io
Class FileInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FileInputStream
All Implemented Interfaces:
Closeable
See Also:
Top Examples, Source Code, File, FileDescriptor, FileOutputStream

public int available()
              throws IOException
See Also:
InputStream
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[14]Non-blocking file read
By Anonymous on 2005/02/22 13:55:12  Rate
try  {  
  
  
 FileInputStream fis = new FileInputStream ( f ) ; 
  
  
 System.out.Print  ( "File - " + f.GetName (  )  + ": " ) ; 
 System.out.Print  ( " Non-blocking read " + fis.available (  )  + " bytes." ) ; 
 System.out.println (  " Total " + f.length (  )  + " bytes." ) ; 
  
  
 fis.close (  ) ; 
 fis = null; 
  
  
  }  catch  ( Exception e )   {  } 


public void close()
           throws IOException
See Also:
InputStream, Closeable
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1863]iostream
By Anonymous on 2007/01/27 00:06:20  Rate
import java.io.BufferedInputStream; 
 import java.io.File; 
 import java.io.FileInputStream; 
 import java.io.IOException; 
 import java.io.InputStreamReader; 
 import java.io.Reader; 
 import java.util.Vector; 
 import org.apache.tools.ant.BuildException; 
 import org.apache.tools.ant.Project; 
 import org.apache.tools.ant.Task; 
 import org.apache.tools.ant.filters.util.ChainReaderHelper; 
 import org.apache.tools.ant.types.FilterChain; 
  
  
 /** 
  * Load a file into a property 
  * 
  * @since Ant 1.5 
  * @ant.task category="utility" 
  */
 
 public class LoadFile extends Task  {  
  
  
     /** 
      * source file, usually null 
      */
 
     private File srcFile = null; 
  
  
     /** 
      * what to do when it goes pear-shaped 
      */
 
     private boolean failOnError = true; 
  
  
     /** 
      * Encoding to use for filenames, defaults to the platform's default 
      * encoding. 
      */
 
     private String encoding = null; 
  
  
     /** 
      * name of property 
      */
 
     private String property = null; 
  
  
     /** 
      * Holds FilterChains 
      */
 
     private final Vector filterChains = new Vector (  ) ; 
  
  
     /** 
      * Encoding to use for input, defaults to the platform's default 
      * encoding.  < p >  
      * 
      * For a list of possible values see 
      *  < a HREF="http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html" >  
      * http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html 
      *  < /a > . < /p >  
      * 
      * @param encoding The new Encoding value 
      */
 
  
  
     public final void setEncoding ( final String encoding )   {  
         this.encoding = encoding; 
      }  
  
  
  
     /** 
      * Property name to save to. 
      * 
      * @param property The new Property value 
      */
 
     public final void setProperty ( final String property )   {  
         this.property = property; 
      }  
  
  
  
     /** 
      * Sets the file to load. 
      * 
      * @param srcFile The new SrcFile value 
      */
 
     public final void setSrcFile ( final File srcFile )   {  
         this.srcFile = srcFile; 
      }  
  
  
  
     /** 
      * If true, fail on load error. 
      * 
      * @param fail The new Failonerror value 
      */
 
     public final void setFailonerror ( final boolean fail )   {  
         failOnError = fail; 
      }  
  
  
  
     /** 
      * read in a source file to a property 
      * 
      * @exception BuildException if something goes wrong with the build 
      */
 
     public final void execute (  )  
         throws BuildException  {  
         //validation 
         if  ( srcFile == null )   {  
             throw new BuildException ( "source file not defined" ) ; 
          }  
         if  ( property == null )   {  
             throw new BuildException ( "output property not defined" ) ; 
          }  
         FileInputStream fis = null; 
         BufferedInputStream bis = null; 
         Reader instream = null; 
         log ( "loading " + srcFile + " into property " + property, 
             Project.MSG_VERBOSE ) ; 
         try  {  
             final long len = srcFile.length (  ) ; 
             log ( "file size = " + len, Project.MSG_DEBUG ) ; 
             //discard most of really big files 
             final int size =  ( int )  len; 
             //open up the file 
             fis = new FileInputStream ( srcFile ) ; 
             bis = new BufferedInputStream ( fis ) ; 
             if  ( encoding == null )   {  
                 instream = new InputStreamReader ( bis ) ; 
              }  else  {  
                 instream = new InputStreamReader ( bis, encoding ) ; 
              }  
  
  
             String text = ""; 
             if  ( size != 0 )   {  
                 ChainReaderHelper crh = new ChainReaderHelper (  ) ; 
                 crh.setBufferSize ( size ) ; 
                 crh.setPrimaryReader ( instream ) ; 
                 crh.setFilterChains ( filterChains ) ; 
                 crh.setProject ( getProject (  )  ) ; 
                 instream = crh.getAssembledReader (  ) ; 
  
  
                 text = crh.readFully ( instream ) ; 
              }  
  
  
             if  ( text != null )   {  
                 if  ( text.length (  )   >  0 )   {  
                     getProject (  ) .setNewProperty ( property, text ) ; 
                     log ( "loaded " + text.length (  )  + " characters", 
                         Project.MSG_VERBOSE ) ; 
                     log ( property + " := " + text, Project.MSG_DEBUG ) ; 
                  }  
              }  
  
  
          }  catch  ( final IOException ioe )   {  
             final String message = "Unable to load file: " + ioe.toString (  ) ; 
             if  ( failOnError )   {  
                 throw new BuildException ( message, ioe, getLocation (  )  ) ; 
              }  else  {  
                 log ( message, Project.MSG_ERR ) ; 
              }  
          }  catch  ( final BuildException be )   {  
             if  ( failOnError )   {  
                 throw be; 
              }  else  {  
                 log ( be.getMessage (  ) , Project.MSG_ERR ) ; 
              }  
          }  finally  {  
             try  {  
                 if  ( fis != null )   {  
                     fis.close (  ) ; 
                  }  
              }  catch  ( IOException ioex )   {  
                 //ignore 
              }  
          }  
      }  
  
  
     /** 
      * Add the FilterChain element. 
      * @param filter the filter to add 
      */
 
     public final void addFilterChain ( FilterChain filter )   {  
         filterChains.addElement ( filter ) ; 
      }  
  
  
 //end class 
  }  
 


public FileInputStream(File file)
                throws FileNotFoundException
See Also:
SecurityManager.checkRead(java.lang.String), File.getPath(), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[474]Use DataInputStream to read a file
By xyz on 2004/07/18 15:22:31  Rate
import java.io.*; 
  
  
 class FileInputDemo 
  {  
   public static void main ( String args [  ]  )  
    {  
     //args.length is equivalent to argc inc 
     if  ( args.length ==1 )  
      {  
       try 
        {  
       //open the file that is the first 
       //command line parameter 
       FileInputStream fstream = new FileInputStream ( args [ 0 ]  ) ; 
  
  
       //convert our input stream to a DataInputStream 
       DataInputStream in = new DataInputStream ( fstream ) ; 
  
  
       //Continue to read lines while there are stilll some left 
       while  ( in.available (  )  !=0 )  
        {  
         //print file line to screen 
         System.out.println ( in.readLine (  )  ) ; 
        }  
       in.close (  ) ; 
        }  
       catch  ( Exception e )  
        {  
         System.err.println ( "File input Error" ) ; 
        }  
      }  
     else 
       System.out.println ( "Invalid parameters" ) ; 
    }  
  }  
 


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


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


[1141]Read one byte from a file
By Anonymous on 2004/11/24 00:15:53  Rate
m_oFileInputStream = new FileInputStream ( "D:\\Misc\\Temp.txt" ) ; 
           while  ( m_oFileInputStream.available (  )   >  0 )   {  
             Integer in = new Integer ( m_oFileInputStream.read (  )  ) ; 
             System.out.println ( in.toString (  )  ) ; 
            } 


protected void finalize()
                 throws IOException
See Also:
close(), Object
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public FileChannel getChannel()
See Also:
position
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public final FileDescriptor getFD()
                           throws IOException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int read()
         throws IOException
See Also:
InputStream
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[610]Read file byte by byte
By rameeyam { at } yahoo { dot } com on 2005/05/02 17:40:12  Rate
import java.io.*; 
  
  
 class fileread {  
  
  
 public static void main ( String args [  ]  )  throws IOException {  
  
  
  
 int i; 
  
  
 FileInputStream fin=new FileInputStream ( "c:\\input.txt" ) ; 
  
  
 do {  
     i=fin.read (  ) ; 
     System.out.println (  ( char ) i ) ; 
      } while ( i != -1 ) ; 
  
  
  
  
     fin.close (  ) ; 
     fin=null; 
  
  
    }  
  
  
    }  
 


public int read(byte[] b)
         throws IOException
See Also:
InputStream.read(byte[], int, int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public int read(byte[] b,
                int off,
                int len)
         throws IOException
See Also:
InputStream.read()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[13]Read whole file
By Anonymous on 2005/08/30 04:27:56  Rate
try  {  
  
  
 FileInputStream fis = new FileInputStream ( f ) ; 
  
  
 if  ( f.length (  )   >  Integer.MAX_VALUE )  
    System.out.println ( "File '" + f.getName (  )  + "' is too big." ) ; 
  
  
 int buffersize =  ( int ) f.length (  ) ; 
 byte [  ]  contents = new byte [ buffersize ] ;; 
  
  
 long n = fis.read ( contents, 0, buffersize ) ; 
 fis.close (  ) ; 
 fis = null; 
  
  
 if  ( n != f.length (  )  )  
    System.out.println ( "Error in reading file '" + f.getName (  )  + "'" ) ; 
 Else 
    System.out.println (  new String ( contents )   ) ; 
  
  
  }  catch  ( Exception e )   {  }  
 


[1358]Matrix reload
By chabanda { at } yahoo { dot } com on 2005/03/23 06:58:46  Rate
package matrixreload; 
  
  
 import java.lang.Math; 
 import java.io.*; 
 import java.io.InputStreamReader; 
 import java.io.ObjectInputStream; 
 import java.io.DataInputStream; 
  
  
 public class MatrixReload  {  
  
  
   public MatrixReload (  )   {  
    }  
   public static void main ( String [  ]  args )   {  
  
  
     int i=0,j=0,x=0,y=0,p=0,q=0; 
     int r=0; 
     int c=0; 
  
  
     DataInputStream keyboard = new DataInputStream (  System.in  ) ; 
  
  
     boolean inputNotOk=true; 
      while  (  inputNotOk )   {  
       System.out .println ( "Enter the no. of raws u want in the matrix  ( for addition ) : " ) ; 
       System.out.flush (  ) ; 
     try  {  
       r = Double.valueOf ( keyboard.readLine (  ) .trim (  )  ) .intValue (  ) ; 
       inputNotOk = false; 
        }  catch  (  java.lang.Exception exception )  {  
         System.out.println (  "cannot understand, please enter the no. of raws again!" ) ; 
         }  
        }  
  
  
       boolean outputNotOk = true; 
       while  (  outputNotOk )   {  
        System.out .println ( "Enter the no. of columns u want in the matrix  ( for addition ) :"  ) ; 
        System.out.flush (  ) ; 
       try  {  
         c = Double.valueOf ( keyboard.readLine (  ) .trim (  )  ) .intValue (  ) ; 
         outputNotOk = false; 
          }  catch  (  java.lang.Exception exception )  {  
            System.out.println (  "cannot understand, please enter the amount again!" ) ; 
             }  
          }  
  
  
         int [  ]  [  ]  first = new int  [ r+1 ]  [ c+1 ] ; 
         int [  ]  [  ]  second = new int  [ r+1 ]  [ c+1 ] ; 
         int [  ]  [  ]  addition = new int  [ r+1 ]  [ c+1 ] ; 
         for  (  i=1; i <  ( r+1 ) ; i++ )  {  
           for  (  j=1; j <  ( c+1 ) ; j++ )  {  
             first [ i ]  [ j ]  =  ( int )  ( 45*Math.random (  )   ) ; 
            }  
          }  
     System.out.print ( "\n" ) ; 
     System.out.println ( "The elments in the first matrix are: " ) ; 
     for  (  i=1; i <  ( r+1 ) ; i++ )  {  
       for  (  j=1; j <  ( c+1 ) ; j++ )  {  
         System.out.print ( first [ i ]  [ j ]  ) ; 
         System.out.print ( " " ) ; 
        }  
     System.out.print ( "\n" ) ; 
      }  
  
  
     for  (  x=1; x <  ( r+1 ) ; x++ )  {  
       for  (  y=1; y <  ( c+1 ) ; y++ )  {  
         second [ x ]  [ y ]  =  ( int )  ( 45*Math.random (  )   ) ; 
        }  
      }  
     System.out.print ( "\n" ) ; 
     System.out.println ( "The elments in the second matrix are: " ) ; 
     for  (  x=1; x <  ( r+1 ) ; x++ )  {  
       for  (  y=1; y <  ( c+1 ) ; y++ )  {  
         System.out.print ( second [ x ]  [ y ]  ) ; 
         System.out.print ( " " ) ; 
        }  
     System.out.print ( "\n" ) ; 
      }  
     System.out.print ( "\n" ) ; 
     System.out.println ( "The elments in the addition matrix are: " ) ; 
     for ( i=1,x=1,p=1; i <  ( r+1 ) ; i++,x++,p++ )  {  
  
  
        for  ( j=1,q=1,y=1; j <  ( c+1 ) ; j++,q++,y++ )  {  
         addition [ p ]  [ q ] = first [ i ]  [ j ]  + second  [ x ]  [ y ] ; 
          }  
      }  
     for  ( p=1; p <  ( r+1 ) ; p++ )  {  
       for ( q=1; q <  ( c+1 ) ; q++ )  {  
       System.out.print ( addition [ p ]  [ q ]  ) ; 
       System.out.print ( " " ) ; 
        }  
     System.out.print ( "\n" ) ; 
      }  
    }  
  } 


public long skip(long n)
          throws IOException
See Also:
InputStream
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags