Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

BufferedReader .readLine always null... Help

843789Apr 4 2010 — edited Apr 9 2010
Hello,
I was making a class that would make writing and reading to a file very easy. I know how to use the reader and scanner but i thought if I made my own class then it would be so much easier... Anyway, I am trying to make it to where I can write to a certain line or read a certain line by reading the first few characters to a line, telling if the number is the number in the method argument, and then either reading the entire line or writing to that line... I can write the numbers to the line so that it looks like 1>> 2>> 3>> with a different number on each line, but i can't get it to read the lines to do any of the other stuff... So here is my code:
In my class I have:
        //declare variables
	public int line = 1;
	public String spacer = ">>";
	public String file;
	public FileReader fileReader;
	public BufferedReader read;
	public FileOutputStream outStream;
	public PrintWriter write;
	//construct the modifier
	public FileMod (String filename) throws Exception {
		file = filename;
	}
	//initialize and close the modifier
	void initModder () throws Exception {
		fileReader = new FileReader(file);
		read = new BufferedReader (fileReader);
		outStream = new FileOutputStream(file);
		write = new PrintWriter(outStream);
	}
	void closeModder () throws Exception {
		write.flush();
		write.close();
		outStream.close();
		read.close();
		fileReader.close();
	}
	//read a line
	void readLine (int lineNumber)throws Exception {
		String lineRead;
		int lineReadLength;
		int startspace = 0;
		int endspace = 0;
		int lineReadInt;
		
		do {
			lineRead = read.readLine();
			lineReadLength = lineRead.length();
			switch (lineReadLength) {
				case 3 : 
					endspace = 1;
					break;
				case 4 :
					endspace = 2;
					break;
				case 5 :
					endspace = 3;
					break;
				case 6 :
					endspace = 4;
					break;
				case 7 :
					endspace = 5;
					break;
				case 8 :
					endspace = 6;
			}
			lineReadInt = Integer.parseInt((lineRead.substring(startspace, endspace)));
		}
		while (lineReadInt != lineNumber - 1);
		String fullLineRead = read.readLine();
		System.out.println(fullLineRead);
	}
	void readTest () throws Exception{
		String lineRead = read.readLine();
		System.out.println(lineRead);
	}
And on my main class i have:
public static void main(String[] args) throws Exception {
		FileMod fileMod = new FileMod("./src/pkg/file.zzz");
		fileMod.initModder();
		fileMod.addLine(100);
		fileMod.readTest();
		fileMod.closeModder();	
	}
I can add the lines easily and control the amount, but i can't read them. I tried using a Scanner and that didn't work, then i tried this Buffered Reader way and it still isn't working. **The read test thing was just a simple reader that i had so that i could check if it would even read a pre-input line and it still didn't work.

I have no idea where the issue is and any help is appreciated!
Thanks!
Thimbletack

I am really sorry for posting this in this forum... i was in a different forum and it posted it in here... I can't figure out how to take it down so... Sorry again!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 7 2010
Added on Apr 4 2010
5 comments
2,478 views