Advertisement
Guest User

ParentClass

a guest
Nov 17th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import java.io.*;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4.  
  5. public class ParentClass{
  6.  
  7. public static void main(String[] args) {
  8.  
  9. File file = new File("C:\\Java_Scratch\\someFile.txt");
  10.  
  11. try {
  12.  
  13. Scanner sc = new Scanner(file);
  14.  
  15. try {
  16. Thread.sleep(5000);
  17. }
  18. catch(InterruptedException ie) {
  19. System.out.print(ie.getStackTrace()) ;
  20.  
  21. }
  22.  
  23. while (sc.hasNextLine()) {
  24. int i = sc.nextInt();
  25. System.out.println(i);
  26. }
  27. sc.close();
  28. }
  29. catch (FileNotFoundException e) {
  30. e.printStackTrace();
  31. }
  32.  
  33.  
  34. try {
  35. int i = ParentClass.countLines("C:\\Java_Scratch\\someFile.txt");
  36. }
  37. catch (IOException ioe) {
  38. System.out.print("ioe" + ioe.getStackTrace() );
  39. }
  40.  
  41. }
  42.  
  43.  
  44. // putting the count function
  45.  
  46.  
  47.  
  48. public static int countLines(String filename) throws IOException {
  49. InputStream is = new BufferedInputStream(new FileInputStream(filename));
  50.  
  51.  
  52. try {
  53. byte[] c = new byte[1024];
  54. int count = 0;
  55. int readChars = 0;
  56. boolean empty = true;
  57. while ((readChars = is.read(c)) != -1) {
  58. empty = false;
  59. for (int i = 0; i < readChars; ++i) {
  60. if (c[i] == '\n') {
  61. ++count;
  62. }
  63. }
  64. }
  65. return (count == 0 && !empty) ? 1 : count;
  66. } finally {
  67. is.close();
  68. }
  69.  
  70. }//END method countLines
  71.  
  72.  
  73.  
  74. // end-count_func
  75.  
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement