Advertisement
Noam_15

Java - Work With Files

Jan 18th, 2018
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. //package ???
  2.  
  3. import java.io.*;
  4. import java.security.MessageDigest;
  5. import java.security.NoSuchAlgorithmException;
  6. import java.util.ArrayList;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9.  
  10. /**
  11.  *
  12.  * @author noamn
  13.  */
  14.  
  15.  
  16. // I need to read:  https://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java
  17. public class WorkWithFiles {
  18.     private final static String PathOfUserDetailsFile =
  19.                 "C:\\Users\\noamn\\Desktop\\ארבע בשורה\\תוכנית בגאווה\\V0.0.2\\Four In A Line\\src\\Files\\UserNameAndPasswordForLogin.txt";
  20. //            URL url = WorkWithFiles.class.getResource("/Files/UserNameAndPasswordForLogin.txt"); // need utf8
  21.     // See:  https://stackoverflow.com/questions/35132693/set-encoding-as-utf-8-for-a-filewriter
  22.    
  23.    
  24.    
  25.  
  26.     // write to text file
  27.     // return true if writing succeeded
  28.     public static Boolean writeStringsToTextFile(String... MyStrings) { // params in java
  29.         BufferedWriter bw = null;
  30.         FileWriter fw = null;
  31.  
  32.         try {
  33.             // this punction also delete the txt file content. to append - add true parameter.
  34.             fw = new FileWriter(PathOfUserDetailsFile);
  35.  
  36.             bw = new BufferedWriter(fw);
  37.             for (String line : MyStrings) {
  38.                 bw.write(line);
  39.                 bw.newLine();
  40.             }
  41.         } catch (IOException e) {
  42.             // e.printStackTrace();
  43.             return false;
  44.         } finally {
  45.             try {
  46.                 if (bw != null) {
  47.                     bw.close();
  48.                 }
  49.                 if (fw != null) {
  50.                     fw.close();
  51.                 }
  52.             } catch (IOException ex) {
  53.                 //ex.printStackTrace();
  54.             }
  55.         }
  56.  
  57.         return true;
  58.     }
  59.    
  60.    
  61.    
  62.     // The function fill the given Strings-ArrayList with all the lines in the text file
  63.     // return true if reading succeeded
  64.     public static Boolean readStringsFromTextFile(ArrayList<String> OutStrings){
  65.         if (OutStrings == null) return false;
  66.        
  67.        
  68.         FileReader fr = null;
  69.         BufferedReader br = null;
  70.         try {
  71.             fr = new FileReader(PathOfUserDetailsFile);
  72.             br = new BufferedReader(fr);
  73.            
  74.             String line;
  75.             while((line = br.readLine()) != null) {
  76.                 OutStrings.add(line);
  77.             }
  78.         } catch (FileNotFoundException e) {
  79.             return false;
  80.         } catch (IOException e) {
  81.             return false;
  82.         }finally {
  83.             try {
  84.                 if (br != null) {
  85.                     br.close();
  86.                 }
  87.                 if (fr != null) {
  88.                     fr.close();
  89.                 }
  90.             } catch (IOException ex) {
  91.                 //ex.printStackTrace();
  92.             }
  93.         }
  94.        
  95.         return true;
  96.     }
  97.  
  98.  
  99.    
  100.     // Create a hash password
  101.     // if failed return null
  102.     public static String makeEncryptedPassword(String plainTextPassword){
  103.         if(plainTextPassword == null) return null;
  104.         if(plainTextPassword.length() == 0) return null;
  105.        
  106.         MessageDigest messageDigest=null;
  107.         try {
  108.             messageDigest = MessageDigest.getInstance("SHA-256");
  109.         } catch (NoSuchAlgorithmException ex) {
  110.             return null;
  111.         }
  112.         messageDigest.update(plainTextPassword.getBytes());
  113.         return new String(messageDigest.digest());
  114.     }
  115.  
  116.    
  117.    
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement