import java.io.*;
import java.net.*;
public class FtpDelete {
private static final String host = "<host>";
private static final int port = 21;
private static final String user = "<username>";
private static final String pass = "<password>";
private static final String filename = "<file>";
private static void expect(BufferedReader in, String prefix) throws IOException {
int tries = 0;
while (!in.ready()) {
try { Thread.sleep(500); } catch (InterruptedException ex) {}
tries++;
if (tries == 4) {
throw new IOException("No input for more than two seconds.");
}
}
String line = "";
boolean prefixSeen = false;
while (in.ready() && (line = in.readLine()) != null) {
prefixSeen |= line.startsWith(prefix);
System.out.println(line);
}
if (!prefixSeen) {
throw new IOException(prefix + " expected. Not seen.");
}
}
public static void main(String[] args) throws Exception {
try(
Socket ctrl = new Socket(host, port);
PrintWriter out = new PrintWriter(ctrl.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(
ctrl.getInputStream()));
) {
expect(in, "220");
out.println("USER " + user);
expect(in, "331");
out.println("PASS " + pass);
expect(in, "230");
out.println("DELE " + filename);
expect(in, "200");
out.println("QUIT");
}
}
}