By GokiSoft.com|
19:31 03/06/2024|
Java Advanced
[Share Code] Tìm hiểu File Object & Zip File - Java 2 - C2307L
#Book.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson02;
import java.io.Serializable;
import java.util.Scanner;
/**
*
* @author teacher
*/
public class Book implements Serializable{
String name;
int price;
public Book() {
}
public Book(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "name=" + name + ", price=" + price;
}
public void parseData(String line){
//parse
}
public void display() {
System.out.println(this);
}
public void input() {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap ten: ");
name = scan.nextLine();
System.out.println("Nhap gia: ");
price = Integer.parseInt(scan.nextLine());
}
}
#Main.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson02;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author teacher
*/
public class Main {
public static void main(String[] args) {
//testWriteFile();
// testWriteObject();
testReadObject();
}
private static void testWriteFile() {
FileWriter writer = null;
BufferedWriter bufferedWriter = null;
try {
writer = new FileWriter("vidu.txt");
bufferedWriter = new BufferedWriter(writer);
//Test write (unicode -> OK)
String line = "SINH VIEN APTECH 19 LE THANH NGHI\n";
bufferedWriter.write(line);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (writer != null) {
try {
writer.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
private static void testWriteObject() {
//Mang Book
List<Book> dataList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Book b = new Book();
b.input();
dataList.add(b);
}
//Lam the nao -> save data vao file
//Cach 1: Luu ca mang vao luon
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("vidu2.dat");
oos = new ObjectOutputStream(fos);
//Luu ca mang
// oos.writeObject(dataList);
for (Book book : dataList) {
oos.writeObject(book);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//End write
}
private static void testReadObject() {
List<Book> dataList;
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("vidu2.dat");
ois = new ObjectInputStream(fis);
// dataList = (List<Book>) ois.readObject();
Book b;
dataList = new ArrayList<>();
while(true) {
try {
b = (Book) ois.readObject();
} catch(IOException | ClassNotFoundException e) {
break;
}
dataList.add(b);
}
for (Book book : dataList) {
book.display();
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//End file
}
}
#readme.txt
Nội dung kiến thức:
- File
FileWriter/BufferedWriter
Read/Write Object -> File
- Zip
- Zip file lúc đọc
- Zip file lúc ghi
- Unzip file lúc đọc
- Unzip file lúc ghi
=====================================================
4 bytes -> 1 ky tu
32bit -> 2^32 ky tu khac nhau -> font (unicode)
30 ky tu khac nhau -> trong file vidu.txt
30 -> 2^5 = 32 ky tu khac nhau
A-> 00001
B-> 00010
C-> 00011
...
...
File Zip -> table hash
Unzip -> 5bit -> 1 ky -> kiem tra trong table hash -> ky tu gi -> unicode
#Test.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.gokisoft.lesson02;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.DeflaterInputStream;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;
/**
*
* @author teacher
*/
public class Test {
public static void main(String[] args) {
// testZip1();
// testZip2();
testUnzip1();
}
private static void testUnzip1() {
FileInputStream fis = null;
InflaterInputStream iis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("vidu1.def");
iis = new InflaterInputStream(fis);
int code;
fos = new FileOutputStream("vidu1-unzip.txt");
while((code = iis.read()) != -1) {
fos.write(code);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(iis != null) {
try {
iis.close();
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(fis != null) {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//End
}
private static void testZip2() {
FileInputStream fis = null;
FileOutputStream fos = null;
DeflaterOutputStream dos = null;
try {
fis = new FileInputStream("vidu.txt");
int code;
fos = new FileOutputStream("vidu-unzip1.txt");
dos = new DeflaterOutputStream(fos);
while((code = fis.read()) != -1) {
dos.write(code);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(dos != null) {
try {
dos.close();
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//End
}
private static void testZip1() {
FileInputStream fis = null;
DeflaterInputStream dis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("vidu.txt");
dis = new DeflaterInputStream(fis);
int code;
fos = new FileOutputStream("vidu1.def");
while((code = dis.read()) != -1) {
fos.write(code);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(dis != null) {
try {
dis.close();
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(fis != null) {
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
//End
}
}
Tags: