1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
| import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class BrowserControl implements IFile {
// Méthode qui détermine si l'application est lançé sur un OS un windows ou pas
public static boolean isWindowsPlatform()
{
String os = System.getProperty("os.name");
if ( os != null && os.startsWith(WIN_ID))
return true;
else
return false;
}
// Fonction qui permet d'écrire dans un fichier
public void ecrire(String nomFic, String texte) {
//on va chercher le chemin et le nom du fichier et on me tout ça dans un String
String adressedufichier = System.getProperty("user.dir") + "/"+ nomFic;
//on met try si jamais il y a une exception
try
{
/**
* BufferedWriter a besoin d un FileWriter,
* les 2 vont ensemble, on donne comme argument le nom du fichier
* true signifie qu on ajoute dans le fichier (append), on ne marque pas par dessus
*/
FileWriter fw = new FileWriter(adressedufichier, true);
// le BufferedWriter output auquel on donne comme argument le FileWriter fw cree juste au dessus
BufferedWriter output = new BufferedWriter(fw);
//on marque dans le fichier ou plutot dans le BufferedWriter qui sert comme un tampon(stream)
output.write(texte);
//on peut utiliser plusieurs fois methode write
output.flush();
//ensuite flush envoie dans le fichier, ne pas oublier cette methode pour le BufferedWriter
output.close();
//et on le ferme
System.out.println("fichier créé");
}
catch(IOException ioe){
System.out.print("Erreur : ");
ioe.printStackTrace();
}
}
// Fonction qui affiche une URL sur Windows ou UNIX
public static void displayURL(String url)
{
boolean windows = isWindowsPlatform();
String cmd = null;
try
{
if (windows)
{
// cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
Process p = Runtime.getRuntime().exec(cmd);
}
else
{
// Under Unix, Netscape has to be running for the "-remote"
// command to work. So, we try sending the command and
// check for an exit value. If the exit command is 0,
// it worked, otherwise we need to start the browser.
// cmd = 'netscape -remote openURL(http://www.javaworld.com)'
cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";
Process p = Runtime.getRuntime().exec(cmd);
try
{
// wait for exit code -- if it's 0, command worked,
// otherwise we need to start the browser up.
int exitCode = p.waitFor();
if (exitCode != 0)
{
// Command failed, start up the browser
// cmd = 'netscape http://www.javaworld.com'
cmd = UNIX_PATH + " " + url;
p = Runtime.getRuntime().exec(cmd);
}
}
catch(InterruptedException x)
{
System.err.println("Error bringing up browser, cmd='" +
cmd + "'");
System.err.println("Caught: " + x);
}
}
}
catch(IOException x)
{
// couldn't exec browser
System.err.println("Could not invoke browser, command=" + cmd);
System.err.println("Caught: " + x);
}
}
// Fonction permettant de lancer firefox
public static void launchFirefox() {
final String CHEMIN = "C:\\My\\Scripts\\";
try {
// On patiente 2 secondes avant d'ouvrir de nouveaux URL
try {
System.out.println("Pour la fluidité du programme, on lance Mozilla Firefox installé içi:" +CHEMIN + "start_firefox.bat"+"\nPatienter pendant son initialisation ...");
Thread.sleep(2000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
String[] commande = {"cmd.exe", "/C", CHEMIN + "start_firefox.bat"};
Runtime.getRuntime().exec(commande);
// On patiente 2 secondes
try {
Thread.sleep(5000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// Fonction permettant de lire un fichier ligne par ligne et d'afficher les URL contenant http ou https
public static void openURLForAllLine(File fileForFileName){
String urlName=""; // Stocke le nom de l'URL en cours d'affichage
// Lire un fichier ligne par ligne
try{
// Création du flux bufférisé sur un FileReader, immédiatement suivi par un
// try/finally, ce qui permet de ne fermer le flux QUE s'il le reader
// est correctement instancié (évite les NullPointerException)
BufferedReader buff = new BufferedReader(new FileReader(fileForFileName));
try {
String line;
// Lecture du fichier ligne par ligne. Cette boucle se termine
// quand la méthode retourne la valeur null.
String[] urlTab = new String[9];
int cpte=9;
int indice = 0;
while (cpte!=0) {
// Tant qu'il y a des lignes à lire et si http ou https présent comme occurence dans la ligne en cours
while ((line = buff.readLine()) != null && (line.contains("http") || line.contains("https"))) {
// on stocke la ligne dans la variable urlName
urlTab[indice]=line;
// On affiche l'URL dans le navigateur
for (int i=0;i<urlTab.length;i++) {
displayURL(urlTab[i]);
}
// On patiente 5 secondes avant d'ouvrir de nouveaux URL
try {
Thread.sleep(10000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
} finally {
// dans tous les cas, on ferme nos flux
buff.close();
}
} catch (IOException ioe) {
// erreur de fermeture des flux
System.out.println("Erreur --" + ioe.toString());
}
}
/* Main principale :
* Lancement du programme
* On récupère le dossier que l'utilisateur aura spécifié
* On récupère le nom du fichier que l'utilisateur aura spécifié
* On appelle la fonction qui ouvre les URL
*/
public static void main(String[] args)
{
launchFirefox();
Scanner sc = new Scanner(System.in);
// Demander le dossier du fichier (se termine par un \)
System.out.println("\nEntrer le nom du dossier où se situe le fichier avec un \\ en fin de chaine");
java.awt.Toolkit.getDefaultToolkit().beep();
String pathFolder = sc.nextLine();
// Demander le nom du fichier avec son extension
System.out.println("Entrer le nom du fichier avec son extension");
java.awt.Toolkit.getDefaultToolkit().beep();
String fileName = sc.nextLine();
File pathFile= new File(pathFolder+fileName); // Stocke le chemin d'accès au fichier
openURLForAllLine(pathFile);
}
} |
Partager