Snippets Java

Abastract

Classe abstraite

public abstract class Forme {

   public abstract float perimetre(); //methode abstraite
   public abstract float surface(); //methode abstraite
   public double rapport() {
      return surface() / perimetre();
   }
}

Date

Formater la date selon une syntaxe

Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("Call event " + dateFormat.format(now));

Millisecond to hh:mm:ss

int seconds = (int) (milliseconds / 1000) % 60 ;
int minutes = (int) ((milliseconds / (1000*60)) % 60);
int hours   = (int) ((milliseconds / (1000*60*60)) % 24);
String.format("%02d:%02d:%02d", hours, minutes, seconds);

File

Fichier ressource

File file = new File(getClass().getResource("name.txt").getFile());

Fichier temporaire

File temp = File.createTempFile("nom_du_fichier", ".xls");

Parcourir un fichier ligne par ligne

try {
	InputStream inputStream = new FileInputStream(new File("readme.txt"));
	InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
	BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
	String line;
	while ((line = bufferedReader.readLine()) != null) {
	   System.out.println(line);
	}
	bufferedReader.close();
} catch (Exception e) {
	System.out.println(e.toString());
}

Trier une liste de fichier par date

List<File> files = new ArrayList<>();
files .add(file1);
files .add(file2);
Collections.sort(files , new Comparator<File>() {
				@Override
				public int compare(File f1, File f2) {
					return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
				}
			});
// The list files is now ordered by dates

Map

Itérer sur un set

     HashSet<String> hashSet= new HashSet<String>();
 
     //add elements to HashSet
     hashSet.add("fred_1");
     hashSet.add("fred_2");
     // [...]
     hashSet.add("fred_n");
 
     Iterator<String> iterator = hashSet.iterator();
     while(iterator.hasNext()){
        System.out.println(iterator .next());
     }

Map

Itérer sur une map

for (Map.Entry<String, Integer> entry : map.entrySet()) {
    System.out.println("Key :"entry.getKey() + " - Value:" + entry.getValue());
}

String

Classe Scanner

Scanner scanner = new Scanner(System.in);
System.out.println("Enter a word :");
String string = scanner.nextLine();
System.out.println("You have entered : " + string);

Thread

public class Counter {

	private int step;
	private Thread thread;
	private Chronometer chronometer;

	public void start() {

		chronometer = new Chronometer();
		thread = new Thread(chronometer);
		thread.start();
	}

	public void stop() {
		chronometer.setRunning(false);
	}

	private class Chronometer implements Runnable {

		boolean isRunning;

		public Chronometer() {
			super();
			this.isRunning = true;
		}

		public boolean isRunning() {
			return isRunning;
		}

		public void setRunning(boolean isRunning) {
			this.isRunning = isRunning;
		}

		public void run() {

			do {
				step = step + step;
				System.out.println("chronometer value :" + step);

				try {
					Thread.sleep(4000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} while (isRunning);
			System.out.println("thread chronometer will stop");
		}
	}
}