IMG-LOGO
×

Tài Liệu Học

Khoá học lập trình Java Core nâng cao

Exception & Collection

[Video] Tìm hiểu exception + debug + package - lập trình Java Nâng Cao [Video] Tìm hiểu collections, Sets, Maps và Generic - lập trình java nâng cao [Video] Chương trình quản lý sinh viên bằng HashMap - Java Advanced

File

[Video] Hướng dẫn đọc ghi file FileInputStream, FileOutputStream, ObjectInputStream trong java - lập trình java nâng cao [Video] Bài tập quản lý sách & lưu thông tin trên Files [Video] File - Quản lý thông tin sinh viên [Video] Quản lý thông tin sinh & lưu object file - Java nâng cao [Video] Viết ứng dụng WinRar | WinZip | 7Zip bằng Java - Nén file Java & giải nén file Java - Khoá học Java [Video] Bài tập quản lý bán vé máy bay - java nâng cao

Thread

[Video] Phân tích quản lý ứng dụng trong HĐH (Window, Mac, Linux)-Phân tích Game đa luồng Java [Video] Tìm hiểu Thread - Đồng bộ Thread - lập trình java nâng cao - Bổ sung thêm lớp học mới. [Video] Giải thích synchronized + Lap trinh Java nang cao [Video] Thread thực hiện in ngẫu nhiên các số nguyên và ký tự - Phần 1 [Video] Thread thực hiện in ngẫu nhiên các số nguyên và ký tự & Đông bộ thread + wait notify + synchronized + Java nâng cao - Phần 2 [Video] Synchronized 2 Thread - Bài tập mảng tên + địa chỉ - Java nâng cao [Video] Bài tập ôn luyện tổng hợp File, OOP, Thread trong java [Video] Sử dụng MultiThreading sinh ký tự a-zA-Z trong java

CSDL

[Video] Kết nối MySQL trong java swing JDBC trong java - lập trình java nâng cao [Video] Kết nối CSDL bằng java swing phần 1 - lập trình java nâng cao [Video] Kết nối CSDL bằng java swing phần 2 - lập trình java nâng cao [Video] Chương trình quản lý thư viện Java + XML Java + CSDL Java- SQL Java [Video] Phần mềm quản lý sinh viên MySQL + Java - Chương trình quản lý sinh viên MySQL + Java - Lập Trình Java [Video] Bài Tập Quản Lý Sinh Viên - CSDL - Java Swing

Ôn Tập Tổng Quát & Exmination

[Video] Bài tập - Chường trình quản lý sở thú - Lập trình Java - Lập trình Java nâng cao - Phần 2




Trang Chủ Java Advanced [Source Code] Tìm hiểu Generic & File (FileInputStream - FileOutputStream) - C2206L

[Source Code] Tìm hiểu Generic & File (FileInputStream - FileOutputStream) - C2206L

by GokiSoft.com - 20:05 15/03/2023 1,092 Lượt Xem

Text
Binary

File -> Rất nhiều thư viên được sử dung thao tác vs File
	 -> Chính được sử dụng trong dự án & thi cử
	 	File
	 	FileInputStream
	 	FileOutputStream
	 	BufferedReader
	 	BufferedWriter
	 	BufferedInputStream
	 	BufferedOutputStream
	 	ObjectInputStream
	 	ObjectOutputStream
=======================================================================
Path:
	- Đường dẫn tương đối
	- Đường dẫn tuyệt đối
		

#Test01.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gokisoft.bt1058;

import java.util.ArrayList;

/**
 *
 * @author teacher
 */
public class Test01 {
    public static void main(String[] args) {
        //Phan tich khai bao bien
        ArrayList<Integer> list1 = new ArrayList<>();
        list1.add(12);
        list1.add(33);
//        list1.add("12312312");
        int t = list1.get(0);
        System.out.println(t);
        
        ArrayList<String> list2 = new ArrayList<>();
        list2.add("123123");
        list2.add("34sdfsdf");
//        list2.add(123);
        
        String s = list2.get(0);
        System.out.println(s);
        
//        TestController<String> controller = new TestController<>();
//        controller.add("Vi du 1");
//        controller.add("Vi du 2");
//        controller.add("Vi du 3");
//        
//        controller.display();
        
        TestController<Student> controller2 = new TestController<>();
        controller2.add(new Student());
        controller2.add(new Student());
        
        controller2.display();
    }
}

