缓冲编写器现在写入文件 [英] buffered writer now writing to file

查看:126
本文介绍了缓冲编写器现在写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以指出我为什么不写入.txt文件的正确方向?

Could someone please point me in the right direction on why this is not writing to the .txt file?

这是我打印时得到的输出。我无法弄清楚代码中的错误在哪里。从输出中可以看出。它看起来像第一个循环一切正常。我的第一个问题是为什么不在.txt文件中写val 5?我的第二个问题是为什么它不会在第二个矩阵之后再次出现?

Here is the output i am getting when I print. I cannot figure out where the error in code is. As you can see from the output. its looks like everything is working correctly for the first loop. My first question is why does it not write the "val 5" to the .txt file? My second question is why does it not go again after the second matrix?

我是一名学生,并且希望我的代码能够获得更好的反馈。请尽可能建议。

I am a student and would love any feedback on my code to get better. Please suggest as much as possible.

输入:

1
5
3
3 -2 4
-1 5 2
-3 6 4

打印时输出:

Size:1
insert 5
len: 1 
size2 1
val5 
Size:3 
insert 3
insert -2
insert 4
insert -1
insert 5
insert 2
insert -3
insert 6
insert 4
len: 9

从.txt文件输出:

Matrix read: 
---------------------------------------

Matrix read: 
---------------------------------------

以下代码:

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

public class Driver{


public static void main(String[] args) {

  //initialize variables 
  String filepath;
  BufferedWriter bw = null;
  String toRead = "";
  CustomList[] arrayForList;
  CustomList listToBuild;



  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");

     //read user input
     Scanner user_input = new Scanner(System.in);
     filepath = user_input.next();

     //print out the file path for user to confirm the 
     //correct file path was entered
     System.out.println("Filepath read: " + filepath);
     System.out.println("");

     //finds the spot of the "." in .txt
     int extCounter = filepath.indexOf('.');
     String Output_Path = filepath.substring(0, extCounter);

     //close the scanner
     user_input.close();


     //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("_Output5_File.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();
     }

     //initialize array to hold strings
     String [] arrayToHoldInts = new String [100];


     //sets up filewriter to write output
     FileWriter fw = new FileWriter(file);
     bw = new BufferedWriter(fw);

     // open input stream test.txt for reading purpose.
     BufferedReader br = new BufferedReader(new FileReader(filepath));

     String input = "";
     input = br.readLine();
     int sizeOfArrayToStore = 0;
     while (input != null) {

        //below 2 lines get the size of the matrix
        sizeOfArrayToStore = Integer.parseInt(input);
        System.out.println("Size:" + sizeOfArrayToStore);

        //reads the next line after getting the size
        input = br.readLine();

        //checks for blanks and continues on error
        if (input.length() == 0){
           continue;
        }


        String [] stringSplitterForBR = null;
        arrayForList = new CustomList [sizeOfArrayToStore * sizeOfArrayToStore];

        //for loop to add ints parse the string that the
        //bufferred reader reads in. there is another nested
        //for loop to add each int that is parsed into a new
        //node for to build the list
        for (int i = 0; i < sizeOfArrayToStore; i++){
           listToBuild = new CustomList();
           stringSplitterForBR = input.split(" ");


           int tracker = 0;
           int valueToInsert = 0;   

           //for loop parses the ints and adds them into nodes
           //from the CustomList class
           for(int j = 0; j < sizeOfArrayToStore; j++) {
              valueToInsert = Integer.parseInt(stringSplitterForBR[tracker]);
              System.out.println("insert " + valueToInsert);
              listToBuild.addToList(valueToInsert);
              tracker++;
           }

           arrayForList[i] = listToBuild;
           input = br.readLine();

        }
        //Compute the deterimant using the same formula from 
        //Lab2


        int length = arrayForList.length;
        System.out.println("len: " + length);

        //print out the results to a .txt file


        bw.write("Matrix read: ");
        bw.newLine();
        bw.write("------------------" +
              "---------------------");
        bw.newLine();
        bw.flush();

        int size2 = 0;
        int valueToPrint;
        for (int x = 0; x < length; x++){

           listToBuild = arrayForList[x];
           size2 = listToBuild.sizeOfList();
           System.out.println("size2 " + size2);
           for (int y = 0; y < size2; y++) {

              valueToPrint = listToBuild.ValueOfNode(y);
              bw.write(valueToPrint);
              System.out.println("val" + valueToPrint);
              bw.flush();

           }
           bw.newLine();
        }


     }  
     bw.close();


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

 }
}


推荐答案

BufferedWriter 上的方法与 println PrintStream 上的方法( System.out 是一个的PrintStream )。例如,您可以使用 int 值作为参数调用 println ,它会将其打印为数字,但是 write 方法将此解释为单个字符的unicode,并且只会在代码中为val 5写入单个字符,这是具有数字值的unicode字符5。

The write method on BufferedWriter has very different semantics from the println method on PrintStream (System.out is a PrintStream). For example, you can call println with an int value as an argument and it will print it as a number, but the write method interprets this as the unicode of a single character and will only write a single character - in your code, for "val 5", that's the unicode character with numeric value 5.

解决方案:用 PrintWriter BufferedWriter >并在需要打印时使用 PrintWriter.println 方法 - 它与 println 方法具有相同的语义 System.out

Solution for you: replace BufferedWriter with PrintWriter and use the PrintWriter.println method whenever you want to print - it has the same semantics as the println method on System.out.

这篇关于缓冲编写器现在写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