T
T
thatmaniscool2017-07-05 13:41:30
Java
thatmaniscool, 2017-07-05 13:41:30

How to append a string to the end of a text file in Java?

A simple program to generate a password. Actually, you need to make a list of passwords, but the problem is that every time it just overwrites the first line.
The program itself is below:

import java.io.*;
import java.util.*;

public class MyTextFile {
  public static class Characters implements Generator <java.lang.Character>{
    public static char [] array = "1234567890qwertyuiopasdfghjklzxcvbnm".toCharArray();
    public static Random rand = new Random ();
    @Override
    public Character next() {
      // TODO Auto-generated method stub
      return array[rand.nextInt(array.length)];
    }
  }
  
  public static void write (String password){
    try{
    PrintWriter print = new PrintWriter (new File ("Password.txt"));
    print.write(password);
    print.close();
    } catch (IOException e){
      throw new RuntimeException (e);
    } 
  }
  
  public static void main (String [] args) throws IOException {
    StringBuilder str = new StringBuilder ();
    for (int i=0; i!=10; i++){
      for (int j=0; j!=10; j++){
        str.append(new Characters().next()); // Создаем пароль
      }   str.append("\n");
      write (str.toString()); // Записываем пароль в файл.
      str.delete(0, str.length()); // Чистим.
    }
  }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Shockoway, 2017-07-05
@Shockoway

Link
In general, if it is assumed that with each "session" the file will still be reset to zero, then it is more reasonable to first generate the required number of passwords, and only then write them all at once. (Your Cap)

R
reus, 2017-07-05
@reus

If you mean that 10 passwords are generated and all 10 are on one line, then just add
print.write(password+"\n")
But judging by your code, your passwords are overwritten due to an unsuccessful write method. Take
out PrintWriter print = new PrintWriter(new File("Password.txt"));
outside of the write method. You can do this for example:

Если вы имеете в виду что генерится 10 паролей и все 10 в одной строке, то просто добавьте 
print.write(password+"\n")

Но судя по вашему коду ваши пароли затираются из-за неудачного метода write. Вынесите 
PrintWriter print = new PrintWriter (new File ("Password.txt"));
за пределы метода write. Можно сделать к примеру так:
<code lang="java">
import java.io.*;
import java.util.*;

public class MyTextFile {
  public static class Characters implements Generator <java.lang.Character>{
    public static char [] array = "1234567890qwertyuiopasdfghjklzxcvbnm".toCharArray();
    public static Random rand = new Random ();
    @Override
    public Character next() {
      // TODO Auto-generated method stub
      return array[rand.nextInt(array.length)];
    }
  }
  
  public static void write (PrintWriter print, String password){
    try{
    print.write(password);
    print.close();
    } catch (IOException e){
      throw new RuntimeException (e);
    } 
  }
  
  public static void main (String [] args) throws IOException {
    StringBuilder str = new StringBuilder ();
    PrintWriter print = new PrintWriter (new File ("Password.txt"));
    for (int i=0; i!=10; i++){
      for (int j=0; j!=10; j++){
        str.append(new Characters().next()); // Создаем пароль
      }   str.append("\n");
      write (print, str.toString()); // Записываем пароль в файл.
      str.delete(0, str.length()); // Чистим.
    }
  }
}
</code>

O
Odissey Nemo, 2017-07-07
@odissey_nemo

Open each time the file in the write method not to overwrite from the beginning of the file, as done here, but to append. I do not remember the specific parameters and calls)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question