I need to be able to build all directories up to and including the directory specified by my File object. For example, suppose I have something like this:
File file = new File( "/var/a/b/c/d/" );
But only /var/ exists. I need a method that builds up to d, and I was wondering if there was a method in a java io library somewhere that does this already.
mkdirs() in java.io.File does the trick.
File file = new File("/var/a/b/c/d");
file.mkdirs();
File.mkdirs()
Related
I want to create a temporary file (that goes away when the application closes) with a specific name. I'm using this code:
f = File.createTempFile("tmp", ".txt", new File("D:/"));
This creates something like D:\tmp4501156806082176909.txt. I want just D:\tmp.txt. How can I do this?
In this case, don't use createTempFile. The point of createTempFile is to generate the "garbage" name in order to avoid name colisions.
You should use File.createNewFile() or simply write to the file. Whichever is more appropriate for your use case. You can then call File.deleteOnExit() to get the VM to look after cleaning up the file.
If you want to create just tmp.txt, then just create the file using createNewFile(), instead of createTempFile(). createTempFile is used to create temporary files that should not have the same name when created over and over.
Also have a look at this post which shows a very simple way to create files.
Taken the post mentioned above:
String path = "C:"+File.separator+"hello"+File.separator+"hi.txt";
//(use relative path for Unix systems)
File f = new File(path);
//(works for both Windows and Linux)
f.mkdirs();
f.createNewFile();
try regex
fileName = fileName.replaceAll("\\d", "");
Assume final String fname = "/dir1/dir2/fname.ext". I do not wish to parse the string recursively in order to create the directories if they do not exist, and only then write to a file. I wish to use the given string, fname, for creating the directories and file if each of which does not exist.
This is the code you are looking for:
File myFile = new File("/dir1/dir2/fname.ext");
myFile.getParentFile().mkdirs();
// do your writing being sure the parent directories exist.
You can use mkdirs to create the path.
File f = new File("/dir1/dir2/fname.ext");
f.getParentFile().mkdirs();
And then work on the file itself.
Currently, my plugin creates a java file in my project(IProject). But I want that java file within a specified Package. How to do it.
IFile sampleFile = parentFolder.getFile("Sample.java");
if(!sampleFile.exists()) FileInputStream fileStream = new FileInputStream("C:\Users\Uma\Desktop\treasureHunt\Application.java"); sampleFile.create(fileStream, false, null);
This is my current piece of code.
How can I create the sampleFile within a package. For example: in package com.mdh.se as com.mdh.se.Sample.java
If you have a "package" (e.g. "com.mdh.se") then you'll have a corresponding subdirectory (for example, "c:\users\uma\desktop\treasurehunt\com\mdh\se"). Simply write your file there.
I think, that the only thing you need to do is to create folders representing your package structure. So your path should look like C:\Users\Uma\Desktop\treasureHunt\com\mdh\se\Sample.java for your example.
You can get a special file inside the package by calling
java.net.URL imgURL = ResourceManager.class.getResource( "ResourceManager.class" );
From these URL you can extract the directory, the file is placed.
A new file you can create with
new File(directory,filename);
Given a File object how can I create the path for saving it?
I tried file.mkdirs() but for example if the file's path is:
/mnt/sdcard/downloads/myapp/temp/song.mp3
it also creates a folder named "song.mp3" inside temp.
How can I do it correctly?
use this code
File myDir=new File("/sdcard/Download");
myDir.mkdirs();
String fname = "Image.jpg";
File file = new File (myDir,fname);
Just try:
file.getParentFile().mkdirs();
this will create the parent directory.
If I have understand correctly what you need is
File.getParent()
hope it helps
If you just want to extract the path you can use lastIndexOf:
String p = "/mnt/sdcard/downloads/myapp/temp/song.mp3";
System.out.println(p.substring(0,p.lastIndexOf('/')));
Of course, if you already have File object then getParent(), as suggested, will be easier.
I try to write to a Csv file via:
mFileWriter = new FileWriter(
"/sdcard/program/file");
mCsvWriter = new CSVWriter(mFileWriter);
At the moment it throws an exception that the file doesn't exist.
It's true that the file doesn't exist. What's the easiest way to create the file?
Does the FILE not exist, or the DIRECTORY it's supposed to go into?
If you want to create a directory structure, you can always do
File file = new File("/full/path/to/file");
file.mkdirs();
This will create any path leading up to this file that doesn't exist yet.
I suppose the missing quotes around your file name are a typo?