简体   繁体   中英

reading characters from file

I'm having trouble understanding why my code isn't working properly.Just to test it, I have only five characters in my txt file. I know that characters are being put into array I create, but I am not sure why wouldnt it print them. Thanks!

        catch (IOException exception) {
            System.err.println(exception);
        }// catch

        finally {
            try {
                if (fileInput != null)
                    fileInput.close();
            }// try

            catch (IOException exception) {
                System.err.println("error!" + exception);
            }// catch

        }// finally

    }// main
}// TestCode

You have a for loop inside your while reader loop. Better to use:

    int index = 0;
    while ((character = fileInput.read()) != -1) {
        inputArray[index] = (char) character;
        index++;
    }

Also ensure that you dont exceed the inputArray size. You may want to consider a using a List type if this data is required to grow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM