Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Does anyone know how i can set the

Status
Not open for further replies.

Dilenger4

Programmer
Nov 28, 2001
13
0
0
US
Does anyone know how i can set the size of a character array to the size of a file? The length method in the File class returns a long value representing the number of bytes in the file. So this declaration long[] c = new long[file.length()]; would be the right size when reading Unicode characters that are encoded using UTF-8 but reading characters that are represented using multiple bytes wouldn't work.

I whipped up this block of code to do the task but there must be a better way. Any ideas? Thanks.

public static int getFileCharCount(String file) throws IOException{

BufferedReader br = new BufferedReader(new FileReader(file));
String lineOnebyOne = null;
String finalText = null;
int numberOfChar = 0;

while ((lineOnebyOne = br.readLine ())!= null){
finalText += lineOnebyOne;
numberOfChar = finalText.length();
br.close();
}
return numberOfChar;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top