0

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);
		
		
			}	
		
}
2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by tiny7415
0

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.

0

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?

0

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....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.