Guest User

Untitled

a guest
Nov 24th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. import java.io.BufferedWriter;
  2. import java.io.IOException;
  3. import java.nio.charset.Charset;
  4. import java.nio.file.OpenOption;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.nio.file.Paths;
  8. import java.util.Scanner;
  9.  
  10. //Replacing class
  11. public class WriteAndUpdate {
  12.  
  13. void replace(String nameOfFile, String current, String update, String FileToUpdate) throws IOException {
  14. Path newPath = Paths.get(nameOfFile);
  15. Path updatePath = Paths.get(FileToUpdate);
  16. Charset characterSet = Charset.forName("UTF-8");
  17. BufferedWriter write = Files.newBufferedWriter(updatePath, characterSet);
  18. Scanner scan = new Scanner(newPath, characterSet.name());
  19. String passwordLine;
  20. while(scan.hasNextLine()) {
  21. passwordLine = scan.nextLine();
  22. passwordLine = passwordLine.replace(current, update);
  23. write.write(passwordLine);
  24. }
  25. scan.close();
  26. write.close();
  27.  
  28. }
  29. }
  30.  
  31. private JPasswordField currPass;
  32. private JPasswordField newPass;
  33. static String npass;
  34.  
  35. String passd = currPass.getText();
  36. npass = newPass.getText();
  37.  
  38. JOptionPane.showMessageDialog(contentPane, "Password updated!", "Password Accepted", JOptionPane.PLAIN_MESSAGE);
  39. try {
  40. updateFile.replace("Password.txt", passd, npass, "Password.txt");
  41.  
  42. } catch (IOException e) {
  43. // TODO Auto-generated catch block
  44. e.printStackTrace();
  45. }
  46.  
  47. /**
  48. * If the file already exists and it is opened for {@link #WRITE}
  49. * access, then its length is truncated to 0. This option is ignored
  50. * if the file is opened only for {@link #READ} access.
  51. */
  52.  
  53. public static void main(String[] args) throws Exception {
  54. Path newPath = Paths.get("/Users/nfn8y/Documents/Code/abc.txt");
  55. Charset characterSet = Charset.forName("UTF-8");
  56. BufferedWriter write = Files.newBufferedWriter(newPath, characterSet,
  57. StandardOpenOption.TRUNCATE_EXISTING);
  58. String passwordLine;
  59. write.write("Overwritten from Java");
  60. write.close();
  61. }
Add Comment
Please, Sign In to add comment