[Gukulkan]

Learn, develop, play.

Create folder Java

We are having 2 variants:
1 – when we want to create single folder (C:\first)
2 – when we want to create all tree for folders (C:\1\2\3)

Let’s try to create it:

1
2
3
4
5
6
7
//First variant
File folder= new File("C:\\first");
file.mkdir();

//Second variant
File folders= new File("C:\\1\\2\\3");
files.mkdirs();

After that we will see all folder which we want.

Create temp file in Java

Sometimes you need to crate temp files and clean memory
java.io.File library helps to do this

Example:

1
2
3
4
5
6
try{
File tempFile = File.createTempFile("tempFileName", ".tmp");
System.out.println(tempFile.getAbsolutePath());
}catch(IOException ex){
ex.printStackTrace();
}

This code create file like tempFileName31231.tmp

Unzip file from GZip Java

We have already zip file in GZip format, lets unzip it

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
String INPUT_FILE = "/home/user/fileArchive.gz";
String OUTPUT_FILE = "/home/user/fileText.txt";

byte[] buffer = new byte[1024];
try{
GZIPInputStream gZIPInputStream = new GZIPInputStream(new FileInputStream(INPUT_FILE));
FileOutputStream fileOutputStream = new FileOutputStream(OUTPUT_FILE);
int len;
while ((len = gZIPInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, len);
}
gZIPInputStream.close();
fileOutputStream.close();

}catch(IOException ex){
ex.printStackTrace();  
}

Archive file in GZip Java

We have already create zip file in java, lets check it with GZip

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private String OUTPUT_FILE = "/home/user/fileArchive.gz";
private String INPUT_FILE = "/home/user/fileText.txt";
 
byte[] buffer = new byte[1024];
try{
GZIPOutputStream gZIPOutputStream = new GZIPOutputStream(new FileOutputStream(OUTPUT_FILE));
 
FileInputStream fileInputStream = new FileInputStream(INPUT_FILE);
int len;
while ((len = fileInputStream.read(buffer)) > 0) {
gZIPOutputStream.write(buffer, 0, len);
}
fileInputStream.close();
gZIPOutputStream.finish();
gZIPOutputStream.close();

}catch(IOException ex){
 ex.printStackTrace();  
}

Unzip file Java

UnZip file in java not so easy like pack it.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
String zipFile="C:\\MyFile.zip";
String outputFolder="C:\\outputzip";
 
byte[] buffer = new byte[1024];
try{

File folder = new File(outputFolder);
if(!folder.exists()){
 folder.mkdir();
}
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));

ZipEntry ze = zis.getNextEntry();
 
while(ze!=null){
 
String fileName = ze.getName();
File newFile = new File(outputFolder + File.separator + fileName);
 
System.out.println("file unzip : "+ newFile.getAbsoluteFile());

new File(newFile.getParent()).mkdirs();
 
FileOutputStream fos = new FileOutputStream(newFile);            
 
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
 
fos.close();  
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
 
System.out.println("Done");
 
}catch(IOException ex){
 ex.printStackTrace();
}

Zip archive file Java

Java core has library for zip packing, it is java.util.zip

Here short example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
byte[] buffer = new byte[1024];
try{
FileOutputStream fileOutputStream = new FileOutputStream("C:\\myFile.zip");
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream );
ZipEntry zipEntry = new ZipEntry("myLog.log");
zipOutputStream.putNextEntry(zipEntry );
FileInputStream fileInputStream= new FileInputStream("C:\\myLog.log");
int len;
while ((len = in.read(buffer)) > 0) {
zipOutputStream .write(buffer, 0, len);
}
in.close();
zipOutputStream.closeEntry();
zipOutputStream.close();

}catch(IOException e){
e.printStackTrace();
}

Byte array to file Java

Like in last example we using java core library FileOutputStream

here example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
FileInputStream fileInputStream=null;
 
File myFile = new File("C:\\myFile.txt");
 
byte[] byteFile = new byte[(int) myFile.length()];
 
try {

fileInputStream = new FileInputStream(myFile);
fileInputStream.read(byteFile);
fileInputStream.close();
 
FileOutputStream fileOuputStream = new FileOutputStream("C:\\toFile.txt");
fileOuputStream.write(bFile);
fileOuputStream.close();

}catch(Exception ex){
ex.printStackTrace();
}

Generic byte array from file Java

For this activity we using library FileInputStream

here simple example for this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
FileInputStream fileInputStream=null;
 
File myFile = new File("C:\\myFile.txt");
 
byte[] byteFile = new byte[(int) myFile.length()];
 
try {
fileInputStream = new FileInputStream(myFile);
fileInputStream.read(byteFile);
fileInputStream.close();
 
for (int i = 0; i < byteFile.length; i++) {
System.out.print((char)byteFile[i]);
}

}catch(Exception ex){
ex.printStackTrace();
}