Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.nio.charset.Charset;
- import java.nio.file.OpenOption;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.Scanner;
- //Replacing class
- public class WriteAndUpdate {
- void replace(String nameOfFile, String current, String update, String FileToUpdate) throws IOException {
- Path newPath = Paths.get(nameOfFile);
- Path updatePath = Paths.get(FileToUpdate);
- Charset characterSet = Charset.forName("UTF-8");
- BufferedWriter write = Files.newBufferedWriter(updatePath, characterSet);
- Scanner scan = new Scanner(newPath, characterSet.name());
- String passwordLine;
- while(scan.hasNextLine()) {
- passwordLine = scan.nextLine();
- passwordLine = passwordLine.replace(current, update);
- write.write(passwordLine);
- }
- scan.close();
- write.close();
- }
- }
- private JPasswordField currPass;
- private JPasswordField newPass;
- static String npass;
- String passd = currPass.getText();
- npass = newPass.getText();
- JOptionPane.showMessageDialog(contentPane, "Password updated!", "Password Accepted", JOptionPane.PLAIN_MESSAGE);
- try {
- updateFile.replace("Password.txt", passd, npass, "Password.txt");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- /**
- * If the file already exists and it is opened for {@link #WRITE}
- * access, then its length is truncated to 0. This option is ignored
- * if the file is opened only for {@link #READ} access.
- */
- public static void main(String[] args) throws Exception {
- Path newPath = Paths.get("/Users/nfn8y/Documents/Code/abc.txt");
- Charset characterSet = Charset.forName("UTF-8");
- BufferedWriter write = Files.newBufferedWriter(newPath, characterSet,
- StandardOpenOption.TRUNCATE_EXISTING);
- String passwordLine;
- write.write("Overwritten from Java");
- write.close();
- }
Add Comment
Please, Sign In to add comment