126

Possible Duplicate:
How to create a Java String from the contents of a file
Whole text file to a String in Java

I am trying to read the contents of a file using FileReader . But i want to read the file without reading a line by line . Is it possible to read the whole file without loop. I am using the following code

 try
 {
     File ff=new File("abc.txt");
     FileReader fr=new FileReader(ff);

     String s;
     while(br.read()!=-1)
     {
          s=br.readLine();
     }
 }

 catch(Exception ex)
 {
     ex.printStackTrace();
 }
Community
  • 1
  • 1
Adesh singh
  • 2,083
  • 9
  • 24
  • 36

5 Answers5

209

Java 7 one line solution

List<String> lines = Files.readAllLines(Paths.get("file"), StandardCharsets.UTF_8);

or

 String text = new String(Files.readAllBytes(Paths.get("file")), StandardCharsets.UTF_8);
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
143

If the file is small, you can read the whole data once:

File file = new File("a.txt");
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fis.read(data);
fis.close();

String str = new String(data, "UTF-8");
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
imxylz
  • 7,847
  • 4
  • 28
  • 25
36

You can try using Scanner if you are using JDK5 or higher.

Scanner scan = new Scanner(file);  
scan.useDelimiter("\\Z");  
String content = scan.next(); 

Or you can also use Guava

String data = Files.toString(new File("path.txt"), Charsets.UTF8);
Jayamohan
  • 12,734
  • 2
  • 27
  • 41
  • 4
    Note that the scanner should be closed after use. – Barry NL Feb 13 '15 at 11:27
  • `String data = Files.toString(new File("path.txt"), Charsets.UTF_8);`. Just a small typo. :-) – Pikachu Jun 12 '15 at 18:33
  • 1
    This code with `Scanner` only read first 1024 bytes from file. Look at source code. It read buffer and check it with this regexp. This always valid - then it return all buffer content. By default buffer have size of 1024 bytes. – Enyby Mar 01 '17 at 05:09
  • 2
    This is BAD approach. The correct solution (for Java 1.7 and higher) is `Files.readAllLines()`. For legacy JREs, `FileInputStream.read()` is probably best. I'm not sure if it's portable to all platforms (i.e. Ctl-Z is recognized everywhere), and it 's easy to miss doing a `Scanner.close()`. – paulsm4 Oct 02 '17 at 01:21
36

If you are using Java 5/6, you can use Apache Commons IO for read file to string. The class org.apache.commons.io.FileUtils contais several method for read files.

e.g. using the method FileUtils#readFileToString:

File file = new File("abc.txt");
String content = FileUtils.readFileToString(file);
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
18

Since Java 11 you can do it even simpler:

import java.nio.file.Files;

Files.readString(Path path);
Files.readString​(Path path, Charset cs)

Source: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html#readString(java.nio.file.Path)

aardbol
  • 2,147
  • 3
  • 31
  • 42