8

I am reading a text file and trying to store it into an array char by char. My approach (below) pre-defines a char array with an initial length of 100000 (my main issue here). So, if the file contains characters less than that amount (problem also if more characters than that), then there are nulls in my array. I want to avoid that. Is there a way to predetermine the amount of characters present in a text file? Or is there a better approach altogether to store the file char by char?

char buf[] = new char[100000];
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
br.read(buf);
for(int i = 0; i < buf.length; i++)
{
     //Do stuff here
}

2 Answers 2

5

Use an ArrayList of Characters to accept the values. After that convert ArrayList to an Array as

 char[] resultArray = list.toArray(new char[list.size()]);

You should use br.readLine() instead of br.read() as follows:

String str;
while((str = br.readLine())!=null){
        str.tocharArray();
}
Sign up to request clarification or add additional context in comments.

2 Comments

then don't use br.read(), use br.readLine and get the string and then extract chars by string.toCharArray()
*str.toCharArray();
4

Use a Scanner instead!

I would use a scanner and read line by rather than char by char and then store it as a string. If you really want a char array it's easier to get it out afterwards.

This functionally does the same thing:

String theString = "";

File file = new File("data.txt");
Scanner scanner = new Scanner(file);

theString = scanner.nextLine();
while (scanner.hasNextLine()) {
       theString = theString + "\n" + scanner.nextLine();
}

char[] charArray = theString.toCharArray();

The upside to this is that you get to take advantage of Java's String type which handles all of the nasty length and concatenation business for you. And the the String.toCharArray() method is already built in to return an array that's automatically sized the way you need it.

8 Comments

you are adding an extra char "\n". Maybe you should remove that.
Reading in char by char will return a newline character for each new line. Scanner assumes that you just want the line's contents and returns a null-terminated String instead. The result is that if you want one gigantic String (as alluded to by the initial length of 100000) you need to re-add those back between each line's character data. (Java String concatenation gets rid of the null-termination between appended strings) In short, this is how you need to do it to get identical results
I still don't see why do you explicitly handle the "\n" when you don't know what's in the file. You are adding noOfLines*new_line_chars to the array whereas one would want only the number of characters he has written in the file. Try your program with just one single line and you'd understand.
also in terms of memory your code is inefficient coz it creates a new string everytime. you should use StringBuilder and append().
Because that's what br.read would have initially done. You can't just ignore newline characters. The OP was unclear on his intentions, and honestly it's important. Case in point, say he was using an HTTPClient to read in HTML data. Readline would do a fantastic job of reading that line by line, but if the intention were to handle an entire HTML document in a single String, you would need to reinsert those characters. Your answer is incorrect based on the original post in assuming the OP's use (although that seems like what he is going for here)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.