-2

As in the tittle , i want to write my text from back and enter to the text file . In first , i read the text from the text file , next i want to save it in text file , but writing by the end of. I don't have any ideas how to fix my code . My code read the text file , and write the text file , but in the same order , from beginning to ending.

Example how it must work:

input text:

aaaaa
bbbb
ccc
dd
f

output text:

f
dd
ccc
bbbb
aaaaa

My code:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class Loading {

    public static void main(String[] args) {
        try {

            BufferedReader br = new BufferedReader(new FileReader(
                    "file.txt.txt"));
            String line, txt = "";
            String[] splittedLine;
            while ((line = br.readLine()) != null) {
                txt += linia + "\n";
                splittedLine = line.split(" ");
            }
            System.out.println(txt);
            br.close();

            BufferedWriter bw = new BufferedWriter(new FileWriter("file2"));
            bw.write(txt);
            bw.newLine();
            bw.close();

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

}
corsiKa
  • 81,495
  • 25
  • 153
  • 204
MOnasz
  • 99
  • 1
  • 2
  • 8
  • If you are going to ask a question, at least go to the effort of putting spaces in the right place and using uppercase 'I' instead of 'i'. – ThePerson Jan 04 '13 at 00:54

4 Answers4

1

Read File to String

Write String data to File starting from end.

For lines, use String array to store data from file and then traverse from end to start of array.

Here is complete program.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

public class Loading {

    public static void main(String[] args) {
        try {

            BufferedReader br = new BufferedReader(new FileReader(
                    "DB.xml"));
            String line, txt = "";
            List<String> lines = new ArrayList<String>();
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }
            System.out.println(txt);
            br.close();

            BufferedWriter bw = new BufferedWriter(new FileWriter("file2"));
            for(int i=lines.size()-1; i>=0; i--){
            bw.write(lines.get(i));
            bw.newLine();
            }
            bw.close();

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

}
Pankaj
  • 5,132
  • 3
  • 28
  • 37
1

You're going to want a list of lines, since you don't know the number of entries beforehand, so you want something that can grow with your code.

List<String> lines = new ArrayList<String>();

And add all your lines to it

while ((line = br.readLine()) != null) {
    lines.add(line);
}

Then write them to your file in reverse order:

for(int i = lines.size() - 1; i >= 0; i--) {
    bw.write(lines.get(i));
    bw.newLine();
}
corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • You could also use recursion without keeping a list, but beware that both recursion and filling a list will blow up for very large files. – xagyg Jan 03 '13 at 23:04
  • You could use recursion, but that would turn an `O(n)` operation into an `O(n^2)` operator, where we consider n to be the number of lines. – corsiKa Jan 03 '13 at 23:05
1

Your code is concatenating the string incorrectly. It needs to add the line text to the beginning of the string rather than at the end if you are trying to reverse the order.

txt = line + txt + "\n";  //original: txt += linia + "\n"

But it would be better to use a StringBuilder object to handle the concatenation for you. Something like...

StringBuilder txt = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
    txt.insert(0, line + "\n");
}
System.out.println(txt.toString());

I do agree that the array approach in the other answers would also work and is probably a little easier to read and maintain. But the StringBuilder approach is closer to what you already have.

0

It should work:

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;

/**
 *
 * @author 
 */
public class Read {
        public static void main(String[] args) {
        try {

            BufferedReader br = new BufferedReader(new FileReader(
                    "D:/file.txt"));
            String line, txt = "";
            ArrayList<String> lines = new ArrayList<String>();

            while ((line = br.readLine()) != null) {
                lines.add(line);
            }



            System.out.println(txt);
            br.close();

            BufferedWriter bw = new BufferedWriter(new FileWriter("D:/file2.txt"));
            for(int i = lines.size()-1; i>=0; i--){
                bw.write(lines.get(i));
                bw.newLine();
            }
            bw.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Dav
  • 330
  • 2
  • 10