How do i read a txt file into a char array. what i have so far only give me blank lines

String fname = "c:\\game.txt";
    char data;
    int row = 2;
        int col = 4;
        char[] matrix = new char[row];

private void openFile()
    {

        try
        {
            FileReader reader = new FileReader(this.fname);

            for (int i=0; i<matrix.length; i++)
            {
                reader.read();
            }
            reader.close();


        }
        catch(Exception e)
        {
            String s = e.toString();
            System.out.println(s);
        }

    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Of course it does - you aren't saving what is read anywhere:

reader.read();

If you want the data read into a char[] buffer then you have to supply it as an argument to the read() method. Check the API for read(). There are a few variations available depending on your needs.

i eventually want to read the file into a 2d array. i want to be able to find a particular character in the matrix.

So perhaps you should close this thread, since you have already started another on the exact same topic.

i have a file.txt like this : 2X^2 + 5X+3=0
i want to save 2 to variable a, 5 to variable b, and 3 to variable c . .
how i can do that . . ?
tanks for your help . .

This article has been dead for over six months. Start a new discussion instead.