Program
import java.io.*;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
public class AssetFinder {
static String fileName = "input.txt";
String line = null;
static String url="";
static HashMap<String,String> data=new HashMap<String,String>();
public static void main(String [] args)
{
AssetFinder asset=new AssetFinder();
url=args[0];
asset.readid(fileName);
asset.fetch(data);
asset.writedetails(data);
}
public void readid(String fileName)
{
{
// FileReader reads text files in the default encoding.
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(fileName)));
while((line = bufferedReader.readLine()) != null) {
System.out.println(line);
data.put(line,"No Data");
}
// // Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'"+ex);
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
public void fetch(HashMap<String,String> data)
{
String command="curl.exe "+url;
for(HashMap.Entry<String, String> resultdata : data.entrySet())
{
String temp=this.fetchdetails(command+resultdata.getKey());
data.put(resultdata.getKey(), temp);
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public String fetchdetails(String command)
{
String result = "";
Process p;
try {
p = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
result = builder.toString();
System.out.println(result);
} catch (IOException e) {
System.out.print("error");
e.printStackTrace();
}
return result;
}
public void writedetails(HashMap<String,String> data)
{
try {
// Assume default encoding.
String path="output.txt";
File file = new File(path);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter =
new FileWriter(file.getAbsolutePath());
// Always wrap FileWriter in BufferedWriter.
BufferedWriter bufferedWriter =
new BufferedWriter(fileWriter);
// Note that write() does not automatically
// append a newline character.
for(HashMap.Entry<String, String> resultdata : data.entrySet())
{
bufferedWriter.newLine();
bufferedWriter.write(resultdata.getKey());
bufferedWriter.newLine();
bufferedWriter.write(resultdata.getValue());
bufferedWriter.newLine();
bufferedWriter.newLine();
}
// Always close files.
bufferedWriter.close();
}
catch(IOException ex) {
System.out.println(
"Error writing to file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
Comments
Post a Comment