Reading in a certain number of characters from a file.

Hi guys,

I need some pointers on how to read in a specified number of characters from a text file.For example,how would I read in the first 100 characters to an array from a text file with an unspecfied number of characters (more than 100 rather than less!).

At present I am getting code errors being thrown due to reading in beyond the array size ie

java.lang.ArrayIndexOutOfBoundsException: 100.

[422 byte] By [theApprenticea] at [2007-11-27 2:33:20]
# 1
post more code , this exception tells me nothing without seeing what you are actually doing.
cafBeana at 2007-7-12 2:49:50 > top of Java-index,Java Essentials,New To Java...
# 2

> post more code , this exception tells me nothing

> without seeing what you are actually doing.

public String getText (String file)//textfile to be analyzed will be inputted as argument 150407 0356

//15042007 0612 Check notes on overloaded getText method below.

{

int temp;

inputFile = file;

int i=0;

try

{

BufferedReader bGetFile = new BufferedReader (new FileReader(inputFile));

while ((temp=bGetFile.read())!=-1)

{

textFromFile[i] = (char)temp;

i++;

}

/*for (int j=0;j<MAX;j++)

{

System.out.print(textFromFile[j]);//25042007 0208.For Testing purposes

}

*/

inputText=new String(textFromFile).trim();

bGetFile.close();

}

catch (IOException e)

{

System.out.println (" ");

System.out.println ("Sorry.An error occurred while trying to read from the input file.Unable to proceed!");

}

System.out.println ("Processing the input text file "); //25042007 2318 being used for debugging purposes.

return inputText;

}

If the file being read is greater than MAX characters it will throw the exception mentioned.What I would like to do is read in MAX characters and discard the rest.>

theApprenticea at 2007-7-12 2:49:50 > top of Java-index,Java Essentials,New To Java...
# 3
I don't see where you size your textFromFile array.
LazarusLonga at 2007-7-12 2:49:50 > top of Java-index,Java Essentials,New To Java...
# 4

> > post more code , this exception tells me nothing

> > without seeing what you are actually doing.

>

> > public String getText (String file)//textfile to be

> analyzed will be inputted as argument 150407 0356

>

> //15042007 0612 Check notes on overloaded getText

> method below.

>{

>int temp;

>inputFile = file;

>int i=0;

> try

>{

> BufferedReader bGetFile = new

> BufferedReader (new FileReader(inputFile));

>while ((temp=bGetFile.read())!=-1)

> {

>textFromFile[i] = (char)temp;

> i++;

>}

> /*for (int j=0;j<MAX;j++)

>{

> System.out.print(textFromFile[j]);//25042007

> 0208.For Testing purposes

>}

> */

> inputText=new

> String(textFromFile).trim();

>bGetFile.close();

>catch (IOException e)

> {

>System.out.println (" ");

> System.out.println ("Sorry.An error occurred while

> trying to read from the input file.Unable to

> proceed!");

>}

>

> System.out.println ("Processing the input

> text file "); //25042007 2318 being used for

> debugging purposes.

>return inputText;

>

>

>

> If the file being read is greater than MAX characters

> it will throw the exception mentioned.What I would

> like to do is read in MAX characters and discard the

> rest.

you have a counter in your while loop keeping track of how many characters you've read. you can compare that to the size of your array to see if you want to break out of the loop.

wang.yi.ganga at 2007-7-12 2:49:50 > top of Java-index,Java Essentials,New To Java...
# 5

> > post more code , this exception tells me nothing

> > without seeing what you are actually doing.

>

>

>while ((temp=bGetFile.read())!=-1)

> {

>textFromFile[i] = (char)temp;

> i++;

>}

//try the code below instead of what you have

//all I added was a i < Max to track your characters read in

while( (temp=bGetFile.read())!=-1 && i < MAX){

textFromFile[i] = (char)temp;

i++;

}

>

>

> If the file being read is greater than MAX characters

> it will throw the exception mentioned.What I would

> like to do is read in MAX characters and discard the

> rest.

cafBeana at 2007-7-12 2:49:50 > top of Java-index,Java Essentials,New To Java...
# 6

> I don't see where you size your textFromFile

> array.

The code I supplied is part of a larger program.I intialised textFromFile at the beginning of the total program ie

char [] textFromFile = new char [MAX];

I initialized MAX to 100.

theApprenticea at 2007-7-12 2:49:50 > top of Java-index,Java Essentials,New To Java...
# 7
Thanks cafBean.Such a simple clear solution.Shows I Still have a lot to learn!
theApprenticea at 2007-7-12 2:49:50 > top of Java-index,Java Essentials,New To Java...