Read File in String Using Java BufferedInputStream Example

  1. /*
  2. Read File in String Using Java BufferedInputStream Example.
  3. This example shows how to read a file content into a Sting
  4. object using available and read methods of Java
  5. BufferedInputStream.
  6. */
  7.  
  8. import java.io.*;
  9.  
  10. public class ReadFileInToString {
  11.  
  12. public static void main(String[] args) {
  13.  
  14. //create file object
  15. File file = new File("C://FileIO//ReadFile.txt");
  16. BufferedInputStream bin = null;
  17.  
  18. try
  19. {
  20. //create FileInputStream object
  21. FileInputStream fin = new FileInputStream(file);
  22.  
  23. //create object of BufferedInputStream
  24. bin = new BufferedInputStream(fin);
  25.  
  26. //create a byte array
  27. byte[] contents = new byte[1024];
  28.  
  29. int bytesRead=0;
  30. String strFileContents;
  31.  
  32. while( (bytesRead = bin.read(contents)) != -1){
  33.  
  34. strFileContents = new String(contents, 0, bytesRead);
  35. System.out.print(strFileContents);
  36. }
  37.  
  38. }
  39. catch(FileNotFoundException e)
  40. {
  41. System.out.println("File not found" + e);
  42. }
  43. catch(IOException ioe)
  44. {
  45. System.out.println("Exception while reading the file "
  46. + ioe);
  47. }
  48. finally
  49. {
  50. //close the BufferedInputStream using close method
  51. try{
  52. if(bin != null)
  53. bin.close();
  54. }catch(IOException ioe)
  55. {
  56. System.out.println("Error while closing the stream :"
  57. + ioe);
  58. }
  59.  
  60. }
  61. }
  62.  
  63. }
  64.  
  65.  
  66. /*
  67. Output would be
  68. This file is for demonstration of how to read a file into String using Java
  69. BufferedInputStream.
  70. */

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

  1. import java.io.*;
  2. class Marks
  3. {
  4. double percentage;
  5. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  6. void main()throws IOException
  7. {
  8. System.out.print("Enter Percentage Marks:");
  9. percentage=Double.parsedouble(br.readLine());
  10. if(percentage>=80)
  11. {
  12. System.out.println("PASSED WITH STAR");
  13. }
  14. else if(percentage<80 || >=60)
  15. {
  16. System.out.println("FIRST DIVISION");
  17. }
  18. else if(percentage<60 || >=40)
  19. {
  20. System.out.println("SECOND DIVISION");
  21. }
  22. else if(percentage<40)
  23. {
  24. System.out.println("FAILED");
  25. }
  26. else()
  27. {
  28. System.out.println("ENTER CORRECT PERCENT MARKS");
  29. }
  30. }
  31. }

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!