Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. package domein;
  2.  
  3.  
  4. import java.io.IOException;
  5. import java.net.Socket;
  6. import java.util.Formatter;
  7. import java.util.Scanner;
  8.  
  9. public class FileTransfer {
  10. private Socket socket;
  11. private Formatter output;
  12. private Scanner input;
  13.  
  14. public FileTransfer(String host) {
  15. try {
  16. socket = new Socket("127.0.0.1", 44444);
  17.  
  18. output = new Formatter(socket.getOutputStream());
  19. output.flush();
  20.  
  21. input = new Scanner(socket.getInputStream());
  22.  
  23. } catch (IOException e) {
  24. System.out.println("Probleem " + e.getMessage());
  25. }
  26. }
  27.  
  28. public String readFile(String fileNaam) {
  29. // Verzoek server om bestand 'fileNaam' door te sturen en lees het bestand in als de server het doorstuurt
  30. output.format("%s%n", "READ");
  31. output.format("%s%n", fileNaam);
  32. output.flush();
  33. String reactie = input.nextLine();
  34. if (reactie.equals("FOUND")) {
  35. String nextLine = input.nextLine(), res = "";
  36. while (!nextLine.contains("*E*O*F*")) {
  37. res += nextLine + "\n";
  38. nextLine = input.nextLine();
  39. }
  40. return res;
  41. }
  42. return "BESTAND NIET GEVONDEN";
  43. }
  44.  
  45. /*
  46. Meld de server dat je het gewijzigde bestand gaat doorsturen, geef de eventueel gewijzigde bestandsnaam mee
  47. door bij onveranderde bestandsnaam zal de server het originele bestand overschrijven stuur het bestand door naar de server
  48. */
  49. public void updateFile(String fileContents, String fileNaam) {
  50. // TODO
  51. }
  52.  
  53. public void closeConnection() {
  54. try {
  55. socket.close();
  56. } catch (IOException ex) {
  57. System.out.println("Probleem " + ex.getMessage());
  58. }
  59. }
  60.  
  61. // De laatste regel moet eindigen met de systeem einde regel. Enkel \n of \r is niet goed.
  62. public String fixEOL(String text) {
  63. if (!text.endsWith(System.lineSeparator())) {
  64. int count = 0;
  65. int lastChar = text.length() - 1;
  66. if (lastChar >= 0 && (text.charAt(lastChar) == '\n' ||
  67. text.charAt(lastChar) == '\r')) {
  68. count++;
  69. }
  70. if (lastChar > 0 && (text.charAt(lastChar - 1) == '\r')) {
  71. count++;
  72. }
  73. if (count > 0) {
  74. text = text.substring(0, text.length() - count);
  75. }
  76. return text + System.lineSeparator();
  77. }
  78. return text;
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement