2016-07-15 141 views
-1

以下代码适用于我一直在研究的项目。输出必须是.txt文件。当while循环运行多次时,我无法让缓冲写入器写入。我尝试过故障排除和测试,但无法使其正常工作。如果我做system.out.prints,输出是正确的,所以我认为问题在于缓冲写入器。我目前没有bw.close(),但我曾尝试将它放在多个位置,但仍然无法使用。 bw.write(“你好”)只是我试图解决问题。我的bw.flush()在错误的位置?Buffered Writer写入.txt文件

我的第二个问题是,这是将输出写入.txt文件的最佳方法吗?我很乐于接受新的方法,并会喜欢一些建议来帮助扩大我对java的知识。

public class Driver{ 


public static void main(String[] args) { 

    String filepath; 
    BufferedWriter bw = null; 

    // TODO Auto-generated method stub 
    try { 
    System.out.println("To find the determinant of a Matrix, please enter the file below!"); 
    System.out.println("Please enter the file path of the txt file:\n"); 


    Scanner user_input = new Scanner(System.in); 
    filepath = user_input.next(); 

    System.out.println("Filepath read: " + filepath); 
    System.out.println(""); 
    int extCounter = filepath.indexOf('.'); 
    String Output_Path = filepath.substring(0, extCounter); 

    // Read input file 
    Scanner input = new Scanner(new File(filepath)); 

    //while loop to read in the text file 
    while (input.hasNextInt()) { 

     //find the size given by the first int 
     //size is then used to allocate the array 
     //to the correct size 
     int size = input.nextInt(); 
     int[][] a = new int[size][size]; 

     for (int i = 0; i < size; i++) { 
      for (int j = 0; j < size; j++) { 

       try{ 
       a[i][j] = input.nextInt(); 

       } 
       catch (java.util.NoSuchElementException e) { 
       e.printStackTrace(); 
       } 
      } 
     } 

     //Specify the file name and path here 
     //the below code allows the user to enter one path 
     //and get the output file at the same path 
     //without having to enter it twice 
     String OutFile; 
     OutFile = Output_Path.concat("_Output_File15.txt"); 
     File file = new File(OutFile); 

     /* This logic will make sure that the file 
     * gets created if it is not present at the 
     * specified location*/ 
     if (!file.exists()) { 
      file.createNewFile(); 
     } 

     FileWriter fw = new FileWriter(file); 
     bw = new BufferedWriter(fw); 


     //print out the determinant by 
     //calling the determinant method 
     Deter deterMethod = new Deter(); 
     bw.write("The Determinant is: " 
       + deterMethod.determinant(a) + " ..."); 
     int deterInt = deterMethod.determinant(a); 
     System.out.println(deterInt); 
     bw.write("" + deterInt); 
     bw.write("hello"); 
     bw.write(deterInt); 
     bw.flush(); 
     //deterInt = 0; 
     //print a blank line 
     System.out.println(); 

    } 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
} 





//determinant method 
public class Deter 
{ 
public static int determinant(int matrix [][]) { 

    //initializes the sum at 0 
    int sum = 0; 

    //base case 
    //1 x 1 matrix (x) is det(a) = x 
    if (matrix.length == 1) { 
    return matrix[0][0]; 

    } else { 

    //for loop to cycle through the columns 
    for (int j = 0; j < matrix.length; j++) { 

     //equation to figure out the minor of the array given. 
     //Deletes a row and column as provided by the definition 
     //of a minor 
     int[][] minor = new int[matrix.length - 1] 
       [matrix.length - 1]; 

     //for loop to cycle through the rows 
     for (int row = 1; row < matrix.length; row++) { 
      for (int column = 0; 
       column < matrix.length; column++) { 


       if (column < j) { 
       minor[row - 1][column] = matrix[row][column]; 
       } else if (column > j) { 
       minor[row - 1][column - 1] = matrix[row][column]; 
       } 
      } 
     } 

     //recursive equation to get the 
     //sum to find the determinent 
     sum+=matrix[0][j]*Math.pow(-1,j)*determinant(minor); 
    } 
    } 
    //return the sum to be printed out 
    return sum; 
} 
} 

回答

0

当你创建你的每个循环迭代新FileWriter,该文件将得到重写,所以无论是移动FileWriter的创建你的循环使用前或使用 它指示构造追加

https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.io.File,%20boolean)

尝试通过创造更多的方法

E,G,模块化的代码

private Scanner getFileScanner() { 

Scanner user_input = new Scanner(System.in); 
String filepath = user_input.next(); 

System.out.println("Filepath read: " + filepath); 
// Read input file 
Scanner input = new Scanner(new File(filepath)); 
return input 
} 
+0

这就是为什么我喜欢堆栈溢出。非常感谢@Scary Wombat。经过两个漫长的夜晚试图解决这个问题...要回答我的第二个问题是这是实现这个输出的最好方法吗? – cfsprod

+0

输出的方式似乎很好。不过,我会建议通过更多地使用用户编写的方法来让代码变得更加模块化。 –

+0

你能否详细说明一下?我是相当新的,并没有真正关注你。 – cfsprod