Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. import java.io.*;
  2. class Assignment22{
  3. static void println(String str){
  4. System.out.println(str);
  5. }
  6. public static void main(String[] args)throws IOException{
  7. if(args.length < 2){
  8. println("Invalid number of arguments");
  9. println("USAGE: java Assignment22 input.txt out1.txt [ out2.txt ... ]");
  10. }
  11. else{
  12. String in_file = args[0];
  13. // Opening the input file
  14. RandomAccessFile in = new RandomAccessFile(in_file, "r");
  15. // Obtaining size of the file
  16. long n = in.length();
  17. // Average size of all files excpet
  18. long n1 = n / (args.length - 1);
  19. // Size of the last output file
  20. long n2 = n - (n1 * (args.length - 2));
  21.  
  22. // Splitting the files
  23. for(int i = 1; i < args.length - 1; i++){
  24. FileOutputStream out = new FileOutputStream(args[i]);
  25. // Copying the content to output files except the last
  26. for(int j = 1; j <= n1; j++){
  27. out.write(in.read());
  28. }
  29. out.close();
  30. }
  31. // Copying remaining content to last output file
  32. FileOutputStream out = new FileOutputStream(args[args.length - 1]);
  33. // Copying the content to output files except the last
  34. for(int j = 1; j <= n2; j++){
  35. out.write(in.read());
  36. }
  37. out.close();
  38. in.close();
  39. }
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement