Charles
Charles

Reputation: 620

What could cause a program to not read an entire file?

I have a program that should read from a file.It reads some amount of data but not all.When i try to read the same file at the same location with another program done in the same manner with the same classes it reads the entire file. Here is the code used in both programs:

                if (i >= 1) {
                try {

                file = new File("Tabel " + i + " " + timeLine + ".csv");
                fw = new FileWriter(file);
                fw.write(FileChooserSave.getInstance().getStringArray()[i]);
                if (i == 1) {
                    FileInputStream fis = new FileInputStream(file);
                    InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
                    BufferedReader br = new BufferedReader(isr);
                    String line;
                    while ((line = br.readLine()) != null) {   
                        System.out.println(line);
                    }
                    fis.close();
                    isr.close();
                    br.close();

                }
            } catch (IOException ex) {
                Logger.getLogger(Frame.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    fw.close();
                } catch (IOException ex) {
                    Logger.getLogger(Frame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

It always stops reading in the same place.I checked to see if there is some ackward character but there isn't. I should also mention that the file from which i read is in the .csv format and is a pretty big one, but i don' see if that is the problem. Thanks in advance!

Upvotes: 0

Views: 131

Answers (2)

Michael Krussel
Michael Krussel

Reputation: 2656

You are reading the file while the FileWriter is still open. This can lead to not having all the writes flushed to disk.

Try to move the call to close before new FileInputStream.

Upvotes: 0

rlegendi
rlegendi

Reputation: 10606

Hm... Your solution is a bit overcomplicated, so there might be quite a lot of things causing the problem.

I'd try launching the application in debug mode and when it "hangs" simply pause the thread e.g. in Eclipse, and you will see where it is waiting for data.

If you are using Java 7, there is a simple command that may help you reading all the lines of a file at once:

Path file = Paths.get(filename);
List<String> lines = Files.readAllLines(file, Charset.defaultCharset());

Upvotes: 3

Related Questions