Please help I am stuck. I am trying to retrieved text from a file named "text.txt" and reversing the contents to another file named "output". Then i have to display the contents of "output" to the console. I am able to display the reversed text to console but only from the original file.

import java.io.*;



public class ReverseFile {



    public static void main(String[] args) {

        try {



            File file = new File("text.txt");
            FileReader reader = new FileReader(file);


            char[] Xnew = new char[(int)file.length()];
            char[] x = new char[(int)file.length()];

            StringBuffer Xbuf = new StringBuffer();
            reader.read(x,0,(int)file.length());

            int leng = (int)file.length();
            for (int i = 0, z = leng - 1; i < leng ; i++, z--) {
                Xnew[i] = x[z];
                Xbuf.append(Xnew[i]);





            }
            System.out.println("--------Text before being reverse--------");
            System.out.println(x);

            System.out.println("--------Text after being reversed--------");
            System.out.println(Xbuf.toString());
            reader.close();


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

Recommended Answers

All 3 Replies

Please help I am stuck. I am trying to retrieved text from a file named "text.txt" and reversing the contents to another file named "output".

Your code doesn't even attempt to write to a file. It only reads. Try to write to the file and if you're stuck, show us your new code.

My apologies. Here is my code with the file out. I am able to have the output.txt created but am unable to get the text reversed in it.

import java.io.*;

public class ReverseFile {

    public static FileOutputStream Output;
    public static PrintStream file;

    public static void main(String[] args) throws IOException {



        try {


            File file = new File("text.txt");
            FileReader reader = new FileReader(file);

            PrintStream os;
            os = new PrintStream(new FileOutputStream("output.txt"));


            char[] Xnew = new char[(int) file.length()];
            char[] x = new char[(int) file.length()];

            StringBuffer Xbuf = new StringBuffer();
            reader.read(x, 0, (int) file.length());

            int leng = (int) file.length();
            for (int i = 0, z = leng - 1; i < leng; i++, z--) {
                Xnew[i] = x[z];
                Xbuf.append(Xnew[i]);

                os.println(Xbuf.toString());
                os.close();

            }


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


        }
    }
}

I hope you don't mind but I changed your PrintStream to

BufferedWriter os = new BufferedWriter(new FileWriter("output.txt"));

BufferedWriter has an overloaded method write(), one parameter this method takes is a char[].

Now your write code looks like

for (int i = 0, z = leng - 1; i < leng; i++, z--) {
      Xnew[i] = x[z];
}
 os.write(Xnew);
 os.close();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.