#Test02.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gokisoft.bt1058;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author teacher
 */
public class Test02 {
    public static void main(String[] args) {
        //test01();
        //test02();
        //test03();
        //test04();
        test06();
    }
    
    static void test06() {
        String filename = "students.txt";
        
        FileInputStream fis = null;
        
        try {
            //B1. Mo file
            fis = new FileInputStream(filename);
            
            StringBuilder builder = new StringBuilder();
            
            int code;
            while((code = fis.read()) != -1) {
                if((char) code == "\n".charAt(0)) {
                    //1 dong chua thong tin sinh vien
                    String line = builder.toString();
                    
                    Student std = new Student();
                    std.parse(line);
                    std.display();
                    
                    builder = new StringBuilder();
                } else {
                    builder.append((char) code);
                }
            }
//            System.out.println(builder.toString());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            //B3. Dong file
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    static void test05() {
        String filename = "students.txt";
        
        FileInputStream fis = null;
        
        try {
            //B1. Mo file
            fis = new FileInputStream(filename);
            
            StringBuilder builder = new StringBuilder();
            
            int code;
            while((code = fis.read()) != -1) {
                builder.append((char) code);
//                System.out.println((char) code);
            }
            
            System.out.println(builder.toString());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            //B3. Dong file
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    static void test04() {
        Student std1 = new Student();
        Student std2 = new Student();
        std1.input();
        std2.input();
        
//        std1.display();
//        std2.display();
        
        String filename = "students.txt";
        
        FileOutputStream fos = null;
        try {
            //B1. Mo file
            fos = new FileOutputStream(filename, true);
            
            //B2. Doc & Ghi du lieu
            byte[] data = std1.toString().getBytes("utf8");
            fos.write(data);
            
            data = std2.toString().getBytes("utf8");
            fos.write(data);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            //B3. Dong file
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
    }
    
    static void test03() {
        String filename = "vidu.txt";
        Scanner scan = new Scanner(System.in);
        
        FileOutputStream fos = null;
        try {
            //B1. Mo file
            fos = new FileOutputStream(filename, true);
            
            //B2. Doc & Ghi du lieu
            String str;
            do {
                System.out.println("Nhap text can them vao file: ");
                str = scan.nextLine();
                byte[] data = ("\n" + str).getBytes("utf8");
                fos.write(data);
            } while(!str.equalsIgnoreCase("N"));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            //B3. Dong file
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    static void test02() {
        String filename = "vidu.txt";
        String str = "\nSinh viên Aptech 54 Lê Thanh Nghị";
        
        FileOutputStream fos = null;
        try {
            //B1. Mo file
            fos = new FileOutputStream(filename, true);
            
            //B2. Doc & Ghi du lieu
            byte[] data = str.getBytes("utf8");
            fos.write(data);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            //B3. Dong file
            if(fos != null) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    Logger.getLogger(Test02.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        
    }
    
    static void test01() {
        String filePath = "C:\\Users\\teacher\\Downloads\\Professional Programming in Java - Slide\\1.Slide\\Session 6.pdf";
        File file = new File(filePath);
        
        if(file.isFile()) {
            System.out.println("File > " + file.canRead() + ", " + file.canWrite() + ", " + file.canExecute());
        } else {
            System.out.println("Folder");
        }
    }
}

#TestController.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gokisoft.bt1058;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author teacher
 */
public class TestController<E extends Student> {
    List<E> dataList = new ArrayList<>();
    
    public void add(E e) {
        dataList.add(e);
    }
    
    public void display() {
        for (int i = 0; i < dataList.size(); i++) {
            System.out.println("Test ...");
        }
    }
}

Bình luận



Đã sao chép!!!