Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import java.io.FileReader;
  2. import java.io.BufferedReader;
  3.  
  4. public class Demo {
  5.  
  6.  
  7. public static void readLinesFromFile(String name)
  8. {
  9. FileReader fr = null;
  10. BufferedReader br = null;
  11. String[] tokens;
  12. try
  13. {
  14. fr = new FileReader(name);
  15. br = new BufferedReader(fr);
  16. int counter = 0;
  17. while(true)
  18. {
  19. counter++;
  20. String line = br.readLine();
  21. if(line == null)break;
  22. // tokens = line.split(" ");
  23. // String lastName = tokens[1];
  24.  
  25. System.out.println(counter + " : " + line);
  26. }
  27. }
  28. catch(Exception e)
  29. {
  30. System.out.println(e.getMessage());
  31. }
  32.  
  33. try {
  34.  
  35. fr.close();
  36. br.close();
  37. } catch (Exception e) {
  38. System.out.println(e.getMessage());
  39. }
  40. }
  41.  
  42. public static void readFromFile(String name)
  43. {
  44. FileReader fr = null;
  45. BufferedReader br = null;
  46. String[] tokens;
  47. try
  48. {
  49. fr = new FileReader(name);
  50. br = new BufferedReader(fr);
  51. int counter = 0;
  52. while(true)
  53. {
  54. counter++;
  55. String line = br.readLine();
  56. if(line == null)break;
  57. tokens = line.split(" ");
  58. String lastName = tokens[1];
  59.  
  60. System.out.println(counter + " : " + lastName);
  61. }
  62. }
  63. catch(Exception e)
  64. {
  65. System.out.println(e.getMessage());
  66. }
  67.  
  68. try {
  69.  
  70. fr.close();
  71. br.close();
  72. } catch (Exception e) {
  73. System.out.println(e.getMessage());
  74. }
  75. }
  76. public static void main(String[]args)
  77. {
  78. // readFromFile("names.txt");
  79. // readFromFile("fruits.txt");
  80.  
  81. readLinesFromFile("D:\\words.txt");
  82.  
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement