Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 5th, 2006, 12:02 AM   #1
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
BufferedReader

I'm not to sure how to move the pointer on the file your working with. For example in the following code: the 2 print statements (in blue) read lines 1 - 3. but the writting in orange continues reading the file. How do i move the pointer up to line 1 again to that the writting in orange does not continue from where the writting in blue left off ?

  BufferedReader input2 = new BufferedReader (new FileReader ("C:\\output.txt"));

                //int intNumberofLines = 0; // default 0 because there is always 1 line to read

                // first we get all the data so that it isn't written over
                
                intNumberofLines = Integer.valueOf (input2.readLine ()).intValue (); 
                c.print(input2.readLine()); 
                c.print(input2.readLine());  
                //c.print ("2nd line is: " + Integer.valueOf (input2.readLine ()).intValue ()); 
                //c.print ("3rd line is: " + Integer.valueOf (input2.readLine ()).intValue ()); 
                //c.print ("number of lines is" + intNumberofLines);

               // String inputString [] = new String [intNumberofLines]; // array large enough to hold all text data
                for (int y = 0 ; y < (intNumberofLines - 1) ; y++) // +1 because we've already read the int for line 1.
                {
                    inputString [y] = input2.readLine (); // because array starts at 0 not 1.
                    c.print ("String is: " + inputString [y]);
                }

                inputStringFinal = new String [intNumberofLines + 1];  // +1 because the tempLicense is added to the array.
                blnStringDeclared = true; // so it can be used when (inputAns = 1)
                
                    // cloning array and adding the final License
                    for (int i = 0 ; i < intNumberofLines ; i++) // cloning the array
                {
                    inputStringFinal [i] = tempLicense;
                }

                inputStringFinal [intNumberofLines] =  tempLicense; // the last boundary is used for the license just created
                
                c.print ("first line 1: " + input2.readLine());
                c.print ("second line 2: " + input2.readLine());
                c.print ("third line 3: " + input2.readLine());
                
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Apr 5th, 2006, 12:55 AM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
If you really want to rewind the BufferedReader, you'll find the mark() and reset() methods useful.

However, I suggest that instead you store all the data you read in from the BufferedReader initially, close() it, and use the data you have already stored rather than rereading it.

For example, why not replace

c.print ("first line 1: " + input2.readLine());
with

c.print ("first line 1: " + inputStringFinal[0]);
titaniumdecoy is online now   Reply With Quote
Old Apr 5th, 2006, 2:14 AM   #3
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Quote:
Originally Posted by titaniumdecoy
If you really want to rewind the BufferedReader, you'll find the mark() and reset() methods useful.

However, I suggest that instead you store all the data you read in from the BufferedReader initially, close() it, and use the data you have already stored rather than rereading it.

For example, why not replace

c.print ("first line 1: " + input2.readLine());
with

c.print ("first line 1: " + inputStringFinal[0]);
Thanks you!! you were right. better to read in the entire file first.
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Apr 12th, 2006, 11:46 PM   #4
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
I have this line of code

BufferedReader input2 = new BufferedReader (new FileReader ("C:\\output.txt"));

I'm wondering if anyone knows how to catch the exception where
"if there's no output.txt found" create it.
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Apr 13th, 2006, 3:29 AM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Catch "FileNotFoundException".
Arevos is offline   Reply With Quote
Old Apr 13th, 2006, 4:57 AM   #6
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Quote:
Originally Posted by Arevos
Catch "FileNotFoundException".
k thx. Goodnight
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Apr 13th, 2006, 7:33 AM   #7
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Well i still can't get this to work. The error has something to do with the 'try' and 'catch' statement. Anyone care to provide some input?

error code:
The constructor "FileReader" can throw the checked exception "java.io.FileNotFoundException",
but it's invocation is neither enclosed in a try statement that can catch that exception nor in the body 
of the method or constructor that "throws" that exception.

program code. Note the blue writting is where the error comes from.
class Database 
        {       
            public int lengthofFile; // holds the file length
            public String tempString = ""; // holds the added value from main. This value is to be added to the list
            public String oldFile [] = new String [1]; // this will hold the old file
            public String newFile [] = new String [1]; // this will hold the updated file
            public BufferedReader input2;
            // the contructor. When an object is created this is the default method used.
            public Database ()
            {

                // before we can modify the file we need to know how many lines it is, at the moment.
                input2 = new BufferedReader (new FileReader  ("C:\\output.txt"));
                lengthofFile = Integer.valueOf (input2.readLine ()).intValue ();
                try
                {
                 c.print ("I'm in Try");
                }
                catch (IOException input2)
                {
                    c.println ("Input error");
                   /* sets the length of the file
                    sothatwecanmodify it later on*/
                    oldFile = new String [lengthofFile];
                    newFile = new String [lengthofFile + 1];
                }
            }
}
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Apr 28th, 2006, 11:03 PM   #8
SugarHigh
Newbie
 
SugarHigh's Avatar
 
Join Date: Apr 2006
Location: Metro Detroit Area
Posts: 3
Rep Power: 0 SugarHigh is on a distinguished road
Send a message via AIM to SugarHigh
ignore this

Last edited by SugarHigh; Apr 28th, 2006 at 11:13 PM. Reason: Accidentally sent this prematurely, didn't notice until I finished the post below
SugarHigh is offline   Reply With Quote
Old Apr 28th, 2006, 11:05 PM   #9
SugarHigh
Newbie
 
SugarHigh's Avatar
 
Join Date: Apr 2006
Location: Metro Detroit Area
Posts: 3
Rep Power: 0 SugarHigh is on a distinguished road
Send a message via AIM to SugarHigh
The problem is that you either have to do a try-catch block that catches an IOException. You must do this for anything that has to do with file IO.

Here is what is should look like:

try{
    input2 = new BufferedReader (new FileReader  ("C:\\output.txt"));
} catch(IOException e){
    // other code
}


NOTE: If you are trying to do file IO in a method as opposed to a constructor, you can just append "throws IOException" to the method name

Example:

public String exampleMethod(BufferedReader b) throws IOException{
     // Code
};

This way you don't have to use a try-catch block each time you deal with file IO, at least in that method.
SugarHigh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:56 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC