Boa tarde, eu estava fazendo um projeto, em que eu tenho que pegar arquivo de um download e quando finalizar, ler o arquivo .txt, comparar com outro se os txt forem diferentes, baixar outro arquivo.
Contudo, só consigo fazer os arquivos serem baixados juntos.(Obs: ainda nao fiz a parte de ler o arquivo)
Alguém poderia me dizer como inicio 1 classe apenas quando terminar o processo de outra?
Aqui está o código:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
/**
*
-
@author A
*/
public class NovoClass1 implements Runnable{
String link;
File out;
static boolean arquivo2;
public NovoClass1(String link, File out){
this.link = link;
this.out = out;
}
@Override
public void run() {
try{
arquivo2 = false;
URL url = new URL(link);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
double fileSize = (double)http.getContentLengthLong();
BufferedInputStream in = new BufferedInputStream(http.getInputStream());
FileOutputStream fos = new FileOutputStream(this.out);
BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
byte[] buffer = new byte[1024];
double downloaded = 0.00;
int read = 0;
double percentDownloaded = 0.00;
while((read = in.read(buffer, 0 , 1024))>= 0){
bout.write(buffer,0,read);
downloaded += read;
percentDownloaded = (downloaded*100)/fileSize;
String percent = String.format("%.4f", percentDownloaded);
System.out.println("Downloaded" + percent + "% of a file");
}
bout.close();
in.close();
System.out.println("Download complete");
arquivo2 = true;
}
catch(IOException ex){
ex.printStackTrace();
}
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
import java.io.File;
/*
- To change this license header, choose License Headers in Project Properties.
- To change this template file, choose Tools | Templates
- and open the template in the editor.
*/
/**
*
-
@author A
*/
public class NovoClass {
public static void main(String[]args){String link = “https://…txt”;
File out = new File(“G:\pastas\…txt”);
new Thread(new NovoClass1(link,out)).start();
if(NovoClass1.arquivo2 = true){
link = “https://…2.txt”;
out = "G:\pastas\…2.txt"
new Thread(new NovoClass1(link,out)).start();
}
}
}