Powered by Blogger.

Java: How to Read a Java String from the contents of a file

I have been using this idiom for some time now. And it seems to be the most wide-spread, at least in the sites I've visited.

Does anyone have a better/different way to Read a Java String from the contents of a file?

private String readFile( String file ) throws IOException {
    BufferedReader reader = new BufferedReader( new FileReader (file));
    String line = null;
    StringBuilder stringBuilder = new StringBuilder();
    String ls = System.getProperty("line.separator");

    while( ( line = reader.readLine() ) != null ) {
        stringBuilder.append( line );
        stringBuilder.append( ls );
    }

    return stringBuilder.toString();
}

Read String from a File Example:-

 import java.io.*;

 public class ReadFile{
   public static void main(String[]args){
      File file = new File(”/home/Parvez/MyJavaBook”, ”MyText.txt”);
      try{
         BufferedReader in = new BufferedReader(new FileReader(file));
         String str = in.readLine();
         while(str != null){
         System.out.println(”Read: ” +str);
         str = in.readLine();
         }
         in.close();
      }catch(FileNotFoundException e1){
         System.out.println(”File not found”);
      }catch(IOException e2){
         System.out.println(”Input/output problem”);
      }
   }
 }

Generation of Source Code:--

/*
Read File in String Using Java Buffered Input Stream Example. This example shows how to read a file content into a Sting object using available and read methods of Java Buffered Input Stream.
*/
    
    import java.io.*;
    
    public class ReadFileInToString {
    
            public static void main(String[] args) {
                  
                    //create file object
                    File file = new File("C://FileIO//ReadFile.txt");
                    BufferedInputStream bin = null;
                  
                    try
                    {
                            //create FileInputStream object
                            FileInputStream fin = new FileInputStream(file);
                          
                            //create object of BufferedInputStream
                            bin = new BufferedInputStream(fin);
                          
                            //create a byte array
                            byte[] contents = new byte[1024];
                          
                            int bytesRead=0;
                            String strFileContents;
                          
                            while( (bytesRead = bin.read(contents)) != -1){
                                  
                                    strFileContents = new String(contents, 0, bytesRead);
                                    System.out.print(strFileContents);
                            }
                          
                    }
                    catch(FileNotFoundException e)
                    {
                            System.out.println("File not found" + e);
                    }
                    catch(IOException ioe)
                    {
                            System.out.println("Exception while reading the file " + ioe);
                    }
                    finally
                    {
                            //close the BufferedInputStream using close method
                            try{
                                    if(bin != null)
                                            bin.close();
                            }catch(IOException ioe)
                            {
                                    System.out.println("Error while closing the stream :"
    + ioe);
                            }
                          
                    }
            }
          
    }
    
    
/*
Output would be This file is for demonstration of how to read a file into String using Java Buffered Input Stream.
*/

0 comments:

Post a Comment