[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:


//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.

Write to temp file Java

Previous we just create empty temp file
Lets write there some text

Example:

try{
File tempFile = File.createTempFile("tempFile", ".tmp");
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(tempFile));
bufferedWriter.write("This is Test for tempFile!");
bufferedWriter.close();
}catch(IOException ex){
ex.printStackTrace();
}

Create temp file in Java

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

Example:


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:

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:

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


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


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

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


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(); }

Generate checksum Java

It is simple example for generating checksum in java


String myFile= "c:\\myFile.TXT";

MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
FileInputStream fileInputStream = new FileInputStream(myFile);
byte[] dataBytes = new byte[1024];

int nread = 0;

while ((nread = fileInputStream.read(dataBytes)) != -1) {
messageDigest.update(dataBytes, 0, nread);
};

byte[] mdbytes = messageDigest.digest();

StringBuffer sb = new StringBuffer("");
for (int i = 0; i < mdbytes.length; i++) { sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1)); } System.out.println(sb.toString());

In the en you will see checksum