简体   繁体   中英

This is for an assignment. I need to read in a text file and then write a text file with the original five lines written in reverse

So here is the description of my assignment,

The FileCopier purpose is to read in a file, reverses the content of the file by line, and prints out a new file.

An example:

The input file contents:

This is the beginning of the file. Second line. This is the third line. Fourth Line. The end of the file.

Should be read in and the following file printed:

The end of the file. Fourth Line. This is the third line. Second line. This is the beginning of the file.

You will need to create your own input file to test with. I will use my own file to test your program. Your program will need to make sure it satisfies the following requirements:

  1. Your resources will need to be closed.
  2. Exceptions will need to be appropriately handled (caught and output provided to the console or logger)
  3. Your program should have the following method implemented: public void execute(String inputFilename, String outputFilename) { }

Submit your class file and the text file you tested with. Feel free to zip them up.

My question is how to use the array that I have read in and print it in reverse. I have gotten pretty close I'm sure, but just can't seem to get it to properly work. Any help or guidance would be greatly appreciated. Below is my code for the assignment.

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

public class TestThingsOut {

     private static String filepath = "."+File.separator+"src"+File.separator;
     private static String inputFileName = "KeyWestTemp.txt";
     private static String outputFileName = "outputTemp.txt";

    private static Scanner s1;
    private static Scanner s2;

    public static void main(String[] args)  {
        TestThingsOut test = new TestThingsOut();
        String inputFile = filepath.concat(inputFileName);
        String outputFile = filepath + outputFileName;

        test.execute(inputFile, outputFile);

        }

    private void execute(String inputFileName, String outputFileName) {
        String[] fileContent = readArray(inputFileName);
        writeOutFile(outputFileName, fileContent);
    }   

    public static String[] readArray(String inputFilename) {
        int ctr = 0;
        try {
            s1 = new Scanner(new File(inputFilename));
            while (s1.hasNextLine()) {
                ctr = ctr + 1;
                s1.nextLine();
            }

            String[] lines = new String[ctr];

            s2 = new Scanner(new File(inputFilename));
            for (int i = 0; i < ctr; i = i + 1) {
                lines[i] = s2.nextLine();
            }
            return lines;


    }   catch (FileNotFoundException e) {
        System.out.println("File not found");
    }
       return null;  
   }

    private void writeOutFile(String outputFileName, String[] fileContent) {
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(outputFileName);
            String[] lines2 = readArray(inputFileName);

            for (int i = lines2.length; i >= 0; i--) {

            fileWriter.write(lines2.toString());    

        }
        } catch (IOException e) {
            e.printStackTrace(); 
         }
        finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

 }

Here is the file that I am trying to read in,

  1. Hey Guys 2 How are you 3 I am very good 4 Thanks 5 This is line 5

which originally worked and I could print it out using Systemoutprint, but I have to follow the instructions of the assignment and write it out to another file, which I can't quite figure out.

instead of using a String array you could use a ArrayList or Vector to store the lines of file.

ArrayList<String> arr=new ArrayList<String>();

This ArrayList will hold the lines of the file and then you can write the same to a different file by iterating over the ArrayList.

//Reading the file
while (s1.hasNextLine()) {
           String s= s1.nextLine();
           arr.add(s);
        }

//Writing the file
for (int i = arr.size()-1; i >= 0; i--) {

        fileWriter.write(arr.get(i).toString());    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM