![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Expert Programmer
|
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());c.print ("first line 1: " + inputStringFinal[0]); |
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quote:
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Catch "FileNotFoundException".
|
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quote:
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#8 |
|
Newbie
|
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 |
|
|
|
|
|
#9 |
|
Newbie
|
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|