Example usage for java.io File toString

List of usage examples for java.io File toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns the pathname string of this abstract pathname.

Usage

From source file:Main.java

private static Uri getUriFromAsset(Context mContext, String path) {
    File dir = mContext.getExternalCacheDir();

    if (dir == null) {
        Log.e(TAG, "Missing external cache dir");
        return Uri.EMPTY;
    }//from   w w  w  . j  ava 2  s . c  o  m
    String resPath = path.replaceFirst("file:/", "www");
    String fileName = resPath.substring(resPath.lastIndexOf('/') + 1);
    String storage = dir.toString() + STORAGE_FOLDER;

    if (fileName == null || fileName.isEmpty()) {
        Log.e(TAG, "Filename is missing");
        return Uri.EMPTY;
    }

    File file = new File(storage, fileName);
    FileOutputStream outStream = null;
    InputStream inputStream = null;

    try {
        File fileStorage = new File(storage);
        if (!fileStorage.mkdir())
            Log.e(TAG, "Storage directory could not be created: " + storage);

        AssetManager assets = mContext.getAssets();
        outStream = new FileOutputStream(file);
        inputStream = assets.open(resPath);

        copyFile(inputStream, outStream);
        outStream.flush();
        outStream.close();
        return Uri.fromFile(file);
    } catch (FileNotFoundException e) {
        Log.e(TAG, "File not found: assets/" + resPath);
    } catch (IOException ioe) {
        Log.e(TAG, "IOException occured");
    } catch (SecurityException secEx) {
        Log.e(TAG, "SecurityException: directory creation denied");
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outStream != null) {
                outStream.flush();
                outStream.close();
            }
        } catch (IOException ioe) {
            Log.e(TAG, "IOException occured while closing/flushing streams");
        }
    }
    return Uri.EMPTY;
}

From source file:Main.java

