Java 在我停止程序的地方,如何继续排列/组合?

Java 在我停止程序的地方,如何继续排列/组合?,java,Java,这是我第二次直接问这个问题了。我似乎找不到解决办法,我知道这不是不可能的。我写了一个java程序,它可以生成一组任意长度的组合,当我停止程序时,我不想从头开始,我如何从停止的地方开始? 谢谢 示例(长度3): 如果我从aaa==>9zI开始,并在此处停止程序,我不想从头开始aaa,而是从9zI开始,继续执行999。我只想从我结束的地方继续 public class Main { public static void main(String[] args) { S_Permutation

这是我第二次直接问这个问题了。我似乎找不到解决办法,我知道这不是不可能的。我写了一个java程序,它可以生成一组任意长度的组合,当我停止程序时,我不想从头开始,我如何从停止的地方开始? 谢谢

示例(长度3):

如果我从
aaa==>9zI
开始,并在此处停止程序,我不想从头开始
aaa
,而是从
9zI
开始,继续执行
999
。我只想从我结束的地方继续

public class Main {

public static void main(String[] args) {
    S_Permutation sp = new S_Permutation();
    String text = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    FileClass.fileExist("new.txt", true);
    System.out.println("");

    sp.permutation(text, "", 7, "sha256.txt","Kaaaaaa");
  }
}  
=====================================================================

public class S_Permutation {

private List<String> permutation;

public S_Permutation() {
    permutation = new ArrayList<>();
}

public boolean saveThis(String words, char a, int limit) {
    int count = 0;
    limit++;
    for (char character : words.toCharArray()) {
        if (count == limit) {
            return false;
        }

        if (character == a) {
            count++;
        } else {
            count = 0;
        }
    }

    return count < limit;
}
private int counter = 0;
private boolean seen = false;
public void permutation(String str, String prefix, int lengthOfPermutationString, String filename, String startPoint) {
    if (prefix.equalsIgnoreCase(startPoint))
    {
        seen = true;
    }
    if (counter == 0) {
        if (startPoint.length() != lengthOfPermutationString) {
            for (int i = startPoint.length(); i < lengthOfPermutationString; i++) {
                startPoint += str.charAt(0);
            }
        }
        counter = -45;
    }
    if (prefix.length() == lengthOfPermutationString) {
        boolean savethis = true;

        for (int i = 0; i < prefix.length(); i++) {
            savethis = this.saveThis(prefix, prefix.charAt(i), 13);
            if (!savethis) {
                break;
            }
        }
        if (savethis && seen) {
            System.out.println(prefix);
            //permutation.add(prefix);
        }

    } else {
        for (int i = 0; i < str.length(); i++) {
            if (permutation.size() == 1000) {
                FileClass.WriteFile("new.txt", permutation);
                permutation.clear();
            }

            permutation(str, prefix + str.charAt(i), lengthOfPermutationString, filename, startPoint);
        }
        FileClass.WriteFile("new.txt", permutation);
        permutation.clear();

      }
  }
 }
public class FileClass {

public static boolean WriteFile(String filename, List<String> doc) {

    try {
        if (!filename.contains(".txt")) {
            filename += ".txt";
        }

        RandomAccessFile raf = new RandomAccessFile(filename, "rw");
        String writer = "";

        writer = doc.stream().map((string) -> string + "\n").reduce(writer, String::concat);
        raf.seek(raf.length());
        raf.writeBytes(writer);
        raf.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
        System.out.println("Error");
        new Scanner(System.in).nextLine();

        return false;
    }

    return true;
}
static RandomAccessFile raf;
public static boolean fileExist(String filename, boolean delete){
    File file = new File(filename);
    if (file.exists() && delete)
    {
        return file.delete();
    }
    return file.exists();
}
public static void WriteFile(String filename, String text) {

    try {
        if (!filename.contains(".txt")) {
            filename += ".txt";
        }

        raf = new RandomAccessFile(filename, "rw");
        long length = raf.length();
        raf.setLength(length + 1);
        raf.seek(raf.length());
        raf.writeBytes(text + "\n");

    } catch (Exception e) {
    }

}

private static void write(List<String> records, Writer writer) throws IOException {
    for (String record : records) {
        writer.write(record);
    }
    writer.flush();
    writer.close();

}

public static void stringWriter(List<String> records, String filename) {

    try {
        File file = new File(filename);
        FileWriter writer = new FileWriter(file, true);
        write(records, writer);
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
        new Scanner(System.in).nextLine();
    }
}

public static boolean CloseFile() {
    try {
        raf.close();
        return true;
    } catch (Exception e) {
        return false;
    }
  }
 }
公共类S_置换{
私有列表置换;
公共S_置换(){
排列=新的ArrayList();
}
公共布尔值saveThis(字符串字、字符a、整数限制){
整数计数=0;
极限++;
for(字符:words.toCharArray()){
如果(计数==限制){
返回false;
}
如果(字符==a){
计数++;
}否则{
计数=0;
}
}
返回计数<限制;
}
专用整数计数器=0;
私有布尔值=false;
公共无效置换(字符串str、字符串前缀、int lengthOfPermutationString、字符串文件名、字符串起始点){
if(前缀equalsIgnoreCase(起始点))
{
所见=真实;
}
如果(计数器==0){
if(startPoint.length()!=lengthOfPermutationString){
对于(int i=startPoint.length();i
=========================================================================

public class S_Permutation {

private List<String> permutation;

public S_Permutation() {
    permutation = new ArrayList<>();
}

public boolean saveThis(String words, char a, int limit) {
    int count = 0;
    limit++;
    for (char character : words.toCharArray()) {
        if (count == limit) {
            return false;
        }

        if (character == a) {
            count++;
        } else {
            count = 0;
        }
    }

    return count < limit;
}
private int counter = 0;
private boolean seen = false;
public void permutation(String str, String prefix, int lengthOfPermutationString, String filename, String startPoint) {
    if (prefix.equalsIgnoreCase(startPoint))
    {
        seen = true;
    }
    if (counter == 0) {
        if (startPoint.length() != lengthOfPermutationString) {
            for (int i = startPoint.length(); i < lengthOfPermutationString; i++) {
                startPoint += str.charAt(0);
            }
        }
        counter = -45;
    }
    if (prefix.length() == lengthOfPermutationString) {
        boolean savethis = true;

        for (int i = 0; i < prefix.length(); i++) {
            savethis = this.saveThis(prefix, prefix.charAt(i), 13);
            if (!savethis) {
                break;
            }
        }
        if (savethis && seen) {
            System.out.println(prefix);
            //permutation.add(prefix);
        }

    } else {
        for (int i = 0; i < str.length(); i++) {
            if (permutation.size() == 1000) {
                FileClass.WriteFile("new.txt", permutation);
                permutation.clear();
            }

            permutation(str, prefix + str.charAt(i), lengthOfPermutationString, filename, startPoint);
        }
        FileClass.WriteFile("new.txt", permutation);
        permutation.clear();

      }
  }
 }
public class FileClass {

public static boolean WriteFile(String filename, List<String> doc) {

    try {
        if (!filename.contains(".txt")) {
            filename += ".txt";
        }

        RandomAccessFile raf = new RandomAccessFile(filename, "rw");
        String writer = "";

        writer = doc.stream().map((string) -> string + "\n").reduce(writer, String::concat);
        raf.seek(raf.length());
        raf.writeBytes(writer);
        raf.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
        System.out.println("Error");
        new Scanner(System.in).nextLine();

        return false;
    }

    return true;
}
static RandomAccessFile raf;
public static boolean fileExist(String filename, boolean delete){
    File file = new File(filename);
    if (file.exists() && delete)
    {
        return file.delete();
    }
    return file.exists();
}
public static void WriteFile(String filename, String text) {

    try {
        if (!filename.contains(".txt")) {
            filename += ".txt";
        }

        raf = new RandomAccessFile(filename, "rw");
        long length = raf.length();
        raf.setLength(length + 1);
        raf.seek(raf.length());
        raf.writeBytes(text + "\n");

    } catch (Exception e) {
    }

}

private static void write(List<String> records, Writer writer) throws IOException {
    for (String record : records) {
        writer.write(record);
    }
    writer.flush();
    writer.close();

}

public static void stringWriter(List<String> records, String filename) {

    try {
        File file = new File(filename);
        FileWriter writer = new FileWriter(file, true);
        write(records, writer);
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
        new Scanner(System.in).nextLine();
    }
}

public static boolean CloseFile() {
    try {
        raf.close();
        return true;
    } catch (Exception e) {
        return false;
    }
  }
 }
公共类FileClass{
公共静态布尔写文件(字符串文件名,列表文档){
试一试{
如果(!filename.contains(“.txt”)){
文件名+=“.txt”;
}
RandomAccessFile raf=新的RandomAccessFile(文件名,“rw”);
字符串编写器=”;
writer=doc.stream().map((字符串)->string+“\n”).reduce(writer,string::concat);
raf.seek(raf.length());
raf.writeBytes(writer);
raf.close();
}捕获(例外e){
System.out.println(e.getMessage());
System.out.println(“错误”);
新扫描仪(System.in).nextLine();
返回false;
}
返回true;
}
静态随机文件raf;
公共静态布尔文件存在(字符串文件名,布尔删除){
文件=新文件(文件名);
if(file.exists()&&delete)
{
返回文件.delete();
}
返回文件.exists();
}
公共静态void WriteFile(字符串文件名、字符串文本){
试一试{
如果(!filename.contains(“.txt”)){
文件名+=“.txt”;
}
raf=新的随机访问文件(文件名,“rw”);
长长度=raf.length();
raf.设定长度(长度+1);
raf.seek(raf.length());
raf.writeBytes(文本+“\n”);
}捕获(例外e){
}
}
私有静态void write(列表记录、Writer Writer)引发IOException{
for(字符串记录:记录){
写作(记录);
}
writer.flush();
writer.close();
}
公共静态void stringWriter(列表记录、字符串文件名){
试一试{
文件=新文件(文件名);
FileWriter=newfilewriter(file,true);
写作(记录、作者);
}捕获(例外情况除外){
System.out.println(例如getMessage());
新扫描仪(System.in).nextLine();
}
}
公共静态布尔CloseFile(){
试一试{
raf.close();
返回true;
}捕获(例外e){
返回false;
}
}
}
为了添加“恢复”机制,您需要制作您的程序。一种方法是不保存排列,而是保存到文件中,在每次迭代时发送到排列的参数:

现在,每次程序启动时,它都会检查调用
permutation
的最后一个参数是什么(文件中的最后一行),然后从那里开始(当程序第一次启动时,文件中不会写入任何内容-因此它将从头开始)

在递归完成之后,我们可以调用另一个方法,该方法将遍历文件的行,并且只读取排列(忽略其他参数),并将它们写入一个更干净的“final_result.txt”文件

不用说,这种实现的成本更高(所有额外的磁盘读写操作),但这是让它支持“恢复”操作的折衷办法。

为了添加“恢复”机制,您需要制作程序。一种方法是不保存排列,而是保存到文件中,在每次迭代时发送到排列的参数:

现在,每次程序启动时,它都会检查调用
permutation
的最后一个参数是什么(文件中的最后一行),然后从那里开始(当程序第一次启动时,不会在