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

public static float[] readBinFloat(String dir, String fileName, int size) {
    float x;// w  w w.  j  a va2 s  .co  m
    int i = 0;
    float[] tab = new float[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.readFloat();
        while (i < size) {
            tab[i] = x;
            i++;
            x = in.readFloat();
        }
    } 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 getOutputMediaFile(Context mContext) {
    File mediaStorageDir = mContext.getExternalFilesDir(cacheCampareImage);
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }/*  w  w  w. j av  a 2 s .c om*/
    }
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;

    mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    return mediaFile;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static File getDiskCacheDir(Context context, String uniqueName) {
    String cachePath;/* w ww  .  ja v  a  2 s .c o  m*/
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageEmulated()) {
        cachePath = context.getExternalCacheDir().getPath();
    } else {
        cachePath = context.getCacheDir().getPath();
    }

    return new File(cachePath + File.separator + uniqueName);
}

From source file:com.cprassoc.solr.auth.util.AuthManagerScriptRenderer.java

public static void renderSolrAuthScript(String pathToSolr) {
    String mime = "sh";
    if (Utils.isWindows()) {
        mime = "bat";
    }//from   w  ww  .  jav a  2  s  .  c  om

    try {

        String script = "cd " + pathToSolr + "\n\r";

        script += "server" + File.separator + "scripts" + File.separator + "cloud-scripts" + File.separator
                + "zkcli." + mime + " -zkhost localhost:9983 -cmd putfile /security.json security.json \n\r";
        //  IOUtils.write(script, new FileWriter(new File(SOLR_AUTH_SCRIPT_NAME+mime)));
        Utils.writeBytesToFile(SOLR_AUTH_SCRIPT_NAME + mime, script);

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

From source file:Main.java

public static float[] readBinTextureArray(String dir, String fileName, int size) {
    float x;//w w w.j  av a 2 s  . com
    int i = 0;
    float[] tab = new float[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.readFloat(); // convert here for OpenGl
        while (true) {
            tab[i] = x / 255.0f;
            i++;
            x = in.readFloat(); // convert here for OpenGl
        }
    } 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 float[] readBinShapeArray(String dir, String fileName, int size) {
    float x;/*w ww.  ja  va2s.c o m*/
    int i = 0;
    float[] tab = new float[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 {
        //read first x
        x = in.readFloat();
        while (true) {
            tab[i] = x;
            i++;
            x = in.readFloat();
        }
    } 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 String getFileNameFromPath(final String path) {
    if (TextUtils.isEmpty(path)) {
        return path;
    }//from www  . j a v  a 2 s  . co  m
    String[] arr = path.split(File.separator);
    return arr.length == 0 ? "" : arr[arr.length - 1];
}

From source file:Main.java

/**
 * Returns the path of one File relative to another.
 * <p/>//from  ww  w  .j  av  a2s. com
 * From http://stackoverflow.com/questions/204784
 *
 * @param target the target directory
 * @param base   the base directory
 * @return target's path relative to the base directory
 */
public static String getRelativePath(File target, File base) {
    String[] baseComponents;
    String[] targetComponents;
    try {
        baseComponents = base.getCanonicalPath().split(Pattern.quote(File.separator));
        targetComponents = target.getCanonicalPath().split(Pattern.quote(File.separator));
    } catch (IOException e) {
        return target.getAbsolutePath();
    }

    // skip common components
    int index = 0;
    for (; index < targetComponents.length && index < baseComponents.length; ++index) {
        if (!targetComponents[index].equals(baseComponents[index]))
            break;
    }

    StringBuilder result = new StringBuilder();
    if (index != baseComponents.length) {
        // backtrack to base directory
        for (int i = index; i < baseComponents.length; ++i)
            result.append("..").append(File.separator);
    }
    for (; index < targetComponents.length; ++index)
        result.append(targetComponents[index]).append(File.separator);
    if (!target.getPath().endsWith("/") && !target.getPath().endsWith("\\")) {
        // remove final path separator
        result.delete(result.length() - "/".length(), result.length());
    }
    return result.toString();
}

From source file:Main.java

public static void writeFile(String directoryPath, String fileName, String content) throws IOException {

    if (notEmptyStr(directoryPath) && notEmptyStr(fileName)) {

        FileOutputStream fos = null;

        try {//from   w  ww .j  a v  a 2  s .c  om
            File directory = new File(directoryPath);

            if (!directory.exists()) {
                directory.mkdirs();
            }
            File file = new File(directoryPath + File.separator + fileName);
            fos = new FileOutputStream(file);
            byte[] contentBytes = content.getBytes("UTF-8");
            fos.write(XML_HEADER.getBytes("UTF-8"));
            fos.write(contentBytes);

        } finally {
            if (fos != null) {
                fos.close();
            }
        }
    }
}

From source file:Main.java

/**
 * /*from  w w  w  . ja  va  2 s.co  m*/
 * @param subDirName
 * @return
 */
public static String[] constructFileList(String subDirName) {
    List<String> fileList = null;

    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        File subDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
                + DIR_NAME + File.separator + subDirName);

        if (subDir.exists()) {
            File[] files = subDir.listFiles();

            for (int i = 0; i < files.length; i++) {
                if (fileList == null) {
                    fileList = new ArrayList<String>();
                }

                // remove the extension
                fileList.add(files[i].getName().split("\\.")[0]);
            }
        } else {
            Log.e(LOG_TAG, "Nothing to import");
        }
    }

    return fileList.toArray(new String[fileList.size()]);
}