简体   繁体   中英

Creating a File using PrintWriter in Java, and Writing to that File

I'm trying to write a program that reads a file (which is a Java source file), makes an Arraylist of certain specified values from that file. and outputs that Arraylist into another resulting file.

I'm using PrintWriter to make the new resulting file. This is a summarised version of my program:

 ArrayList<String> exampleArrayList = new ArrayList<String>();
 File actualInputFile = new File("C:/Desktop/example.java");

 PrintWriter resultingSpreadsheet= new PrintWriter("C:/Desktop/SpreadsheetValues.txt", "UTF-8");

 FileReader fr = new FileReader(actualInputFile);
 BufferedReader br = new BufferedReader(fr);
 String line=null;

 while ((line = br.readLine()) != null) {
 // code that makes ArrayList
 }

 for (int i = 0; i < exampleArrayList.size(); i++) {
      resultingSpreadsheet.println(exampleArrayList.get(i));
 }
 resultingSpreadsheet.close();

The problem is that when i run this, nothing gets printed to the resultingSpreadsheet. It's completely empty.

BUT, this program works perfectly (meaning that it prints out everything correctly to the resultingSpreadsheet file) when I replace:

 File actualInputFile = new File("C:/Desktop/example.java"); 

which is the file that I want as my input file, and which has a size of 481 KB, with:

 File smallerInputFile = new File("C:/Desktop/smallerExample.txt"); 

which is really just a smaller .txt example version of the .java source file, and it has a size of 1.08 KB.

I've tried a few things including flushing the PrintWriter, wrapping it around FileWriter, copy-pasting all the code from the .java file into a text file in case it was an extension problem, but these don't seem to work. I'm starting to think it must be because of the size of the file that the PrintWriter makes, but it's very possible that that's not the problem. Perhaps I need to put everything in a stream (like it says here: http://docs.oracle.com/javase/6/docs/api/java/io/PrintWriter.html )? If so, how would I do that?

Why is reading the bigger actualInputFile and outputting its data correctly such a problem, when everything works fine for the smallerInputFile? Can anyone help with this?

Check for exceptions while writing to the the excel sheet , because i really don't think its a problem of size. Below is the sample code that is executing successfully and the file size was approx 1 MB.

public class Test {

/**
 * @param args
 */
public static void main(String[] args) {
    BufferedReader br = null;

    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader("D:\\AdminController.java"));

        while ((sCurrentLine = br.readLine()) != null) {
            System.out.println(sCurrentLine);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

}

This should go as a comment, but I do not have the rep. In the documentation it has both write methods and print methods. Have you tried using write() instead?

I doubt it's the size of the file, it may be between the two files you are testing one is .txt, and the other is .java

EDIT: Probably second suggestion of the two. First is just something I noticed with the docs.

The methods of PrintWriter do not throw Exception. Call the checkError() method which would flush the stream as well as return true if an error occurred. It is quite possible that an error occurred processing the larger file, an encoding error for instance.

Check your program. When the file is empty it means that your program doesn't close the PrintWriter before finishing the program. For example you may have a return in a part of your program which cause that resultingSpreadsheet.close(); have not being run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM