import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DemoData {
    static String pathName = "F:/ccc";
    public static void ghiFile(Double[] gia, int[] soLuong, String[] mota, String path) {
		DataOutputStream out = null;
		try {
              out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
			for (int i = 0; i < gia.length; i++) {
				out.writeDouble(gia[i]);
				out.writeInt(soLuong[i]);
				out.writeUTF(mota[i]);
			}
			System.out.println("Ghi thành công !");

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();

		} finally {
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static String docFile(String path) {
		String s = "";
		DataInputStream input = null;
		try {
			input = new DataInputStream(new BufferedInputStream(new FileInputStream(path)));
			while (true) {
				// Ghi như thế nào thì phải đọc đúng thứ tự
				double gia = input.readDouble();
				int soLuong = input.readInt();
				String moTa = input.readUTF();
				s += String.format("Bạn đặt %d %s với giá %.2f \n", soLuong, moTa, gia);
				}
        } catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
      finally {
			try {
				input.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return s;

	}

	public static void main(String[] args) {
		Double[] gia = new Double[] { 1.8, 2.9, 3.6, 4.7 };
		int[] soLuong = new int[] { 9, 8, 7, 6 };
		String[] mota = new String[] { "quần", "áo", "giá", "dép" };

		ghiFile(gia, soLuong, mota, pathName);
		System.out.println(docFile(pathName));
	}

}
