Java 8 File - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 File class.

This code example shows how to use the following methods:exists, isDirectory
        File baseDir = new File(testSupport.getDomainSlaveConfiguration().getDomainDirectory());
        validateDirectory(baseDir);
        File data = new File(baseDir, "data");
        validateDirectory(data);
        validateServerDirectory(data);
        File log = new File(baseDir, "log");
        validateDirectory(log);
        validateServerDirectory(log);
        File tmp = new File(baseDir, "tmp");
        validateDirectory(tmp);
        validateServerDirectory(tmp);
    } 
 
    private void validateServerDirectory(File typeDir) {
        File servers = new File(typeDir, "servers");
        validateDirectory(servers);
        File server = new File(servers, "main-three");
        validateDirectory(server);
    } 
 

9
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
        return defaultFile;
    } 
     
    public File asFile(File file) {
        try { 
            Files.copy(defaultFile, file);
            return file;
        } catch (Exception ex) {
            throw new UnexpectedException(ex);
        } 
    } 
     
    public File asFile(String name) {
        return asFile(new File(name));
    } 
 
    public byte[] asBytes() { 
        return IO.readContent(defaultFile);
    } 
 
9
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:canRead, exists
    private static final Properties userprops;
 
    static { 
        Properties props = new Properties();
 
        File file = new File(getUserHome(), JLINE_RC);
        if (file.exists() && file.canRead()) {
            try { 
                InputStream input = new BufferedInputStream(new FileInputStream(file));
                try { 
                    props.load(input);
                    Log.debug("Loaded user configuration: ", file);
                } 
                finally { 
                    input.close();
                } 
            } 
            catch (IOException e) {
                Log.warn("Unable to read user configuration: ", file, e);
            } 

9
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:delete, exists, lastModified
	} 
 
	@Override 
	public File get(String key) {
		File file = super.get(key);
		if (file.exists()) {
			boolean cached;
			Long loadingDate = loadingDates.get(file);
			if (loadingDate == null) {
				cached = false;
				loadingDate = file.lastModified();
			} else { 
				cached = true;
			} 
 
			if (System.currentTimeMillis() - loadingDate > maxFileAge) {
				file.delete();
				loadingDates.remove(file);
			} else if (!cached) {
				loadingDates.put(file, loadingDate);

9
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:
		return status;
	} 
	 
	private File constructFilePaths (String file1, String file2){
		File newPath;
		newPath = new File(file1+"/"+file2);
		return newPath;
	} 
 
} 
9
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:exists, getAbsolutePath, getName, isDirectory
    public static MixedDomainTestSupport create(String testClass, Version.AsVersion version) throws Exception {
        OldVersionCopier oldVersionCopier = OldVersionCopier.expandOldVersion(version);
        File dir = oldVersionCopier.getVersionDir(version);
 
        return new MixedDomainTestSupport(testClass, "master-config/domain.xml", "master-config/host.xml",
                "slave-configs/" + dir.getName() + "/domain/configuration/host-slave.xml", dir.getAbsolutePath());
    } 
} 

9
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:exists, isDirectory
 */ 
public class StandaloneMain { 
 
 
    public static void main(String[] args) throws Exception{
        File baseDir = new File(args[0]);
        if (!baseDir.exists() || !baseDir.isDirectory()) {
            throw new IllegalArgumentException("Base dir does not exist: " + baseDir.getAbsolutePath());
        } 
 
        File templateFile = new File(args[1]);
        if (!templateFile.exists()) {
            throw new IllegalArgumentException("Template file does not exist: " + templateFile.getAbsolutePath());
        } 
 
        File subsystemsFile = new File(args[2]);
        if (!subsystemsFile.exists()) {
            throw new IllegalArgumentException("Subsystems file does not exist " + subsystemsFile.getAbsolutePath());
        } 
 

9
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:delete, deleteOnExit, exists, getName
    protected final File fileName;
 
 
    ConfigurationFilePersistenceResource(final ModelNode model, final ConfigurationFile configurationFile,
                                         final AbstractConfigurationPersister persister) throws ConfigurationPersistenceException {
        super(model, persister);
        this.configurationFile = configurationFile;
        this.fileName = configurationFile.getMainFile();
    } 
 
    @Override 
    public void doCommit(ExposedByteArrayOutputStream marshalled) {
        final File tempFileName = FilePersistenceUtils.createTempFile(fileName);
        try { 
            try { 
                FilePersistenceUtils.writeToTempFile(marshalled, tempFileName);
            } catch (Exception e) {
                MGMT_OP_LOGGER.failedToStoreConfiguration(e, fileName.getName());
                return; 
            } 

9
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:delete, deleteOnExit, exists
            throw MESSAGES.failedToRenameTempFile(e, tempFileName, fileName);
        } 
    } 
 
    static void deleteFile(File file) {
        if (file.exists()) {
            if (!file.delete() && file.exists()) {
                file.deleteOnExit();
                throw new IllegalStateException(MESSAGES.couldNotDeleteFile(file));
            } 
        } 
    } 
 
    static File writeToTempFile(ExposedByteArrayOutputStream marshalled, File tempFileName) throws IOException {
        deleteFile(tempFileName);
 
        final FileOutputStream fos = new FileOutputStream(tempFileName);
        final InputStream is = marshalled.getInputStream();
        try { 
            BufferedOutputStream output = new BufferedOutputStream(fos);
9
CodeRank
Connect your IDE to all the code out there  Get Codota for Java
This code example shows how to use the following methods:exists, isDirectory
 */ 
public class DomainMain { 
 
 
    public static void main(String[] args) throws Exception {
        File baseDir = new File(args[0]);
        if (!baseDir.exists() || !baseDir.isDirectory()) {
            throw new IllegalArgumentException("Base dir does not exist: " + baseDir);
        } 
        File templateFile = new File(args[1]);
        if (!templateFile.exists()) {
            throw new IllegalArgumentException("Template file does not exist: " + templateFile);
        } 
        File subsystemsFile = new File(args[2]);
        if (!subsystemsFile.exists()) {
            throw new IllegalArgumentException("Subsystems file does not exist " + subsystemsFile);
        } 
        File outputFile = new File(args[3]);
 
 
9
CodeRank
Connect your IDE to all the code out there  Get Codota for Java

Related Java 8 File Questions & Answers:

Other Java 8 File Examples

1:	  File directory = Environment.getExternalStorageDirectory();
2:	  // assumes that a file article.rss is available on the SD card
3:	  File file = new File(directory + "/article.rss");
-
1:	  File directory = Environment.getExternalStorageDirectory();
2:	  // assumes that a file article.rss is available on the SD card
3:	  File file = new File(directory + "/article.rss");
4:	  if (!file.exists()) {
5:	    throw new RuntimeException("File not found");
-
1:	  File directory = Environment.getExternalStorageDirectory();
2:	  // assumes that a file article.rss is available on the SD card
3:	  File file = new File(directory + "/article.rss");
4:	  if (!file.exists()) {
5:	    throw new RuntimeException("File not found");

Read More

19:	
20:	  MediaRecorder recorder;
21:	  File audiofile = null;
22:	  private static final String TAG = "SoundRecordingActivity";
23:	  private View startButton;
-
37:	    stopButton.setEnabled(true);
38:	
39:	    File sampleDir = Environment.getExternalStorageDirectory();
40:	    try {
41:	      audiofile = File.createTempFile("sound", ".3gp", sampleDir);

Read More

5:	public class FileCache {
6:	 
7:	    private File cacheDir;
8:	 
9:	    public FileCache(Context context){
-
10:	        //Find the dir to save cached images
11:	        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
12:	            cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"TempImages");
13:	        else
14:	            cacheDir=context.getCacheDir();
-
17:	    }
18:	 
19:	    public File getFile(String url){
20:	        String filename=String.valueOf(url.hashCode());
21:	        File f = new File(cacheDir, filename);
-
19:	    public File getFile(String url){
20:	        String filename=String.valueOf(url.hashCode());
21:	        File f = new File(cacheDir, filename);
22:	        return f;
23:	 
-
19:	    public File getFile(String url){
20:	        String filename=String.valueOf(url.hashCode());
21:	        File f = new File(cacheDir, filename);
22:	        return f;
23:	 
-
25:	 
26:	    public void clear(){
27:	        File[] files=cacheDir.listFiles();
28:	        if(files==null)
29:	            return;
-
28:	        if(files==null)
29:	            return;
30:	        for(File f:files)
31:	            f.delete();
32:	    }

Read More

5:	
6:	    private File cacheDir;
7:	
8:	    public FileCache(Context context){
9:	        //Find the dir to save cached images
-
10:	        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
11:	            cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
12:	        else
13:	            cacheDir=context.getCacheDir();
14:	        if(!cacheDir.exists())
-
17:	
18:	    public File getFile(String url){
19:	        //I identify images by hashcode. Not a perfect solution, good for the demo.
20:	        String filename=String.valueOf(url.hashCode());
21:	        //Another possible solution (thanks to grantland)
-
22:	        //String filename = URLEncoder.encode(url);
23:	        File f = new File(cacheDir, filename);
24:	        return f;
25:	
26:	    }
-
22:	        //String filename = URLEncoder.encode(url);
23:	        File f = new File(cacheDir, filename);
24:	        return f;
25:	
26:	    }
-
28:	    public void clear(){
29:	        File[] files=cacheDir.listFiles();
30:	        if(files==null)
31:	            return;
32:	        for(File f:files)
-
31:	            return;
32:	        for(File f:files)
33:	            f.delete();
34:	    }
35:

Read More

8:		 * returning image / video
9:		 */
10:		private static File getOutputMediaFile(int type) {
11:	
12:			// External sdcard location
-
11:	
12:			// External sdcard location
13:			File mediaStorageDir = new File(
14:					Environment
15:							.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
-
11:	
12:			// External sdcard location
13:			File mediaStorageDir = new File(
14:					Environment
15:							.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
-
28:			String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
29:					Locale.getDefault()).format(new Date());
30:			File mediaFile;
31:			if (type == MEDIA_TYPE_IMAGE) {
32:				mediaFile = new File(mediaStorageDir.getPath() + File.separator
-
30:			File mediaFile;
31:			if (type == MEDIA_TYPE_IMAGE) {
32:				mediaFile = new File(mediaStorageDir.getPath() + File.separator
33:						+ "IMG_" + timeStamp + ".jpg");
34:			} else if (type == MEDIA_TYPE_VIDEO) {
-
33:						+ "IMG_" + timeStamp + ".jpg");
34:			} else if (type == MEDIA_TYPE_VIDEO) {
35:				mediaFile = new File(mediaStorageDir.getPath() + File.separator
36:						+ "VID_" + timeStamp + ".mp4");
37:			} else {

Read More

See Code Examples for Java 8 File Methods: