简体   繁体   中英

Java: Having trouble reading from a file

import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;


public class TextFile {

private static void doReadWriteTextFile() {

    try {

        // input/output file names
        String inputFileName  = "README_InputFile.rtf";

        // Create FileReader Object
        FileReader inputFileReader   = new FileReader(inputFileName);

        // Create Buffered/PrintWriter Objects
        BufferedReader inputStream   = new BufferedReader(inputFileReader);


        while ((inLine = inputStream.readLine()) != null) {
            System.out.println(inLine);
        }

        inputStream.close();

    } catch (IOException e) {

        System.out.println("IOException:");
        e.printStackTrace();

    }

}

public static void main(String[] args) {
    doReadTextFile();
}

}

I'm just learning Java, so take it easy on me. My program's objective is to read a text file and output it into another text file in reverse order. The problem is the professor taught us to to deal with strings and reverse it and such, but nothing about importing/exporting files. Instead, he gave us the following sample code which should import a file. The file returns 3 errors: The first two deal with inLine not being a symbol on lines 24 and 25. The last cannot find the symbol doReadTextFile on line 40.

I have no idea how to read this file and make the necessary changes to reverse and output into a new file. Any help is hugely appreciated.

I also had to change the file type from .txt to .rtf. I'm not sure if that affects how I need to go about this.

EDIT I defined inLine and fixed the doReadWritetextFile naming error, which fixed all my compiling errors. Any help on outputting into new file still appreciated!

I'm also aware he gave me bad sample code. It's supposed to be so we can learn troubleshooting, but with no working code to go off of and very extremely knowledge of the language, it's very difficult to see what's wrong. Thanks for the help!

The good practice will be to use a BufferedFileReader

BufferedFileReader bf = new BufferedFileReader(new FileReader(new File("your_file.your_extention")));

Then you can read lines in your file :

// Initilisation of the inLine variable...
String inLine = null;

while((inLine = bf.readLine()) != null){
    System.out.println(inLine);
}

To output a file, you can use StringBuilder to hold the file contents:

private static void doReadWriteTextFile() 
{
    ....
    StringBuilder sb = new StringBuilder();
    while ((inLine = inputStream.readLine()) != null) 
    {
        sb.append(inline);
    }

     FileWriter writer = new FileWriter(new File("C:\\temp\\test.txt"));
     BufferedWriter bw = new BufferedWriter(writer);
     w.write(sb.toString());
     bw.close();
}

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