KickJava   Java API By Example, From Geeks To Geeks.

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

java.io
Class InputStream

java.lang.Object
  extended by java.io.InputStream
All Implemented Interfaces:
Closeable
Direct Known Subclasses:
AudioInputStream, ByteArrayInputStream, FileInputStream, FilterInputStream, InputStream, ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream
See Also:
Top Examples, Source Code, BufferedInputStream, DataInputStream, read(), OutputStream, PushbackInputStream

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


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


[1142]Delete a file even when the InputStream related to it is open
By Farrukh Mahmood on 2005/03/20 17:44:48  Rate
I am able to delete a file even when the InputStream related to it is open.

[1364]Read a page over HTTP
By Anonymous on 2005/05/20 18:38:47  Rate
InputStream in = new URL (  "http://www.kickjava.com"  ) .openStream (  ) ; 
 try  {  
   InputStreamReader inR = new InputStreamReader (  in  ) ; 
   BufferedReader buf = new BufferedReader (  inR  ) ; 
   String line; 
   while  (   (  line = buf.readLine (  )   )  != null  )   {  
     System.out.println (  line  ) ; 
    }  
  }  finally  {  
   in.close (  ) ; 
  }  
 


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


[1787]Getting Values From Console &Writing It To A File
By RM on 2006/07/14 04:46:01  Rate
import java.io.*; 
 public class GettingValuesFromConsoleWritingToFile  {  
  
  
   /** 
    * @param args 
    * @throws IOException  
    */
 
   public static void main ( String [  ]  args )  throws IOException  {  
     System.out.println ( "Enter your Name/Sex/age" ) ; 
     InputStreamReader lsInputReader = new InputStreamReader ( System.in ) ; 
     BufferedReader lsBufferedReader = new BufferedReader ( lsInputReader ) ; 
     String lsInput = null; 
     while (  ( lsInput=lsBufferedReader.readLine (  )  ) !=null )  
      {  
       System.out.println ( lsInput ) ; 
        
       File file = new File ( "C:/YourFile.txt" ) ; 
       boolean isFile = file.exists (  ) ; 
       if ( !isFile )  
        {  
         file.createNewFile (  ) ; 
         System.out.println ( "File has been created at >  C:/YourFile.txt" ) ; 
         BufferedWriter out = new BufferedWriter ( new FileWriter ( file )  ) ; 
         out.write ( lsInput ) ; 
         out.close (  ) ; 
        }  
       else 
        {  
         System.out.println ( "Find ur File at >  C:/YourFile.txt" ) ; 
        }  
      }  
  
  
    }  
  
  
  }  
 


public void mark(int readlimit)
See Also:
reset()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean markSupported()
See Also:
reset(), mark(int)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


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


public int read(byte[] b)
         throws IOException
See Also:
read(byte[], int, int), NullPointerException
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:
read(), NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1749]Returns the contents of the file in a byte array
By Anonymous on 2006/04/28 00:30:40  Rate
// Returns the contents of the file in a byte array. 
     public static byte [  ]  getBytesFromFile ( File file )  throws IOException  {  
         InputStream is = new FileInputStream ( file ) ; 
      
         // Get the size of the file 
         long length = file.length (  ) ; 
      
         // You cannot create an array using a long type. 
         // It needs to be an int type. 
         // Before converting to an int type, check 
         // to ensure that file is not larger than Integer.MAX_VALUE. 
         if  ( length  >  Integer.MAX_VALUE )   {  
             // File is too large 
          }  
      
         // Create the byte array to hold the data 
         byte [  ]  bytes = new byte [  ( int ) length ] ; 
      
         // Read in the bytes 
         int offset = 0; 
         int numRead = 0; 
         while  ( offset  <  bytes.length 
                &&  ( numRead=is.read ( bytes, offset, bytes.length-offset )  )   > = 0 )   {  
             offset += numRead; 
          }  
      
         // Ensure all the bytes have been read in 
         if  ( offset  <  bytes.length )   {  
             throw new IOException ( "Could not completely read file "+file.getName (  )  ) ; 
          }  
      
         // Close the input stream and return bytes 
         is.close (  ) ; 
         return bytes; 
      } 


[2004]Fix while error
By Mike { dot } dever { dot } ctr { at } dla { dot } mil on 2009/12/31 14:04:38  Rate
  public static byte [  ]  getBytesFromFile (  File file  )  
     throws IOException 
    {  
     InputStream is = new FileInputStream (  file  ) ; 
  
  
     // Get the size of the file  
     long length = file.length (  ) ; 
  
  
     // You cannot create an array using a long type.  
     // It needs to be an int type.  
     // Before converting to an int type, check  
     // to ensure that file is not larger than Integer.MAX_VALUE.  
     if (  length  >  Integer.MAX_VALUE  )  
      {  
       // File is too large 
       throw new IOException (  "File Too Large For Buffer"  ) ; 
      }  
  
  
     // Create the byte array to hold the data  
     byte [  ]  bytes = new byte [   (  int  )  length  ] ; 
  
  
     // Read in the bytes  
     int offset = 0; 
     int numRead = 0; 
  
  
     while (   ( offset  <  bytes.length )  &&  (  ( numRead = is.read (  bytes, offset, offset  )  )   > = 0 )   )  
      {  
       offset += numRead; 
      }  
  
  
     // Ensure all the bytes have been read in  
     if (  offset  <  bytes.length  )  
      {  
       throw new IOException (  "Could not completely read file " + file.getName (  )   ) ; 
      }  
  
  
     // Close the input stream and return bytes  
     is.close (  ) ; 
     return bytes; 
    }  
 


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


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

Popular Tags