如何修改此代码以写入文件?

时间:2019-02-18 21:36:05

标签: java java.util.scanner permutation filewriter printwriter

我正在尝试修改此堆算法,该算法显示字符串输入的所有可能排列。我正在尝试修改代码,以便能够与带有编写器文件的Scanner类一起使用。当我尝试写入新文件时,它没有按原样添加所有24个字符串,而是添加了前4个字符串。由于它是void方法,因此无法使用pw.println(obj.heapPermutation(a,a。长度,a.length))。有解决此问题的建议吗?

谢谢

P.S。我在网上找到了此代码,并且我承认这不是我的。

import java.util.Scanner;
import java.io.*;
import static java.lang.System.*;

class HeapAlgo 
{ 




    void heapPermutation(String a[], int size, int n) throws IOException
    { 
       FileWriter fw = new FileWriter("note.txt");
       PrintWriter pw = new PrintWriter(fw);


 // if size becomes 1 then prints the obtained 
 // permutation 
       if (size == 1) 
          for (int i=0; i<n; i++)
          { 
              System.out.println(a[i] + "");



          } 


          for (int i=0; i<size; i++) 
          { 
             heapPermutation(a, size-1, n); 

// if size is odd, swap first and last 
// element 
                if (size % 2 == 1) 
                { 
                    String temp = a[0]; 
                    a[0] = a[size-1]; 
                    a[size-1] = temp; 
                } 

// If size is even, swap ith and last 
// element 
                else
                { 
                   String temp = a[i]; 
                   a[i] = a[size-1]; 
                   a[size-1] = temp; 
                } 


         }


  } 


public static void main(String args[]) throws IOException 
{ 

   HeapAlgo obj = new HeapAlgo(); 
   String a[] = new String["abcd","bbbb","cccc","dddd"];
   obj.heapPermutation(a, a.length, a.length);

1 个答案:

答案 0 :(得分:1)

您的PrintWriter在heapPermutation方法中初始化,因为它被递归调用,heapPermutation(a, size-1, n)每次都会被覆盖。我相信此操作的默认行为是替换文件,而不是附加文件。

您应该创建一个构造函数来初始化PrintWriter,以便不会每次都重新初始化。

class HeapAlgo {
    // create a writer at the class level
    private PrintWriter _pw;

    // Create a constructor to assign the writer
    public HeapAlgo(PrintWriter pw) {
       this._pw = pw;
    }

    void heapPermutation(String a[], int size, int n) throws IOException { 
    // if size becomes 1 then prints the obtained 
    // permutation 
        if (size == 1) 
            for (int i=0; i<n; i++) { 
                System.out.println(a[i] + "");
                this._pw.print(a[i] + ""); // print here I belive?
            }

        for (int i=0; i<size; i++) { 
            heapPermutation(a, size-1, n); 

            // if size is odd, swap first and last 
            // element 
            if (size % 2 == 1) { 
                String temp = a[0]; 
                a[0] = a[size-1]; 
                a[size-1] = temp; 
            }

            // If size is even, swap ith and last 
            // element 
            else { 
               String temp = a[i]; 
               a[i] = a[size-1]; 
               a[size-1] = temp; 
            } 
        }
    }
}

public static void main(String args[]) throws IOException {
    FileWriter fw = new FileWriter("note.txt");
    PrintWriter pw = new PrintWriter(fw);
    HeapAlgo obj = new HeapAlgo(pw); // Pass in a writer
    String a[] = new String["abcd","bbbb","cccc","dddd"];
    obj.heapPermutation(a, a.length, a.length);