Hi,
I dont understand why, but for some reason, the system does not recognize the buffered reader in the fileReverse method. Every time I try to invoke the readLine method on aBufferedReader, I get null as a result.
Even my file reads:
null
null
null
null

Can someone please help me figure out why?
Greatly appreciate it!

import java.io.*; // 
public class ex9a {
		
			
public static int fileReverse(int countLine, FileInputStream aFileInputStream, FileOutputStream aFileOutputStream, BufferedReader aBufferedReader) throws IOException{
	
		PrintStream coolPoet;
		coolPoet = new PrintStream(aFileOutputStream);

		int count;
		for(count=0; count<countLine; count++){
			String apoet;
			apoet = aBufferedReader.readLine();
			System.out.println(apoet);
		}
		
		if(countLine == 0){
			aFileInputStream.close();
			aFileOutputStream.close();
			return countLine = 0;
		}
		
		else{
			String aPoemLine;
			aPoemLine = aBufferedReader.readLine();
			coolPoet.println(aPoemLine);
			countLine =  fileReverse(countLine-1, aFileInputStream, aFileOutputStream, aBufferedReader);
			return countLine;
			
		}
			
	}	
		public static void main(String args[]) throws IOException {

			File apoem;
			apoem = new File("src/infile.txt");
			FileInputStream forInfile;
			forInfile = new FileInputStream(apoem);
			InputStreamReader readInfile;
			readInfile = new InputStreamReader(forInfile);
			BufferedReader bufferInfile, buffer;
			bufferInfile = new BufferedReader(readInfile);
			
			buffer = bufferInfile;
			
			File robertFrost;
			robertFrost = new File("src/Robert Frost.txt");
			FileOutputStream outInfile;
			outInfile = new FileOutputStream(robertFrost);
			
			
			int lineNumber;
			lineNumber = 0;
				
			while(buffer.readLine() != null){
					lineNumber++;
			}
			
					
			ex9a.fileReverse(lineNumber, forInfile, outInfile, bufferInfile);
		
		
			}	
		
}

Recommended Answers

All 3 Replies

seems like the program will work fine.

except for the name of the output file. try changing your file name from
"src/Robert Frost.txt" to "src/Robert_Frost.txt" and let me know how it works.

seems like the program will work fine.

except for the name of the output file. try changing your file name from
"src/Robert Frost.txt" to "src/Robert_Frost.txt" and let me know how it works.

Hey tiny7415,
Thanks for the reply!xD
I tried changing the name of the file as you suggested, but the output is still the same T.T
Any other suggestions?

arent you suppose to do an outputStreamwriter instead of InputStreamReader?

inputStreamReader will reads bytes and decodes them into characters using a specified charset and so on.

while
outputStreamwriter - Characters written to it are encoded into bytes using a specified charset and so on....

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.