Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package ???
- import java.io.*;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.util.ArrayList;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author noamn
- */
- // I need to read: https://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java
- public class WorkWithFiles {
- private final static String PathOfUserDetailsFile =
- "C:\\Users\\noamn\\Desktop\\ארבע בשורה\\תוכנית בגאווה\\V0.0.2\\Four In A Line\\src\\Files\\UserNameAndPasswordForLogin.txt";
- // URL url = WorkWithFiles.class.getResource("/Files/UserNameAndPasswordForLogin.txt"); // need utf8
- // See: https://stackoverflow.com/questions/35132693/set-encoding-as-utf-8-for-a-filewriter
- // write to text file
- // return true if writing succeeded
- public static Boolean writeStringsToTextFile(String... MyStrings) { // params in java
- BufferedWriter bw = null;
- FileWriter fw = null;
- try {
- // this punction also delete the txt file content. to append - add true parameter.
- fw = new FileWriter(PathOfUserDetailsFile);
- bw = new BufferedWriter(fw);
- for (String line : MyStrings) {
- bw.write(line);
- bw.newLine();
- }
- } catch (IOException e) {
- // e.printStackTrace();
- return false;
- } finally {
- try {
- if (bw != null) {
- bw.close();
- }
- if (fw != null) {
- fw.close();
- }
- } catch (IOException ex) {
- //ex.printStackTrace();
- }
- }
- return true;
- }
- // The function fill the given Strings-ArrayList with all the lines in the text file
- // return true if reading succeeded
- public static Boolean readStringsFromTextFile(ArrayList<String> OutStrings){
- if (OutStrings == null) return false;
- FileReader fr = null;
- BufferedReader br = null;
- try {
- fr = new FileReader(PathOfUserDetailsFile);
- br = new BufferedReader(fr);
- String line;
- while((line = br.readLine()) != null) {
- OutStrings.add(line);
- }
- } catch (FileNotFoundException e) {
- return false;
- } catch (IOException e) {
- return false;
- }finally {
- try {
- if (br != null) {
- br.close();
- }
- if (fr != null) {
- fr.close();
- }
- } catch (IOException ex) {
- //ex.printStackTrace();
- }
- }
- return true;
- }
- // Create a hash password
- // if failed return null
- public static String makeEncryptedPassword(String plainTextPassword){
- if(plainTextPassword == null) return null;
- if(plainTextPassword.length() == 0) return null;
- MessageDigest messageDigest=null;
- try {
- messageDigest = MessageDigest.getInstance("SHA-256");
- } catch (NoSuchAlgorithmException ex) {
- return null;
- }
- messageDigest.update(plainTextPassword.getBytes());
- return new String(messageDigest.digest());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement