Advertisement
tjb1

Untitled

Nov 24th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Chap5prob14_fileDISPLAY {
  7.  
  8. //data members
  9. // are the basic characteristics of the object
  10. private String filename;
  11.  
  12.  
  13.  
  14. //method members
  15. //are the functionalities of the object
  16.  
  17. //constructors These methods create (instantiate) an instance of the object
  18.  
  19. /**
  20. * Default constructor sets the filename to ""
  21. */
  22. public Chap5prob14_fileDISPLAY(){
  23. filename = "";
  24.  
  25. }
  26.  
  27. /**
  28. * full constructor sets the filename according to the parameters
  29. * @param filename is the filename
  30. */
  31. public Chap5prob14_fileDISPLAY(String filename){
  32. this.filename = filename;
  33.  
  34. }
  35.  
  36. //getters
  37.  
  38. /**
  39. * this method returns the filename
  40. * @return the filename
  41. */
  42. public String getfilename(){
  43. return filename;
  44. }
  45.  
  46.  
  47.  
  48. //setters
  49.  
  50. /**
  51. * This method resets the filename according to the parameter
  52. * @param value will be set as the new value
  53. */
  54. public void setfilename( String value ) {
  55. this.filename = value;
  56. }
  57.  
  58.  
  59. //toString method
  60.  
  61. public String toString(){
  62. String str;
  63.  
  64. str = ("\tThe file name you entered is: " + filename);
  65. return str;
  66. }
  67.  
  68. //all other object specific methods
  69.  
  70. //file must be a .txt file, do not enter extension
  71.  
  72.  
  73. public StringBuffer displayHead() throws IOException {
  74. File dataFile = new File(filename+".txt");
  75. Scanner input = new Scanner(dataFile);
  76. StringBuffer file = new StringBuffer("");
  77. double lineCount = 0;
  78.  
  79. while(input.hasNextLine() && lineCount <= 4){
  80. file.append(input.next() + "\n");
  81.  
  82. lineCount++;
  83. }
  84.  
  85. return file;
  86. }
  87.  
  88. public StringBuffer displayContents() throws IOException {
  89. File dataFile = new File(filename+".txt");
  90. Scanner input = new Scanner(dataFile);
  91. StringBuffer file = new StringBuffer("");
  92.  
  93. while(input.hasNextLine()){
  94. file.append(input.next() + "\n");
  95. }
  96.  
  97. return file;
  98.  
  99. }
  100.  
  101. public StringBuffer displayWithLineNumbers() throws IOException {
  102. File dataFile = new File(filename+".txt");
  103. Scanner input = new Scanner(dataFile);
  104. StringBuffer file = new StringBuffer("");
  105. int i = 0;
  106.  
  107. while(input.hasNextLine()){
  108. file.append(i + "\t" + input.next() + "\n");
  109. i++;
  110. }
  111.  
  112. return file;
  113.  
  114. }
  115.  
  116. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement