Example usage for java.io File separator

List of usage examples for java.io File separator

Introduction

In this page you can find the example usage for java.io File separator.

Prototype

String separator

To view the source code for java.io File separator.

Click Source Link

Document

The system-dependent default name-separator character, represented as a string for convenience.

Usage

From source file:Main.java

/***
 * write a line to the log file/*from   ww  w .j av  a 2  s .c o  m*/
 * 
 * @param line
 */
public static void writeToLog(String line) {
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date();
    File f = new File(Environment.getExternalStorageDirectory() + File.separator + folderName);
    boolean success = true;
    if (!f.exists()) {
        success = f.mkdir();
    }
    if (success) {
        // Do something on success
        File fileDir = new File(f, dateFormat.format(date) + ".txt");
        try {
            FileOutputStream os = new FileOutputStream(fileDir, true);
            os.write(line.getBytes());
            os.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void saveImageToSD(Context ctx, String filePath, Bitmap bitmap, int quality) throws IOException {
    if (bitmap != null) {
        File file = new File(filePath.substring(0, filePath.lastIndexOf(File.separator)));
        if (!file.exists()) {
            file.mkdirs();/* w ww . java2  s .c  o m*/
        }
        file = new File(filePath);
        if (!file.exists()) {
            file.createNewFile();
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        bitmap.compress(CompressFormat.JPEG, quality, bos);
        bos.flush();
        bos.close();
        if (ctx != null) {
            scanPhoto(ctx, filePath);
        }
    }
}

From source file:Main.java

public static File getDiskCacheDir(Context context, String fileName) {
    String cachePath;//ww  w . j a va  2 s  . c o  m
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
            && !Environment.isExternalStorageRemovable()) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }
    Log.d("bonus", "cachePath = " + cachePath);
    return new File(cachePath + File.separator + fileName);
}

From source file:Main.java

private static String createDir(String path) {
    boolean isHaveSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    if (!isHaveSDCard) {
        return null;
    }/* www . j  a  v  a 2  s  . c o  m*/
    File directory = Environment.getExternalStorageDirectory();
    File file = new File(directory.getAbsolutePath() + path);
    if (!file.exists()) {
        boolean isSuccess = file.mkdirs();
        if (isSuccess) {
            return file.getPath() + File.separator;
        } else {
            return null;
        }
    }
    return file.getPath() + File.separator;
}

From source file:Main.java

public static boolean writeFile(byte[] buffer, String folder, String fileName) {
    boolean writeSucc = false;
    boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    String folderPath = "";
    if (sdCardExist) {
        folderPath = Environment.getExternalStorageDirectory() + File.separator + folder + File.separator;
    } else {//  w w w.j  a va2 s .  co  m
        writeSucc = false;
    }

    File fileDir = new File((folderPath));
    if (!fileDir.exists()) {
        fileDir.mkdirs();
    }

    File file = new File(folderPath + fileName);
    FileOutputStream out = null;

    try {
        out = new FileOutputStream(file);
        out.write(buffer);
        writeSucc = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return writeSucc;
}

From source file:Main.java

public static void saveBackgroundImage(Context ctx, String filePath, Bitmap bitmap, int quality)
        throws IOException {
    if (bitmap != null) {
        File file = new File(filePath.substring(0, filePath.lastIndexOf(File.separator)));
        if (!file.exists()) {
            file.mkdirs();//from   w  ww .ja v  a2s . c  om
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        bitmap.compress(CompressFormat.PNG, quality, bos);
        bos.flush();
        bos.close();
        if (ctx != null) {
            scanPhoto(ctx, filePath);
        }
    }
}

From source file:Main.java

public static String saveImg(Bitmap b, String name) throws Exception {
    String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "QuCai/shareImg/";
    File mediaFile = new File(path + File.separator + name + ".jpg");
    if (mediaFile.exists()) {
        mediaFile.delete();//from   w  w w  .  j a  va 2s  .  com

    }
    if (!new File(path).exists()) {
        new File(path).mkdirs();
    }
    mediaFile.createNewFile();
    FileOutputStream fos = new FileOutputStream(mediaFile);
    b.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();
    b.recycle();
    b = null;
    System.gc();
    return mediaFile.getPath();
}

From source file:Main.java

public static int[] readBin83PtIndex(String dir, String fileName) {
    int i = 0;/*from  ww  w .  ja va  2s .  c o m*/
    int x = 0;
    int[] tab = new int[83];
    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        x = in.readInt();
        while (true) {
            tab[i] = x;
            i++;
            x = in.readInt();
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return tab;
}

From source file:Main.java

public static File getDiskCacheDir(Context context, String uniqueName) {
    // Check if media is mounted or storage is built-in, if so, try and use external cache dir
    // otherwise use internal cache dir
    final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable() ? Environment.getExternalStorageDirectory().getPath()
                    : context.getCacheDir().getPath();
    return new File(cachePath + File.separator + uniqueName);
}

From source file:Main.java

public static int[] readBinIntArray(String dir, String fileName, int size) {
    int x;/*w  w  w.jav a  2 s  . c  om*/
    int i = 0;
    int[] tab = new int[size];
    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        x = in.readInt();
        while (true) {
            tab[i] = x;
            i++;
            x = in.readInt();
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return tab;
}