Showing posts with label File. Show all posts
Showing posts with label File. Show all posts

Friday, January 15, 2016

[Java] Copy dữ liệu sang file khác trong Java

Bài viết sẽ trình bày cách copy dữ liệu từ một file sang một file khác trong Java. Theo thứ tự để copy dữ liệu sang file khác, ta cần phải đọc dữ liệu (sử dụng FileInputStream) và viết dữ liệu sang file khác (sử dụng FileOutputStream).
Chương trình sau sẽ copy dữ liệu từ file "MyInputFile.txt" sang file "MyOutputFile.txt". Nếu file "MyOutputFile.txt" chưa tồn tại thì chương trình sẽ tạo file và copy dữ liệu sang file đó.
package simplecodecjava.blogspot.com;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyExample {
 public static void main(String[] args) {
  FileInputStream instream = null;
  FileOutputStream outstream = null;

  try {
   File infile = new File("src/simplecodecjava/blogspot/com/MyInputFile.txt");
   File outfile = new File("src/simplecodecjava/blogspot/com/MyOutputFile.txt");

   instream = new FileInputStream(infile);
   outstream = new FileOutputStream(outfile);

   byte[] buffer = new byte[1024];

   int length;
   /*
    * copy dữ liệu từ file đầu vào sang file đầu ra
    *  sử dụng phương thức read và write trong java.
    */
   while ((length = instream.read(buffer)) > 0) {
    outstream.write(buffer, 0, length);
   }
   /*Đóng luồng input/output*/
   instream.close();
   outstream.close();
   System.out.println("Copy thành công!");
  } catch (IOException ioe) {
   ioe.printStackTrace();
  }
 }
}
Output
Copy thành công!
Chương trình trên sử dụng phương thức read để đọc dữ liệu.
public int read(byte[] b) throws IOException
Khi gọi phương thức trên chương trình sẽ đọc vào mảng buffer 1024 (kích thước của mảng đệm buffer) byte từ luồng dữ liệu đầu vào instream . Phương thức này sẽ trả về số tổng số file được đọc vào mảng buffer. Nếu file không chứa dữ hiệu hoặc chương trình đã đọc đến đoạn kết thúc của file phương thức read sẽ trả về -1.

Chương trình trên sử dụng phương thức write để ghi dữ liệu.
public void write(byte[] b,int off, int length) throws IOException
Phương thức trên sẽ viết length byte từ mảng b bắt đầu từ vị trí off ra file đầu ra.

Thursday, December 31, 2015

[Java] Tạo file trong Java

Bài viết này sẽ trình bày cách tạo một file trong Java bằng phương thức createNewFile(). Phương thức này sẽ tạo ra một file rỗng. Nếu folder của đường dẫn tạo file chưa có file thì phương thức sẽ trả về TRUE. Nếu folder của đường  dẫn tạo file đã có file tồn tại phương thức sẽ trả về FALSE. Phương thức creatNewFile() sẽ ném ra các exception sau:
- IOException - Nếu có lỗi xảy ra trong quá trình tạo file.
- SecurityException - Nếu tồn tại Security manager và phương thức SecurityManager.checkWrite(java.lang.String) không có quyền write vào file.
Chương trình cài đặt:
Chương trình sau sẽ tạo ra một file txt có tên là "newfile.txt" trong ổ C. Để tạo file ở một ổ đĩa bất kỳ chỉ cần thay thổi đường dẫn tạo file.
package simplecodecjava.blogspot.com;

import java.io.File;
import java.io.IOException;

public class CreateFileDemo {
 public static void main(String[] args) {
  try {
   File file = new File("C:\\newfile.txt");
   /*
    * Nếu trong ổ C chưa tồn tại file newfile.txt thì phương thức createNewFile() trả về true
    * Nếu file đã tồn tại thì phương thức createNewFile() trả về false.
    */
   boolean fvar = file.createNewFile();
   if (fvar) {
    System.out.println("File đã được tạo thành công");
   } else {
    System.out.println("File đã tồn tại");
   }
  } catch (IOException e) {
   System.out.println("Đã xảy ra lỗi:");
   e.printStackTrace();
  }
 }
}