Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.io.LineNumberReader;
  7.  
  8. public class FileCountExample {
  9.  
  10. public static void main(String[] args) {
  11. File file = new File("file1.txt");
  12.  
  13. if (file.exists()) {
  14. System.out.println("Total Lines=" + getLineCount(file));
  15. getmaxline(File file);
  16. } else {
  17. System.out.println("File does not exists!");
  18. }
  19. }
  20.  
  21. private static int getLineCount(File file) {
  22. int linenumber = 0;
  23. FileReader fr = null;
  24. try {
  25. fr = new FileReader(file);
  26.  
  27. /**
  28. * buffered character-input stream that keeps track of line numbers
  29. */
  30. LineNumberReader lnr = new LineNumberReader(fr);
  31. while (lnr.readLine() != null) {
  32. linenumber++;
  33. }
  34. lnr.close();
  35.  
  36. return linenumber;
  37. }
  38.  
  39. finally {
  40. if (null != fr)
  41. try {
  42. r.close();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. return linenumber;
  48. }
  49.  
  50. public static int getmaxline(File file) {
  51. int lines =0,i;
  52. int characters[1000][1000];
  53. int maxCharacters =0;
  54. String longestLine= "";
  55.  
  56. while(input.hasNextLine()){
  57. String line = input.nextLine();
  58. lines++;
  59. characters[lines][1]=line.length();
  60. }
  61. for(int i=lines; i>=0; i--){
  62. if(maxCharacters< characters[lines][i])
  63. maxCharacters=characters[lines][i];
  64. }
  65. System.out.println("max number of characters :"+maxCharacters);
  66. for(int i=lines; i>=0; i--){
  67. if(maxCharacters< characters[lines][i])
  68. maxCharacters=characters[lines][i];
  69. }
  70. }
  71.  
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement