Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.util.*;
import java.io.*;

public class Main {
    public static void saveBArrToFile(String fileName, byte[] barr) throws IOException {
        File file = new File(fileName);
        file.getParentFile().mkdirs();
        File tempFile = new File(fileName + ".tmp");
        FileOutputStream fos = new FileOutputStream(tempFile);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bos.write(barr, 0, barr.length);
        bos.flush();
        fos.flush();
        bos.close();
        fos.close();
        file.delete();
        tempFile.renameTo(file);
    }

    public static void close(Closeable closable) {
        if (closable != null) {
            try {
                closable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static boolean delete(File file) {
        file.setWritable(true);
        try {
            if (!file.delete()) {
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(0);
                fos.flush();
                fos.close();
            }
            Log.d("delete", "Deleted file: " + file + " successfully");
            return true;
        } catch (IOException e) {
            Log.d("delete", "The deleting file: " + file + " is not successfully", e);
            return false;
        }
    }

    private static void delete(File file, StringBuilder sb) {
        long length = file.length();
        boolean deleted = file.delete();
        if (deleted) {
            sb.append(file.getAbsolutePath() + " length " + length + " bytes, deleted.\r\n");
        } else {
            sb.append(file.getAbsolutePath() + " length " + length + " bytes, can't delete.\r\n");
        }
    }
}