Advertisement
Guest User

Untitled

a guest
Sep 26th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class LineCounter{
  6.  
  7. private File file;
  8.  
  9. public LineCounter(File file){
  10. this.file = file;
  11. }
  12.  
  13. public void countLines(){
  14. int count = 0;
  15.  
  16. try(Scanner scan = new Scanner(new BufferedReader(new FileReader(file)))){
  17.  
  18. while(scan.hasNextLine()){
  19.  
  20. count++;
  21. }
  22.  
  23. }catch(Exception e){
  24. System.out.println("Nuk egziston file");
  25. }
  26.  
  27. System.out.println("File i ka " + count + " rreshta");
  28.  
  29. }
  30.  
  31. public static void main(String[] args) throws Exception{
  32.  
  33. File f = new File("test123.txt");
  34.  
  35. if (!f.exists()){
  36. f.createNewFile();
  37. }
  38.  
  39.  
  40. System.out.println("123");
  41.  
  42. LineCounter lc = new LineCounter(f);
  43.  
  44. lc.countLines();
  45.  
  46. }
  47.  
  48.  
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement