Best code examples for Java 8 File class (java.io.File)

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 File class.
You can now enable Codota on your own code to easily search and navigate your Java codebase.

Java 8 File examples from Open Source projects
This code example shows how to use the following methods: exists, isDirectory
60:	File log = new File(baseDir, "log"); 
-
76:	assertTrue(file + " exists", file.exists()); 
77:	assertTrue(file + " is a directory", file.isDirectory()); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods:
39:	Files.copy(defaultFile, file); 
-
47:	return asFile(new File(name)); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: canRead, exists
44:	File file = new File(getUserHome(), JLINE_RC); 
45:	if (file.exists() && file.canRead()) { 
-
50:	Log.debug("Loaded user configuration: ", file); 
-
57:	Log.warn("Unable to read user configuration: ", file, e); 
-
61:	Log.trace("User configuration file missing or unreadable: ", file); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: delete, exists, lastModified
70:	if (file.exists()) { 
-
72:	Long loadingDate = loadingDates.get(file); 
-
75:	loadingDate = file.lastModified(); 
-
81:	file.delete(); 
82:	loadingDates.remove(file); 
-
84:	loadingDates.put(file, loadingDate); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods:
122:	newPath = new File(file1+"/"+file2); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: exists, getAbsolutePath, getName, isDirectory
48:	"slave-configs/" + dir.getName() + "/domain/configuration/host-slave.xml", dir.getAbsolutePath()); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: exists, isDirectory
34:	File baseDir = new File(args[0]); 
35:	if (!baseDir.exists() || !baseDir.isDirectory()) { 
-
52:	ConfigurationAssembler assembler = new ConfigurationAssembler(baseDir, templateFile, "server", subsystemsFile, outputFile); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: delete, deleteOnExit, exists, getName
45:	super(model, persister); 
-
63:	configurationFile.commitTempFile(tempFileName); 
-
69:	if (tempFileName.exists() && !tempFileName.delete()) { 
70:	MGMT_OP_LOGGER.cannotDeleteTempFile(tempFileName.getName()); 
71:	tempFileName.deleteOnExit(); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: delete, deleteOnExit, exists
101:	if (file.exists()) { 
102:	if (!file.delete() && file.exists()) { 
103:	file.deleteOnExit(); 
104:	throw new IllegalStateException(MESSAGES.couldNotDeleteFile(file)); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: exists, isDirectory
34:	File baseDir = new File(args[0]); 
35:	if (!baseDir.exists() || !baseDir.isDirectory()) { 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods:
42:	return new File(location, name); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods:
38:	File path = new File(desc.filenameFor(Component.PRIMARY_INDEX)); 
39:	in = RandomAccessReader.open(path); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: exists, getAbsolutePath, getParentFile, mkdirs
86:	File file = new File(outPath, outFile); 
87:	System.out.println("Generating " + file.getAbsolutePath()); 
-
89:	File parent = file.getParentFile(); 
90:	if (!parent.exists()) { 
91:	parent.mkdirs(); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: exists, getAbsolutePath, isDirectory, mkdir
44:	private static final File DIR = new File(new File("target"), 
-
48:	if (DIR.exists() && !DIR.isDirectory()) 
49:	throw new IllegalStateException(DIR.getAbsolutePath() 
-
51:	DIR.mkdir(); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: canWrite, equals, exists, renameTo, toString
146:	file=new File(dir, 
-
152:	if (file.exists()&&!file.canWrite()) 
-
156:	if (out==null || !file.equals(_file)) 
-
160:	if (!_append && file.exists()) 
161:	file.renameTo(new File(file.toString()+"."+_fileBackupFormat.format(now))); 
-
163:	out=new FileOutputStream(file.toString(),_append); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: getPath, mkdirs
51:	file.mkdirs(); 
-
53:	AtomicFile aFile = new AtomicFile(file); 
-
58:	Log.e(TAG, "Cannot write file " + file.getPath()); 
-
103:	writeFile(new File(getContext().getFilesDir(), "system/packages-stopped.xml"), 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: exists
45:	 
-
56:	if (tempFileName.exists() && !tempFileName.delete()) { 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: delete, toURI
97:	File wsdlFile = new File(this.getClass().getSimpleName() + ".wsdl"); 
98:	TestNoAddressingTestCase.downloadWSDLToFile(wsdlURL, wsdlFile); 
-
100:	Service service = Service.create(wsdlFile.toURI().toURL(), serviceName); 
-
104:	wsdlFile.delete(); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: exists
47:	if(violatesChroot(toCreate)) { 
-
51:	if(toCreate.exists()) { 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: exists
70:	store = new File(name); 
-
74:	if (!store.exists()) { 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: delete, exists, getName
74:	File reportFile = new File(location); 
75:	Assert.assertTrue("JDR report missing, not located at " + location, reportFile.exists()); 
-
79:	reportFile.delete(); 
-
83:	String reportName = reportFile.getName().replace(".zip",""); 
-
90:	throw new RuntimeException("Unable to validate JDR report: " + reportFile.getName(), e); 
-
96:	throw new RuntimeException("Unable to close JDR report: " + reportFile.getName(), e); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: exists, mkdir
87:	deployDir = new File(tempDir + File.separator + "tempDeployments"); 
88:	if (deployDir.exists()) { 
89:	FileUtils.deleteDirectory(deployDir); 
-
91:	assertTrue("Unable to create deployment scanner directory.", deployDir.mkdir()); 
Full Snippet Info
8
CodeRank
This code example shows how to use the following methods:
38:	long length = new File(path).length(); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: exists, isDirectory
83:	File libraryDir = new File(libraryPath); 
84:	if (libraryDir.exists()) { 
85:	if (!libraryDir.isDirectory()) { 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods:
48:	archive.addAsResource(new File(resourceURL.getFile()), path); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: exists
84:	File file = new File(baseDir, deploymentName); 
85:	if (!file.exists()) { 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: delete, toURI
103:	File wsdlFile = new File(this.getClass().getSimpleName() + ".wsdl"); 
104:	TestNoAddressingTestCase.downloadWSDLToFile(wsdlURL, wsdlFile); 
-
106:	Service service = Service.create(wsdlFile.toURI().toURL(), serviceName); 
-
110:	wsdlFile.delete(); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: exists, mkdir
73:	deployDir = new File(tempDir, "tempDeployment"); 
74:	if (deployDir.exists()) { 
75:	FileUtils.deleteDirectory(deployDir); 
-
77:	Assert.assertTrue("Unable to create deployment scanner directory.", deployDir.mkdir()); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: exists, getAbsolutePath
144:	File jpsExe = new File(jreHome, "bin/jps.exe"); 
145:	if (!jpsExe.exists()) { 
-
148:	if(jpsExe.exists()) { 
149:	return String.format("%s -vl", jpsExe.getAbsolutePath()); 
Full Snippet Info
8
CodeRank
This code example shows how to use the following methods: exists, mkdir
64:	deployDir = new File(tempDir + File.separator + "tempDeployment"); 
65:	if (deployDir.exists()) { 
66:	FileUtils.deleteDirectory(deployDir); 
-
68:	assertTrue("Unable to create deployment scanner directory.", deployDir.mkdir()); 
Full Snippet Info
8
CodeRank
This code example shows how to use the following methods: exists, getAbsolutePath, isDirectory
69:	File file = new File(targetOldVersions, version.getExpandedDirectoryName()); 
70:	if (!file.exists() || !file.isDirectory()) { 
71:	throw new IllegalStateException("Could not find " + file.getAbsolutePath()); 
Full Snippet Info
8
CodeRank
This code example shows how to use the following methods:
55:	f.renameTo(new File("/sdcard" + file)); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods:
49:	archive.addAsResource(new File(resourceURL.getFile()), path); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: exists, isDirectory
166:	File moduleDir = new File(modulePath); 
167:	if (!moduleDir.exists()) { 
-
170:	if (!moduleDir.isDirectory()) { 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: exists
76:	File f=new File(getContext().getFilesDir(), uri.getPath()); 
-
78:	if (f.exists()) { 
Full Snippet Info
8
CodeRank
This code example shows how to use the following methods:
67:	return FileBitmapTextureAtlasSource.create(new File(FileUtils.getAbsolutePathOnInternalStorage(pContext, pFilePath)), pTextureX, pTextureY); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods:
50:	archive.addAsResource(new File(resourceFilePath), path); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods: exists
118:	final File testModuleRoot = new File(getModulePath(), MODULE_NAME.replace('.', File.separatorChar)); 
119:	assertFalse("Module is already deployed at " + testModuleRoot, testModuleRoot.exists()); 
-
129:	assertTrue("Module deployment failed! Module dir does not exist: " + testModuleRoot, testModuleRoot.exists()); 
Full Snippet Info
8
CodeRank
This code example shows how to use the following methods:
150:	if(new File(name).isAbsolute()) { 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods:
83:	FileResourceManager resourceManager = new FileResourceManager(new File(path), 1024 * 1024); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods:
62:	this.counterFile = new File(fileName); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods:
60:	jarFile = new File(TestSuiteEnvironment.getTmpDir() + File.separator + "Dummy.jar"); 
61:	new ZipExporterImpl(jar).exportTo(jarFile, true); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods:
87:	cliTestAnotherWar = new File(tempDir + File.separator + war.getName()); 
88:	new ZipExporterImpl(war).exportTo(cliTestAnotherWar, true); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods:
33:	sdk_with_platform_default = new AndroidSdk(new File(env_ANDROID_HOME), "16"); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods:
97:	FileUtil.fullyDelete(new File(BASEDIR)); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods:
34:	final File configFile = new File(args[0]); 
35:	final int discoveryResult = LoadBalancerClientCli.runDiscovery(ZOOKEEPER_HOSTNAME + ":" + ZOOKEEPER_PORT, 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods:
91:	war1 = new File(tempDir + File.separator + war.getName()); 
92:	new ZipExporterImpl(war).exportTo(war1, true); 
Full Snippet Info
9
CodeRank
This code example shows how to use the following methods:
379:	File source = new File(srcPath); 
-
381:	Utils.copyRecursively(new Utils.FileSource(), source, target, true); 
Full Snippet Info
9
CodeRank
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: