ppp
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Helpers.java 1.7KB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import java.io.*;
  2. import java.math.BigInteger;
  3. import java.security.MessageDigest;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.util.ArrayList;
  6. public class Helpers {
  7. // Lesen einer Textdatei in ein Stringarray
  8. public static String[] readTextdatei(String name) {
  9. String line;
  10. ArrayList<String> result = new ArrayList<String>();
  11. try {
  12. FileReader fr = new FileReader(name);
  13. BufferedReader br = new BufferedReader(fr);
  14. while ((line = br.readLine()) != null)
  15. result.add(line);
  16. br.close();
  17. }
  18. catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. return result.toArray(new String[]{});
  22. }
  23. // Schreiben eines Stringarray in eine Textdatei
  24. public static void writeTextdatei(String name, String[] lines) {
  25. try {
  26. FileWriter fw = new FileWriter(name);
  27. BufferedWriter bw = new BufferedWriter(fw);
  28. PrintWriter pw = new PrintWriter(bw);
  29. for (int i = 0; i < lines.length; i++)
  30. pw.println(lines[i]);
  31. pw.close();
  32. }
  33. catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. // Berechnen des Hashwerts zu einem String und
  38. // Rückgabe als Hex-Zeichenkette
  39. public static String getHash(String w) {
  40. try {
  41. byte[] bytesOfMessage = w.getBytes("UTF-8");
  42. MessageDigest md = MessageDigest.getInstance("MD5");
  43. byte[] thedigest = md.digest(bytesOfMessage);
  44. return String.format("%032x", new BigInteger(1, thedigest));
  45. } catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
  46. e.printStackTrace();
  47. }
  48. return null;
  49. }
  50. }