.:: CODE SNIPPET ::.

"Your time is limited, so don't waste it living someone else's life"

Tag Archives: file

How to decompress zip file in Java


Introduce how to decompress a zip file in Java:

public void unzip(File zipFile, String outputFolder)
    {
        byte[] buffer = new byte[1024];
        ZipEntry ze = null;
        try
        {
            File folder = new File(outputFolder);
            if (!folder.exists())
            {
                folder.mkdir();
            }
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
            while ((ze = zis.getNextEntry()) != null)
            {
                String fileName = ze.getName();
                File newFile = new File(outputFolder + File.separator + fileName);
                System.out.println("unziping : " + 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();
            }
            zis.close();
        }
        catch (Exception ex)
        {
            if (zipFile != null)
            {
                System.out.println("Do not decompress file " + zipFile.getName() + " (size: 0kb)!");
            }
        }
    }
Advertisements

Read and write a txt file in C#


In the case you want to read or write some short information from or into a text file, Csharp supports a simple way for you in System.IO.File. You create a text file in directory “C:\\config.txt” and then:
Read file
There are some lines in above file, and each line has its own meaning, so you need to read every line and write into a list or an array like this:

private void readFile()
{
    string[] lines = System.IO.File.ReadAllLines(@"C:\config.txt");
    //then you can convert every element of the array into the data you want
}

Write file
You have some information need to be stored in a text file, you can use the below function:

private void writeFile()
{
    string[] lines = { lstCube.Count.ToString(), numOfXRect.ToString(), count.ToString() };
}

Are they so simple,hihi, i just want to store in case i need.:D

%d bloggers like this: