Advertisement
Booster

ReadFromFileNoCatchExceptions

Dec 11th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. package exceptions;
  2.  
  3. import java.util.Scanner;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7.  
  8. public class ReadFromFile {
  9. /**
  10.  * Прочетете файла ред по ред с класа java.util.Scanner и добавяйте редовете в StringBuilder.
  11.  * Декларирайте в сигнатурата на метода,
  12.  * че изхвърля IOException и не обработвайте никакви изключения в него.
  13.  * @param args
  14.  */
  15.         public static void main(String[] args) throws IOException {
  16.                
  17.                 File file = new File("C:\\Users\\Azbe\\Desktop\\zapisVideo.txt");
  18.                 FileInputStream fis = new FileInputStream(file);
  19.                 Scanner input = new Scanner(fis);
  20.                 StringBuilder strBuild = new StringBuilder();
  21.                 String nl = System.getProperty("line.separator");
  22.                
  23.                 try {
  24.                         while (input.hasNextLine()){
  25.                                 strBuild.append(input.nextLine() + nl);
  26.                         }
  27.                 } finally {
  28.                         input.close();
  29.                 }
  30.                 System.out.print(strBuild);
  31.                
  32.                
  33.         }
  34.        
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement