Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package domein;
- import java.io.IOException;
- import java.net.Socket;
- import java.util.Formatter;
- import java.util.Scanner;
- public class FileTransfer {
- private Socket socket;
- private Formatter output;
- private Scanner input;
- public FileTransfer(String host) {
- try {
- socket = new Socket("127.0.0.1", 44444);
- output = new Formatter(socket.getOutputStream());
- output.flush();
- input = new Scanner(socket.getInputStream());
- } catch (IOException e) {
- System.out.println("Probleem " + e.getMessage());
- }
- }
- public String readFile(String fileNaam) {
- // Verzoek server om bestand 'fileNaam' door te sturen en lees het bestand in als de server het doorstuurt
- output.format("%s%n", "READ");
- output.format("%s%n", fileNaam);
- output.flush();
- String reactie = input.nextLine();
- if (reactie.equals("FOUND")) {
- String nextLine = input.nextLine(), res = "";
- while (!nextLine.contains("*E*O*F*")) {
- res += nextLine + "\n";
- nextLine = input.nextLine();
- }
- return res;
- }
- return "BESTAND NIET GEVONDEN";
- }
- /*
- Meld de server dat je het gewijzigde bestand gaat doorsturen, geef de eventueel gewijzigde bestandsnaam mee
- door bij onveranderde bestandsnaam zal de server het originele bestand overschrijven stuur het bestand door naar de server
- */
- public void updateFile(String fileContents, String fileNaam) {
- // TODO
- }
- public void closeConnection() {
- try {
- socket.close();
- } catch (IOException ex) {
- System.out.println("Probleem " + ex.getMessage());
- }
- }
- // De laatste regel moet eindigen met de systeem einde regel. Enkel \n of \r is niet goed.
- public String fixEOL(String text) {
- if (!text.endsWith(System.lineSeparator())) {
- int count = 0;
- int lastChar = text.length() - 1;
- if (lastChar >= 0 && (text.charAt(lastChar) == '\n' ||
- text.charAt(lastChar) == '\r')) {
- count++;
- }
- if (lastChar > 0 && (text.charAt(lastChar - 1) == '\r')) {
- count++;
- }
- if (count > 0) {
- text = text.substring(0, text.length() - count);
- }
- return text + System.lineSeparator();
- }
- return text;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement