Skip to content

Instantly share code, notes, and snippets.

@adrientetar
Created November 30, 2012 16:31
Show Gist options
  • Select an option

  • Save adrientetar/4176819 to your computer and use it in GitHub Desktop.

Select an option

Save adrientetar/4176819 to your computer and use it in GitHub Desktop.
Opération sur fichiers
/**
* Ce programme fait diverses opérations sur un fichier texte.
* @author Adrien Tétar
* @version 1.0
*
* Copyright © 2012, Adrien Tétar. All rights reserved.
*/
import java.awt.Component;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Arrays;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
class Main {
private static final Boolean DEBUG = false;
/*
* Triggers b/w simple input function and more
* complicated, bulletproof function.
*/
private static final Boolean BULLETPROOF = true;
private static String[] nom = new String[10];
private static String[] tel = new String[10];
/* Java file chooser, for file abs. path input. */
public static String dialogFichier(Component parent)
{
JFileChooser box = new JFileChooser();
if (box.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION)
{
return box.getSelectedFile().getAbsolutePath();
}
/* NOTE: 'null' case (Cancel button) is handled directly in the program. */
return null;
}
/* Java file chooser, for .txt file abs. path input. */
public static String dialogFichierTxt(Component parent)
{
JFileChooser box = new JFileChooser();
box.setFileFilter(new FileNameExtensionFilter("Fichiers texte", "txt"));
if (box.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION)
{
/* Append '.txt' extension if the user didn't do it. */
if(!box.getSelectedFile().getAbsolutePath().toLowerCase().endsWith(".txt"))
{
return box.getSelectedFile().getAbsolutePath() + ".txt";
}
else
{
return box.getSelectedFile().getAbsolutePath();
}
}
/* NOTE: 'null' case (Cancel button) is handled directly in the program. */
return null;
}
/* Java file chooser, for .txt file saving. */
public static String nouveauFichierTxt(Component parent)
{
JFileChooser box = new JFileChooser();
box.setFileFilter(new FileNameExtensionFilter("Fichiers texte", "txt"));
if (box.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION)
{
/* Append '.txt' extension if the user didn't do it. */
if(!box.getSelectedFile().getAbsolutePath().toLowerCase().endsWith(".txt"))
{
return box.getSelectedFile().getAbsolutePath() + ".txt";
}
else
{
return box.getSelectedFile().getAbsolutePath();
}
}
/* NOTE: 'null' case (Cancel button) is handled directly in the program. */
return null;
}
public static BufferedReader lireFichier(String path) throws FileNotFoundException
{
BufferedReader Fichier = null;
if (BULLETPROOF == false)
{
return Fichier = new BufferedReader(new FileReader(path));
}
else
{
path = null;
/* Avoid crash on blank input. */
if ((path = dialogFichierTxt(null)) != null)
{
Fichier = new BufferedReader(new FileReader(path));
}
/* 'null' value means user clicked on Cancel. Quit. */
else
{
System.exit(0);
}
}
return Fichier;
}
/* Same function as before but totally free of hardcoded path. */
public static BufferedReader lireFichierGen() throws FileNotFoundException
{
BufferedReader Fichier = null;
/* BULLETPROOF. */
String path;
/* Avoid crash on blank input. */
if ((path = dialogFichierTxt(null)) != null)
{
Fichier = new BufferedReader(new FileReader(path));
}
/* 'null' value means user clicked on Cancel. Quit. */
else
{
System.exit(0);
}
return Fichier;
}
/* ==================================== */
/*
* Function n°1.
* Inputs a file and displays its content.
*/
public static void affFichier() throws IOException
{
BufferedReader Fichier = lireFichier("D:\\Documents and Settings\\" +
"ISN\\Mes documents\\Téléchargements\\repertoire.txt");
String ligne;
while ((ligne = Fichier.readLine()) != null)
{
System.out.println(ligne);
}
Fichier.close();
}
/*
* Function n°1.
* Reads a txt file which must have following properties:
* - 10 lines long
* - syntax: <Name>\n<Phone number>\n<...>
*/
public static void ouvertureFichier() throws IOException
{
BufferedReader Fichier = lireFichier("D:\\Documents and Settings\\" +
"ISN\\Mes documents\\Téléchargements\\repertoire.txt");
for (int i=0; i<10; i++)
{
nom[i] = Fichier.readLine();
tel[i] = Fichier.readLine();
}
Fichier.close();
}
/*
* Function n°1.
* Reads a txt file which must have following properties:
* - X lines long
* - syntax: <Name>\n<Phone number>\n<...>
*/
public static void ouvertureFichierGen() throws IOException
{
BufferedReader Fichier = lireFichierGen();
String ligne;
int cpt = 0;
while ((ligne = Fichier.readLine()) != null)
{
/*
* One line out of two will contain names,
* the second ones will contain phone numbers.
*/
if (cpt % 2 == 0)
{
nom[cpt/2] = ligne;
}
else
{
tel[cpt/2] = ligne;
}
cpt++;
}
Fichier.close();
}
public static void affVariables()
{
System.out.println("nom: " + Arrays.toString(nom));
System.out.println("tel: " + Arrays.toString(tel));
}
/* ==================================== */
/*
* Function n°2.
* Input a name from the user, give back the corresponding
* phone number.
*/
public static void rechercheNum() throws IOException
{
/*
* If 'nom' and 'tel' haven't been filled yet,
* call the input function.
*/
if (nom[0] == null || tel[0] == null)
{
ouvertureFichierGen();
}
/* If nothing matches, say it to the user. */
Boolean found = false;
String tmp = JOptionPane.showInputDialog("Quel est le nom de la personne désirée ?");
/* 'null' value means user clicked on Cancel. Quit. */
if (tmp == null)
{
System.exit(0);
}
for (int i=0; i<nom.length; i++)
{
if (nom[i].equals(tmp))
{
JOptionPane.showMessageDialog(null, "Le numéro de téléphone de " + tmp + " est " + tel[i] + ".");
found = true;
// /*
// * Halt the loop to avoid further researches.
// * Note: this will prevent displaying every matches in case of name duplicates.
// */
// i = nom.length - 1;
}
}
if (found == false)
{
JOptionPane.showMessageDialog(null, "Aucune correspondance n'a été trouvée dans le répertoire.");
}
}
/* ==================================== */
/*
* Function n°3.
* Creates a txt file which will store a sentence given by the user,
* encrypted by ASCII+10 method.
* Then, another function will decrypt the message and display it.
*/
public static void cryptPhrase() throws IOException
{
String phrase = JOptionPane.showInputDialog("Quelle est la phrase à crypter ?");
/* 'null' value means user clicked on Cancel. Quit. */
if (phrase == null)
{
System.exit(0);
}
PrintWriter Fichier = null;
if (BULLETPROOF == false)
{
Fichier = new PrintWriter(new BufferedWriter(
new FileWriter("D:\\Documents and Settings\\" +
"ISN\\Mes documents\\Téléchargements\\message.txt")));
}
else
{
String path;
/* Avoid crash on blank input. */
if ((path = nouveauFichierTxt(null)) != null)
{
Fichier = new PrintWriter(new BufferedWriter(new FileWriter(path)));
}
/* 'null' value means user clicked on Cancel. Quit. */
else
{
System.exit(0);
}
}
StringBuffer crypt = new StringBuffer(phrase);
for(int i=0; i<phrase.length(); i++)
{
crypt.setCharAt(i, (char)((phrase.charAt(i)+10) % 128));
}
JOptionPane.showMessageDialog(null, "La phrase a été cryptée avec succès.");
Fichier.println(crypt);
Fichier.close();
}
public static void decryptPhrase() throws IOException
{
BufferedReader Fichier = null;
if (BULLETPROOF == false)
{
Fichier = lireFichier("D:\\Documents and Settings\\" +
"ISN\\Mes documents\\Téléchargements\\message.txt");
}
else
{
Fichier = lireFichierGen();
}
String phrase;
int cpt = 0;
/* In case there's more than one line.. */
while ((phrase = Fichier.readLine()) != null)
{
cpt++;
StringBuffer decrypt = new StringBuffer(phrase);
for(int i=0; i<phrase.length(); i++)
{
decrypt.setCharAt(i, (char)((phrase.charAt(i)-10) % 128));
}
JOptionPane.showMessageDialog(null, "La phrase [" + cpt + "] a été décryptée avec succès: " + decrypt);
}
Fichier.close();
}
/* ==================================== */
public static void main(String[] args) throws IOException
{
/* TODO: add GUI. */
/* Function n°1. */
if (DEBUG)
{
System.out.println("Fonction n°1");
}
// affFichier();
// ouvertureFichier();
ouvertureFichierGen();
affVariables();
/* Function n°2. */
if (DEBUG)
{
System.out.println("Fonction n°2");
}
rechercheNum();
/* Function n°3. */
if (DEBUG)
{
System.out.println("Fonction n°3");
}
cryptPhrase();
decryptPhrase();
if (DEBUG)
{
System.out.println("nom: " + Arrays.toString(nom));
System.out.println("tel: " + Arrays.toString(tel));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment