By GokiSoft.com|
10:11 09/09/2021|
Java Advanced
Bài tập nén File & giải nén File & Liệt kê files trong 1 thư mục - winrar - winzip
Bài 1 :
Tìm 1 file text có kích cỡ lớn sau đó nén dữ liệu vào 1 file khác đặt tên là : vidu.data & Viết giải thuật giải nén file.
Bài 2:
Liệt kê tất cả các file trong 1 thư mục -> Vẽ cấu trúc cây folder
Ví dụ: Nhập vào folder tên là ABC -> Kết quả sẽ như sau
ABC
-- a.txt
-- b.txt
-- AB
---- bb.txt
---- BB
------ cc.png
------ dd.jpg
-- k.doc
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Vũ Việt Đức [C1907L]](https://www.gravatar.com/avatar/114894070fbd15fc0c29ffdeab37f4b5.jpg?s=80&d=mm&r=g)
Vũ Việt Đức
2020-04-29 13:51:52
/*
* 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 lession4;
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.DeflaterOutputStream;
/**
*
* @author ADMIN
*/
public class Bai1 {
public static void main(String[] args) {
FileInputStream fis = null;
DeflaterOutputStream dos = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("vidu.txt");
fos = new FileOutputStream("vidu.zip");
dos = new DeflaterOutputStream(fos);
int code;
while((code = fis.read()) != -1){
dos.write(code);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Bai1.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Bai1.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if(fis != null){
try {
fis.close();
} catch (IOException ex) {
Logger.getLogger(Bai1.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(fos != null){
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(Bai1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
![Phạm Ngọc Minh [T1907A]](https://www.gravatar.com/avatar/9fa44938f36838ed4b4ac8e30e070c74.jpg?s=80&d=mm&r=g)
Phạm Ngọc Minh
2020-04-15 07:08:26
package Bt;
import java.io.File;
public class b2 {
public static void main(String[] args) {
File file = new File("d:/Java2");
String[] fileList = file.list();
for (int i=0; i< fileList.length; i++) {
System.out.println(fileList[i]);
}
}
}
package Bt;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.InflaterInputStream;
public class Inflater {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("d:/Java2/Test.def");
InflaterInputStream iis = new InflaterInputStream(fis);
FileOutputStream fos = new FileOutputStream("d:/Java2/Test1.txt");
int code;
while ((code = iis.read()) != -1 ) {
fos.write(code);
}
}
}
package Bt;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterInputStream;
public class Deflater {
public static void main(String[] args) throws FileNotFoundException, IOException {
DeflaterInputStream dis;
FileOutputStream fos;
try (FileInputStream fis = new FileInputStream("d:/Java2/Test.txt")) {
dis = new DeflaterInputStream(fis);
fos = new FileOutputStream("d:/Java2/Test.def");
int code ;
while ((code = dis.read()) != -1) {
fos.write(code);
}
}
dis.close();
fos.close();
System.out.println(" End deflater !!! >>>>");
}
}
![thienphu [T1907A]](https://www.gravatar.com/avatar/c4573ea65e411176c1852fd8584f1ab1.jpg?s=80&d=mm&r=g)
thienphu
2020-03-26 11:39:55
/*
* 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 BufferReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterInputStream;
/**
*
* @author Thien Phu
*/
public class ChuyendataSangFile {
public static void main(String[] args) throws FileNotFoundException, IOException {
String filename = "D:\\baitapjava\\BT1\\src\\BufferReader\\student.txt";
FileInputStream fis = new FileInputStream(filename);
DeflaterInputStream dis = new DeflaterInputStream(fis);
String newfile = "D:\\baitapjava\\BT1\\src\\BufferReader\\student2.def";
FileOutputStream fos = new FileOutputStream(newfile);
int code;
while ((code = dis.read()) != -1) {
fos.write(code);
}
fos.close();
dis.close();
fis.close();
System.out.println("Finish chuyen file");
}
}
![thienphu [T1907A]](https://www.gravatar.com/avatar/c4573ea65e411176c1852fd8584f1ab1.jpg?s=80&d=mm&r=g)
thienphu
2020-03-26 11:34:44
/*
* 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 BTVeFile;
import java.io.File;
/**
*
* @author Thien Phu
*/
public class ShowFile {
public static void main(String[] args) {
String folder = "D:\\baitapjava";
File file = new File(folder);
//in ten folder ra man hinh
System.out.println(file.getAbsoluteFile());
File[] children = file.listFiles();
System.out.println("Danh sach cac file trong thu muc \n");
for (File children1 : children) {
System.out.println(children1);
}
}
}
![Đỗ Văn Huấn [T1907A]](https://www.gravatar.com/avatar/04c40301dd027839d265b3c3c9dc6e6b.jpg?s=80&d=mm&r=g)
Đỗ Văn Huấn
2020-03-26 03:53:11
package java2_Advanced.BaiTapNgay25_3_2020.GiaiNen_File.Bai1;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterInputStream;
import java.util.zip.InflaterInputStream;
public class ZipFile {
public static void main(String[] args) throws IOException {
deflaterFile();
intlaterFile();
}
static void deflaterFile() throws IOException {
FileInputStream fis = new FileInputStream("D://FPT.txt");
DeflaterInputStream dis = new DeflaterInputStream(fis);
FileOutputStream fos = new FileOutputStream("d:/vidu.data");
int a;
while ((a = dis.read()) != -1) {
fos.write(a);
}
fis.close();
dis.close();
fos.close();
System.out.println("Nen file ket thuc.");
}
static void intlaterFile() throws IOException {
FileInputStream fis = new FileInputStream("d:/vidu.data");
InflaterInputStream iis = new InflaterInputStream(fis);
FileOutputStream fos = new FileOutputStream("D://FPT.txt");
int a;
while ((a = iis.read()) != -1) {
fos.write(a);
}
fis.close();
iis.close();
fos.close();
System.out.println("Giai nen file ket thuc.");
}
}
![Minh Nghia [T1907A]](https://www.gravatar.com/avatar/ecca255d725eed36a872105205af1b8e.jpg?s=80&d=mm&r=g)
Minh Nghia
2020-03-25 14:15:26
/*
* 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 FileNen;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterInputStream;
/**
*
* @author Administrator
*/
public class Bai1 {
public static void main(String[] args) throws FileNotFoundException, IOException {
String filename = "message.txt";
FileInputStream fis = new FileInputStream(filename);
DeflaterInputStream dis = new DeflaterInputStream(fis);
String zipFileName = "vidu.data";
FileOutputStream fos = new FileOutputStream(zipFileName);
int code;
while((code = dis.read()) != -1){
fos.write(code);
}
fos.close();
dis.close();
fis.close();
System.out.println("Ket thua qua trinh nen file");
}
}
/*
* 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 FileNen;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.InflaterInputStream;
/**
*
* @author Administrator
*/
public class Bai1b {
public static void main(String[] args) throws FileNotFoundException, IOException {
String filename = "vidu.data";
FileInputStream fis = new FileInputStream(filename);
InflaterInputStream iis = new InflaterInputStream(fis);
FileOutputStream fos = new FileOutputStream("message2.txt");
int code;
while ((code = iis.read()) != -1) {
fos.write(code);
}
fis.close();
iis.close();
fos.close();
System.out.println("Ket thuc qua trinh giai nen file");
}
}
/*
* 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 FileNen;
import java.io.File;
/**
*
* @author Administrator
*/
public class Files {
public static void main(String[] args) {
File listed = new File("C:\\Users\\Administrator\\Documents\\NetBeansProjects\\Java2");
System.out.println("Danh sach file:");
String[] file = listed.list();
for (String n : file) {
System.out.println(n);
}
}
}
![Trương Công Vinh [T1907A]](https://www.gravatar.com/avatar/223a7e3a46f4a747f81b921fe023fcc4.jpg?s=80&d=mm&r=g)
Trương Công Vinh
2020-03-25 09:57:29
/*
* 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 Tese;
import java.io.File;
/**
*
* @author DELL
*/
public class b2 {
public static void main(String[] args) {
File file = new File("d:/Java2");
String[] fileList = file.list();
for (int i=0; i< fileList.length; i++) {
System.out.println(fileList[i]);
}
}
}
/*
* 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 Tese;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.InflaterInputStream;
/**
*
* @author DELL
*/
public class Inflater {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("d:/Java2/Test.def");
InflaterInputStream iis = new InflaterInputStream(fis);
FileOutputStream fos = new FileOutputStream("d:/Java2/Test1.txt");
int code;
while ((code = iis.read()) != -1 ) {
fos.write(code);
}
}
}
/*
* 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 Tese;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterInputStream;
/**
*
* @author DELL
*/
public class Deflater {
public static void main(String[] args) throws FileNotFoundException, IOException {
DeflaterInputStream dis;
FileOutputStream fos;
try (FileInputStream fis = new FileInputStream("d:/Java2/Test.txt")) {
dis = new DeflaterInputStream(fis);
fos = new FileOutputStream("d:/Java2/Test.def");
int code ;
while ((code = dis.read()) != -1) {
fos.write(code);
}
}
dis.close();
fos.close();
System.out.println(" End deflater !!! >>>>");
}
}