# Solved: JAVA: How to read complete text in a file



## sepala (May 20, 2010)

Hi all,

In the following code I am reading 100 bytes from a text file. But I want to read the complete text in the file. How can I do that? Please help me.


```
import java.io.*;

public class FileReader{

    public static void main(String[]args)
    {
        System.out.println("Reading the File");

        byte buffer[] = new byte[100];

        String path = "C:/javay/Student.txt";

        try
        {
            FileInputStream file = new FileInputStream(path);
            file.read(buffer,0,100);
        }
        catch(IOException io)
        {
            System.out.println(io);
        }

        String a = new String(buffer);

        System.out.println(a);

    }
}
```


----------



## andythepandy (Jul 2, 2006)

Try this:

```
try {
	File f = new File("C:/javay/Student.txt");
	FileImageInputStream fis = new FileImageInputStream(f);
	byte b[] = new byte[(int) f.length()];					//create a byte[] with the same length as the file
	fis.read(b);								//this reads from the beginning of the file, to b.length
										//which we set to the length of the file,
										//so fis.read(b) reads to the end of the file
										//it is the same as calling fis.read(b, 0, b.length);
	for(int i = 0; i < f.length(); i++)
		System.out.print((char) b[i]);					//print out file
} catch(IOException e) {
	e.printStackTrace();
}
```
Andy


----------



## sepala (May 20, 2010)

Thanks for the help andy. I really appreciate this.


----------

