fork download
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8.  
  9. public class DemoData {
  10. static String pathName = "F:/ccc";
  11. public static void ghiFile(Double[] gia, int[] soLuong, String[] mota, String path) {
  12. DataOutputStream out = null;
  13. try {
  14. for (int i = 0; i < gia.length; i++) {
  15. out.writeDouble(gia[i]);
  16. out.writeInt(soLuong[i]);
  17. out.writeUTF(mota[i]);
  18. }
  19. System.out.println("Ghi thành công !");
  20.  
  21. } catch (Exception e) {
  22. // TODO: handle exception
  23. e.printStackTrace();
  24.  
  25. } finally {
  26. try {
  27. out.close();
  28. } catch (IOException e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34.  
  35. public static String docFile(String path) {
  36. String s = "";
  37. DataInputStream input = null;
  38. try {
  39. while (true) {
  40. // Ghi như thế nào thì phải đọc đúng thứ tự
  41. double gia = input.readDouble();
  42. int soLuong = input.readInt();
  43. String moTa = input.readUTF();
  44. s += String.format("Bạn đặt %d %s với giá %.2f \n", soLuong, moTa, gia);
  45. }
  46. } catch (Exception e) {
  47. // TODO: handle exception
  48. e.printStackTrace();
  49. }
  50. finally {
  51. try {
  52. input.close();
  53. } catch (IOException e) {
  54. // TODO Auto-generated catch block
  55. e.printStackTrace();
  56. }
  57. }
  58. return s;
  59.  
  60. }
  61.  
  62. public static void main(String[] args) {
  63. Double[] gia = new Double[] { 1.8, 2.9, 3.6, 4.7 };
  64. int[] soLuong = new int[] { 9, 8, 7, 6 };
  65. String[] mota = new String[] { "quần", "áo", "giá", "dép" };
  66.  
  67. ghiFile(gia, soLuong, mota, pathName);
  68. System.out.println(docFile(pathName));
  69. }
  70.  
  71. }
  72.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:9: error: class DemoData is public, should be declared in a file named DemoData.java
public class DemoData {
       ^
1 error
stdout
Standard output is empty