/*
Read File in String Using Java BufferedInputStream Example.
This example shows how to read a file content into a Sting
object using available and read methods of Java
BufferedInputStream.
*/
import java.io.*;
public class ReadFileInToString {
public static void main(String[] args) {
//create file object
File file = new File("C://FileIO//ReadFile.txt");
BufferedInputStream bin = null;
try
{
//create FileInputStream object
FileInputStream fin = new FileInputStream(file);
//create object of BufferedInputStream
bin = new BufferedInputStream(fin);
//create a byte array
byte[] contents = new byte[1024];
int bytesRead=0;
String strFileContents;
while( (bytesRead = bin.read(contents)) != -1){
strFileContents = new String(contents, 0, bytesRead);
System.out.print(strFileContents);
}
}
catch(FileNotFoundException e)
{
System.out.println("File not found" + e);
}
catch(IOException ioe)
{
System.out.println("Exception while reading the file "
+ ioe);
}
finally
{
//close the BufferedInputStream using close method
try{
if(bin != null)
bin.close();
}catch(IOException ioe)
{
System.out.println("Error while closing the stream :"
+ ioe);
}
}
}
}
/*
Output would be
This file is for demonstration of how to read a file into String using Java
BufferedInputStream.
*/
Bookmark/Search this post with:
if-else
In a test result are declared as follows
if % marks >=80 then the result declared is "PASSED WITH STAR"
if %marks <80 but >=60 then the candidate is awarded "FIRST DIVISION"
if %marks <60 but >=40 then the candidate is awarded "SECOND DIVISION"
if % marks <40 then the result is declaredas "FAILED"
write a program in java to input the %marks and print the result
import java.io.*; class
Post new comment