I am working on a program that read in a text file. Below is the
code that I am currently using
-------------------
public void load_file()
{
FileReader fp;
char[] reading_buffer = new char[1024];
try
{
fp = new FileReader(new File(file_name));
while (fp.read(reading_buffer) != -1)
{
buffer += new String(reading_buffer);
}
}
catch(IOException event)
{
System.exit(0);
}
}
-----------------------
file_name (protected String file_name) and buffer (protected String
buffer) are class member.
-----------------------
For the most part this works, but if the file is not a 1024*x bytes
in size, the end of buffer will contain unknown character (stuff not
in the file) to make the length of reading_buffer 1024*x bytes.
Why do I get this garbage character?
I am mostly a C/C++ programmer, so I am sure there is a more
efficient way to accomplish this, but I don't know what it is, this
just seemed the most normal (comeing from a C/C++ point of view).
Can anyone tell me a better way to accomplish this?