public static String mergeFlockedFiles(String FilePath) {

    String result = null;/*from  w w  w  .  j  a  v a2 s  .co  m*/
    try {
        result = java.net.URLDecoder.decode(FilePath, "UTF-8");
    } catch (UnsupportedEncodingException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    String fileName = result.substring(result.lastIndexOf('/') + 1);
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

        File flockedFilesFolder = new File(
                Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
        System.out.println("FlockedFileDir: " + flockedFilesFolder);
        long leninfile = 0, leng = 0;
        int count = 1, data = 0;
        int bytesRead = 0;
        try {
            File filename = new File(flockedFilesFolder.toString() + "/" + fileName);
            if (filename.exists())
                filename.delete();
            //BUFFER_SIZE = (int) filename.length();
            System.out.println("filename: " + filename);
            FileOutputStream fos = new FileOutputStream(filename, true);

            while (true) {
                File filepart = new File(filename + ".part" + count);
                System.out.println("part filename: " + filepart);
                if (filepart.exists()) {
                    FileInputStream fis = new FileInputStream(filepart);
                    byte fileBytes[] = new byte[(int) filepart.length()];
                    bytesRead = fis.read(fileBytes, 0, (int) filepart.length());
                    assert (bytesRead == fileBytes.length);
                    assert (bytesRead == (int) filepart.length());
                    fos.write(fileBytes);
                    fos.flush();
                    fileBytes = null;
                    fis.close();
                    fis = null;
                    count++;
                    filepart.delete();
                } else
                    break;
            }
            fos.close();
            fos = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return fileName;
}

From source file:Main.java

public static boolean StoreByteImage(Context mContext, byte[] imageData, int quality, String expName) {

    File sdImageMainDirectory = new File("/sdcard/myImages");
    FileOutputStream fileOutputStream = null;
    String nameFile = "";
    try {//from   ww w. j a  va2s . c o  m

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 5;

        Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);

        fileOutputStream = new FileOutputStream(sdImageMainDirectory.toString() + "/" + nameFile + ".jpg");

        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

        myImage.compress(CompressFormat.JPEG, quality, bos);

        bos.flush();
        bos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return true;
}

From source file:com.splunk.shuttl.testutil.TUtilsFile.java

/**
 * @return a new file having same contents (but different path) as the
 *         specified file.// w  w  w .  jav  a 2 s. c  om
 */
public static File createFileWithContentsOfFile(File file) {
    File newFile = createFile();
    try {
        FileUtils.copyFile(file, newFile);
    } catch (IOException e) {
        TUtilsTestNG.failForException("Couldn't create file with contents of " + file.toString(), e);
    }
    newFile.deleteOnExit();
    return newFile;
}

From source file:com.ibm.watson.catalyst.corpus.tfidf.Template.java

public static Pattern getTemplatePattern(File file, String before, String after) {
    StringBuffer sb = new StringBuffer();

    List<String> verbs = new ArrayList<String>();
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        while (br.ready())
            verbs.add(br.readLine());/*  ww  w  . ja  va  2  s . c  om*/
    } catch (FileNotFoundException e) {
        throw new IllegalStateException("Could not find " + file.toString());
    } catch (IOException e) {
        throw new IllegalStateException("IO Error reading " + file.toString());
    }

    sb.append(before).append(makeOr(verbs)).append(after);

    Pattern p = Pattern.compile(sb.toString(), Pattern.CASE_INSENSITIVE);
    return p;
}

From source file:com.cjwagner.InfoSecPriv.ExtensionServer.java

private static void initializeLogStore() {

    System.out.println("Date: " + new Date().getTime());
    System.out.println("Initializing Log Store...");

    int fileLoadCount = 0;
    storeSize = 0;//from   w  w  w  . jav  a 2s . com
    logStore = new ConcurrentHashMap<String, LoggerMessage>();
    File dir = new File(logDir);
    dir.mkdir();

    File[] files = dir.listFiles();
    for (File file : files) {
        String fullName = file.getName();
        String ext = getExtension(fullName);
        String ip = rmvExtension(fullName);
        if (!ext.equals(logFileExt) || ip.length() == 0) {
            continue;
        }
        LoggerMessage data = null;
        try {
            System.out.println(file.toString());
            String json = FileUtils.readFileToString(file, null);
            data = LoggerMessage.fromJSON(json);
        } catch (Exception e) {
            System.out.println("Failed to parse file: " + fullName + "  ERROR: " + e.getMessage());
            continue;
        }
        //data is verified at this point
        LoggerMessage rec = logStore.get(ip);
        if (rec == null) {
            logStore.put(ip, data);
        } else {
            rec.getLogs().addAll(data.getLogs());
        }
        storeSize += data.getLogs().size();
        fileLoadCount++;
    }
    System.out.println(
            "Initialization complete.  Files loaded: " + fileLoadCount + "  Total logs loaded: " + storeSize);
}

From source file:net.sf.ehcache.config.ConfigurationFactory.java

/**
 * Configures a bean from an XML file./* ww  w  .j  av a  2  s.  c o m*/
 */
public static Configuration parseConfiguration(final File file) throws CacheException {
    if (file == null) {
        throw new CacheException("Attempt to configure ehcache from null file.");
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("Configuring ehcache from file: " + file.toString());
    }
    Configuration configuration = null;
    InputStream input = null;
    try {
        input = new BufferedInputStream(new FileInputStream(file));
        configuration = parseConfiguration(input);
    } catch (Exception e) {
        throw new CacheException("Error configuring from " + file + ". Initial cause was " + e.getMessage(), e);
    } finally {
        try {
            if (input != null) {
                input.close();
            }
        } catch (IOException e) {
            LOG.error("IOException while closing configuration input stream. Error was " + e.getMessage());
        }
    }
    return configuration;
}

From source file:ee.ria.DigiDoc.util.FileUtils.java

private static void clearDirectory(File directory) {
    if (directory.isDirectory()) {
        for (File child : directory.listFiles()) {
            boolean delete = child.delete();
            if (delete) {
                Timber.d("clearDirectory() called with: directory = %s File %s deleted", directory.toString(),
                        child.getName());
            }/*ww  w  . j  av a  2  s .c  o  m*/
        }
    }
}

From source file:it.geosolutions.geobatch.unredd.script.util.FileManager.java

/**
 * @deprecated use apache FileUtils directly
 *///from   w  w  w. j  av  a2  s  .co m
public static String copyfile(String destDir, String destFileName, String srcFileName) {
    File source = new File(srcFileName);
    File dest = new File(destDir, destFileName);
    try {
        FileUtils.copyFile(source, dest);
        LOGGER.info("File copied: " + source + " --> " + dest);
    } catch (IOException ex) {
        LOGGER.warn("Error copying file: " + source + " --> " + dest, ex);
        return null;
    }
    // TODO: check original return line
    return dest.toString();
}

From source file:com.ms.commons.test.classloader.IntlTestURLClassPath.java

private static void copyDirectoryTo(File dirFrom, File dirTo) throws IOException {
    if (dirFrom.exists() && dirFrom.isDirectory()) {
        FileUtil.copyDirectory(dirFrom, dirTo, new FileFilter() {

            public boolean accept(File pathname) {
                return (!pathname.toString().contains(".svn"));
            }/*from  w  ww  .  j a  v a  2  s .co  m*/
        });
    }
}