Home > File > Gán nội dung file vào một biến trong Java

Gán nội dung file vào một biến trong Java

Có nhiều cách để thực hiện việc này:

  • Sử dụng DataInputStream
  • Sử dụng BufferedInputStream
  • Đọc từng dòng sử dụng BufferedReader
package quyetdv.java.javaio.file;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class AssignFileContentIntoVariable {
    public static void main(String[] args) {
         AssignFileContentUsingDataInputStream();
         AssignFileContentUsingBufferedInputStream();
         AssignFileContentFromLineByLine();
    }
    /**
    * Read File in String Using DataInputStream
    */
    private static void AssignFileContentUsingDataInputStream() {
        try {
             DataInputStream dis = new DataInputStream(new FileInputStream(
             "test1.txt"));
             byte[] datainBytes = new byte[dis.available()];
             dis.readFully(datainBytes);
             dis.close();
             String content = new String(datainBytes, 0, datainBytes.length);
             System.out.println(content);
        } catch (Exception e) {
             e.printStackTrace();
        }
    }
    /**
    * Read File in String Using BufferedInputStream
    */
    private static void AssignFileContentUsingBufferedInputStream() {
        File file = new File("test1.txt");
        BufferedInputStream bis = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            byte[] contents = new byte[1024];
            int bytesRead = 0;
            String strFileContents;
            while ((bytesRead = bis.read(contents)) != -1) {
                strFileContents = new String(contents, 0, bytesRead);
                System.out.println(strFileContents);
            }
        } catch (FileNotFoundException e) {
            System.out.println("File not found" + e);
        } catch (IOException ioe) {
            System.out.println("Exception while reading the file " + ioe);
        } finally {
            try {
                if (bis != null)
                bis.close();
            } catch (IOException ioe) {
                System.out.println("Error while closing the stream :" + ioe);
            }
        }
    }
   /**
   * Read File in String From Line by Line
   */
   private static void AssignFileContentFromLineByLine() {
       try {
           File file = new File("test1.txt");
           FileReader fileReader = new FileReader(file);
           BufferedReader br = new BufferedReader(fileReader);
           StringBuffer sb = new StringBuffer();
           String line;
           while ((line = br.readLine()) != null) {
               sb.append(line);
               sb.append("\n");
           }
           fileReader.close();
           System.out.println(sb.toString());
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
}
Advertisements
Categories: File
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: