1

This is my input file:

This is  a file test.
please take
notice of it.
Peace.

and this is the output:

This is  a file test. 
please take 
notice of 

I do not understant why it stops and "it." ,but it displays the word "test.". Here's the code:

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

  public class FileTest {

public static void readFile() throws Throwable {
    File _output = new File("output.txt");
    FileWriter _filewr = new FileWriter(_output);
    BufferedWriter _buffwrt = new BufferedWriter(_filewr);
    FileInputStream f = new FileInputStream(
            "C:\\Users\\Diana\\Desktop\\FileTest\\test.txt.txt");
    BufferedReader _buffer = new BufferedReader(new InputStreamReader(f));
    String _str = _buffer.readLine();
    while (_str != null) {
        String[] _vect = _str.split(" ");
        int i;
        for (i = 0; i < _vect.length; i++) {
            if (_vect[i].contains(".")) {
                _buffwrt.write(_vect[i] + " ");
                break;
            }

            else
                _buffwrt.write(_vect[i] + " ");
            _buffwrt.flush();
        }
        _str = _buffer.readLine();

    }

}

public static void main(String[] args) throws Throwable {
    readFile();
}

}

Raedwald
  • 46,613
  • 43
  • 151
  • 237
nnewbie
  • 59
  • 1
  • 1
  • 9
  • what do you see in output file? the whole sentence right? – SMA Jan 03 '15 at 10:03
  • @almasshaikh yes the whole text i have written in the input – nnewbie Jan 03 '15 at 10:05
  • nah what's your question? I believe you are trying to find why it stops at "it."? or something else? – SMA Jan 03 '15 at 10:06
  • @almasshaikh yes i am trying to figure out why is it stopping at it . – nnewbie Jan 03 '15 at 10:08
  • **Exactly** how many lines does your file contain? – Elliott Frisch Jan 03 '15 at 10:08
  • @ElliottFrisch well the input file contains 4 lines.I does not matter for me(or for what I will use this code) if the output file displays the 4 lines exactly as the input or it will all be written in one line.The problem was that the code was skipping the las 2 words ,but it has been resolved . – nnewbie Jan 03 '15 at 10:14

2 Answers2

3

use another _buffwrt.flush(); outside the loop to flush anything remaining and don't forget _buffwrt.close(); at the end

update
@Svetlin Zarev mention a good point that only _buffwrt.close(); at the end will be sufficient to flush anything remaining http://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html#close%28%29

Tareq Salah
  • 3,720
  • 4
  • 34
  • 48
  • 1
    thanks man ,that really worked!Next time I'll keep in mind what you said regarding the flushes & closing the file. – nnewbie Jan 03 '15 at 10:09
  • 1
    `Close` will `flush` the contents of the writer, so no need for explicit `flush`. Javadoc: http://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html#close%28%29 – Svetlin Zarev Jan 03 '15 at 10:25
1

The basic answer is, the break statement is breaking out of the loop, which is what you want, but it's also skipping the _buffwrt.flush(); statement. Because you're not closing the output streams correctly, the buffered content is simply been discard.

While you could put more flush statements in, it kind of defeats the purpose of BufferedWriter, instead, you should simply manage you resources better and make sure that they are getting closed when you are done with them, which will, in this case, flush the buffers before closing the resource.

File _output = new File("output.txt");
try (BufferedWriter _buffwrt = new BufferedWriter(new FileWriter(_output))) {
    try (BufferedReader _buffer = new BufferedReader(new FileReader("test.txt"))) {
        String _str = _buffer.readLine();
        while (_str != null) {
            String[] _vect = _str.split(" ");
            int i;
            for (i = 0; i < _vect.length; i++) {
                if (_vect[i].contains(".")) {
                    System.out.println("1 " + _vect[i]);
                    _buffwrt.write(_vect[i] + " ");
                    break;
                } else {
                    System.out.println("2 " + _vect[i]);
                    _buffwrt.write(_vect[i] + " ");
                }
            }
            _str = _buffer.readLine();
        }
    }
} catch (IOException exp) {
    exp.printStackTrace();
}

Take a look at The try-with-resources Statement for more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366