Advertisement
Ivan_Bochev

Exceptionss

Nov 27th, 2019 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. package t3;
  2. //import java.io.*;
  3. import java.io.BufferedReader;
  4. import java.io.FileInputStream;
  5. import java.io.InputStreamReader;
  6. import java.util.Scanner;
  7. import java.io.FileNotFoundException;
  8. import java.io.IOException;
  9.  
  10.  
  11. public class Exceptionss
  12. {
  13.  
  14.     // Read File Method -----------------------------------------------------------------
  15.     public static void readFile(String fileName)
  16.     {
  17.  
  18.        
  19.         try
  20.         {
  21.             FileInputStream fis = new FileInputStream(fileName); // Входен стрийм от файл
  22.  
  23.             BufferedReader in = new BufferedReader(new InputStreamReader(fis)); // Буфер за прочетеното
  24.        
  25.             String tmp = null; // Временна променлива за разпечатване на съдържанието на файла
  26.  
  27.             while ((tmp = in.readLine()) != null)
  28.                 {
  29.                     System.out.println(tmp);
  30.                 }
  31.  
  32.             in.close();
  33.             fis.close();
  34.  
  35.         }
  36.        
  37.         catch (FileNotFoundException e)
  38.         {
  39.           System.out.println("The File \"" + fileName + "\" does not exist! Unable to read it.");
  40.       }  
  41.        
  42.        
  43.         catch (IOException e)
  44.      {
  45.             e.printStackTrace();
  46.      }
  47.            
  48.     }
  49.    
  50.    
  51.     // MAIN ////////////////////////////////////////////////////
  52.     public static void main(String[] args)
  53.     {
  54.        
  55.         Scanner input = new Scanner(System.in);
  56.        
  57.         System.out.print("Input file name : ");
  58.         String fileName = input.nextLine();
  59.         System.out.println();
  60.        
  61.             readFile(fileName);
  62.            
  63.            
  64.             input.close();
  65.    
  66.     }// END MAIN
  67.  
  68. }// END CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement