Q: How to put a new line in an ArrayList java How to input a new line in an java String array.
  For example my input file is this:
   Student learns programming 
   java.Teacher rates 
   student.Nero gets ten.

and the output is in one line.
   Output:
  Student learns programming java.Teacher rates student.Nero gets ten.

my code used is this:
 File f=new File(s3);
      File _if=new File(s2);

      File _dic=new File(s4);
      f.createNewFile();

      FileInputStream _inputfile=new FileInputStream(_if.getAbsolutePath());
      BufferedReader _readfile=new BufferedReader(new InputStreamReader(_inputfile));
      String _str=_readfile.readLine();
      FileWriter _filewrite=new FileWriter(f);
      BufferedWriter _buffwrt=new BufferedWriter(_filewrite);
      int sum=0,sum2=0;
      ArrayList<String> _array=new ArrayList<String>();
            while (_str != null) {

                String[] _vect = _str.split(" ");
                int i;


                for (i = 0; i < _vect.length; i++) {

                    _array.add(_vect[i]);}


                _str =_readfile.readLine();



            }


A: You can use Apache Commons IO
Take a look in this method, that reads a file straightforward to a List.

A: you can add a "\n" string to the arraylist after the for loop:
for (i = 0; i < _vect.length; i++) {
    _array.add(_vect[i]);
}
_array.add("\n");//this will add a new line character to the arraylist


A: Instead of adding the newline character as a String to the list, I would append the newline to the String itself:
for (i = 0; i < _vect.length; i++) {
    _array.add(_vect[i] + "\n");
}

I do not think wasting allocation of memory by making the newline String a unique entry on the List is a good idea.

A: If the whole intention of this assignment is to read the file and write it back, you don't really need an ArrayList at all. You can do something like:
while ( _str != null ) {
    _buffwrt.write( _str );
    _buffwrt.newLine();
}

This simply reads each line from the input file and writes it back to the output file.
But I suspect, from your code, that you actually want to do something with the words in this file. You didn't explain what it is you are doing. Maybe you want to count them, or maybe you want to translate them to something.
The important thing, though, is that you don't have to keep all the words of the whole file in an ArrayList together. You should do it for each line separately. You may even be able to work directly with the split array. For example, here is how you would count the words of the whole file, and still write each line separately.
int sum = 0;

while ( _str != null ) {


    String[] _vect = _str.split(" ");
    sum += vect.length;

    _buffwrt.write( _str );
    _buffwrt.newLine();
}

This adds the number of words in each line to the total of words after reading each line. But it still writes the original line to the output file as before.
If what you want to do is make changes to the words and write them back changed, you could do something like:
while ( _str != null ) {


    String[] _vect = _str.split(" ");

    for ( int i = 0 ; i < _vect.length; i++ ) {

        // Change _vect[i] in some way

        _vect[i] = ....;

        // Write a space if it's not the first word
        if ( i != 0 ) {
           _buffwrt.write( ' ' );
        }

        _buffwrt.write(vect[i]);

     }

    _buffwrt.newLine();
}

What you do here is split the line, work on each word, and then write it to the output file. You also write a space before it if it's not the first word.
The important point here is not to collect all the words in the file together, and then try to remember which word belonged to which line, or adding newlines to the ArrayList, which are not words and may affect the logic of your program. Instead, work on each line separately, and write it separately, and then you can add a newline at the end of processing.
