Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. import java.io.EOFException;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.RandomAccessFile;
  7. import java.util.Scanner;
  8.  
  9. public class Randomaccessfile {
  10.  
  11. public static void Create(String inputfile, String outputfile) throws IOException
  12. {
  13. Scanner user = new Scanner(System.in);
  14.  
  15. System.out.println("Please Enter an input file name:");
  16. inputfile = user.nextLine();
  17.  
  18. File file = new File(inputfile + ".txt");
  19.  
  20. //Create the file
  21. if (file.createNewFile()){
  22. System.out.println("File is created!");
  23. }else{
  24. System.out.println("File already exists. ");
  25. }
  26.  
  27. //Write Content
  28. FileWriter writer = new FileWriter(file);
  29. writer.write("Test data"); //delete when done
  30. writer.close();
  31.  
  32.  
  33. //Delete old file if it exists
  34. if (file.exists()){
  35. file.delete();
  36. FileWriter writer1 = new FileWriter(file + ".txt");
  37. writer1.write("replaced"); //delete when done
  38. writer1.close();
  39. }
  40.  
  41. //Create output file
  42. System.out.println("Please Enter an output file name:");
  43. outputfile = user.nextLine();
  44.  
  45. File file2 = new File(outputfile);
  46.  
  47. //Delete old file if it exists
  48. if (file2.exists()){
  49. file2.delete();
  50. FileWriter writer2 = new FileWriter(file2);
  51. writer2.write("replaced"); //delete when done
  52. writer2.close();
  53. }
  54.  
  55. //Create the file
  56. if (file.createNewFile()){
  57. System.out.println("File is created!");
  58. }else{
  59. System.out.println("File already exists. ");
  60. }
  61.  
  62. // Create input and output file streams
  63. Scanner fin = new Scanner (new FileInputStream(file));
  64. RandomAccessFile raf = new RandomAccessFile(file2, "rw");
  65. Student rec = new Student();
  66. raf.close();
  67. fin.close();
  68. user.close();
  69.  
  70. }
  71.  
  72. public static void File() throws IOException{
  73. Scanner scanner = new Scanner(System.in);
  74. String filename = ("");
  75. System.out.println("Which file would you like work with?");
  76. filename = scanner.nextLine();
  77. RandomAccessFile raf = new RandomAccessFile(filename + ".txt", "rw");
  78. scanner.close();
  79.  
  80. }
  81.  
  82. public static void Display(Student rec, RandomAccessFile raf) throws IOException{
  83. File();
  84. print(raf,rec);
  85. raf.close();
  86.  
  87. }
  88.  
  89. public static void print (RandomAccessFile raf, Student rec) throws IOException
  90. {
  91. raf.seek(0);
  92. try {
  93. while (true){
  94. rec.readFromFile(raf);
  95. System.out.println(rec);
  96. }
  97. }
  98. catch (EOFException e){
  99.  
  100. }
  101. raf.close();
  102. }
  103.  
  104. public static void Add(RandomAccessFile raf, Student rec) throws IOException
  105. {
  106. File();
  107. Scanner keyIn = new Scanner(System.in);
  108. // Write a new record to the end of the random access file
  109. System.out.print("Enter your first name, last name, student ID, and GPA: ");
  110. rec.readFromTextFile(keyIn);
  111. raf.seek(raf.length());
  112. rec.writeToFile(raf);
  113. System.out.println("Record with new addition:");
  114. print (raf, rec);
  115.  
  116. raf.close();
  117. }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement