I am using:
// File (or directory) to be moved
File file = new File(output.toString());
// Destination directory
File dir = new File(directory_name);
// Move file to new directory
boolean success = file.renameTo(new File(dir, new_file.getName()));
if (!success) {
// File was not successfully moved
}
In this case file is main.vm, and folder is seven
the program shows that it works(the file exists and all) but the file is not moving to the seven directory.
Any ideas why?
Is it ok that the file name is main.vm or do i need to enter full path? the same for the folder.
Thanks
Maybe you wanna take a look at the Apache Commons FileUtils
Works for me. (run with java -ea opt.)
File f = new File("foo.mv");
if(!f.exists())
assert f.createNewFile() : "failed to create foo.mv";
File folder = new File("7");
if(!folder.exists())
assert folder.mkdir() : "failed to create new directory";
File fnew = new File(folder, f.getName());
assert !fnew.exists() : "fnew already exists";
f.renameTo(fnew);
assert fnew.exists() : "fnew does not exist -- move failed";
System.out.format("moved %s to %s\n",f, fnew);
Try to do following steps:
check if you move the file inside same filesystem - otherwise it will fail;
create destination directory;
define "new_file" variable.
You need to enter full path of the file, not only the filename. And would be nice if you'll show up your full source code in the future, for better understanding/answers.
Related
The project name is 'producer'. I have a file located in project folder C:/Users/Documents/producer/krb5.conf.
If I want to write its relative path, should I write
File file = new File("krb5.conf");
or
File file = new File("producer/krb5.conf");
or
File file = new File("./krb5.conf");
?
You can use both your 1. and 3. option.
The 2. option would refer to C:/Users/Documents/producer/producer/krb5.conf.
For the purpose of testing you could try to get the absolute path from each file and print it.
// 1.
File file1 = new File("krb5.conf");
File file2 = new File("producer/krb5.conf");
File file3 = new File("./krb5.conf");
System.out.println(file1.getAbsolutePath());
// Output: C:\Users\Documents\producer\krb5.conf
System.out.println(file2.getAbsolutePath());
// Output: C:\Users\Documents\producer\producer\krb5.conf
System.out.println(file3.getAbsolutePath());
// Output: C:\Users\Documents\producer\.\krb5.conf
The 3. path may look a bit weird at first, but it also works.
C:\Users\Documents\producer\. points to the current directory, so it is essentially the same as C:\Users\Documents\producer.
currently I'm working on a project and I have to change the savepath of my application. So I will firstly check if the directory exists using
File file = new File(path);
file.exists();
My problem is that the method file.exists() returns false even when I try to input C: as my path. Nevertheless, if I don't specify any folder, let say :
File file = new File("testFile.xml");
Then the new file will be created in the main directory. I suspect Eclipse automatically adds a relative path everytime I do the check since when I use text editor, the following returns true
new File("C:").exists()
Now, is there any way to tell Eclipse to recognise the path that I enter as an absolute path?
Thanks!
EDITED ****
I found that my problem is that Eclipse seems to auto append every file path that I create with the source directory
File = new File("C:/")
will give me
"C:\Users\Christopher\Documents\School Stuff\CS2103\JOBS\main\C:\"
which is automatically appended by eclipse with the project directory and hence, disabling me from creating file outside of my project directory
Could you try file.getAbsoluteFile().exists() ?
File.isAbsolute():
File file = new File(path);
if (file.isAbsolute()) {
}
in Eclipse, right click on project and go to run> run configuration and go to arguments give the default path for saving file.... project always create file on that location.
File fileTest = new File("C:/test");
if (!fileTest.exists()) {
if (fileTest.mkdirs()) {
fileTest.setReadable(true, false);
fileTest.setWritable(true, false);
} else {
System.out.println("Failed To Create Directories! :-"+ "C:/");
}
}
So with the code below it creates the file in the folder
File f = new File(path);
if(!f.exists())
f.mkdirs();
, but i only want to create the directory, because after this i use this code
file.transferTo(new File(path));
which saves a Multipart file to the same location, but it throws and error because there is already a file. Is there a way only to create the folder without the file ? One solution is to delete the first file, but looking for better solution
EDIT:
File f = new File(path);
this line creates the folders and the file, it shouldn't. I use java 8 and IntelliJ 14
SOLUTION:
The problem was Intellij or Intellij debug watches. After restarting it and clearing watches which were like:
new File(path)
file.transferTo(new File(path))
f.exists()
The code started working.
It should be
f.getParentFile().mkdirs();
You don't need to check for existence beforehand: mkdirs() already does that.
File dir = new File("<Your_Path>/TestDirectory");
// attempt to create the directory here
boolean successful = dir.mkdir();
if (successful)
{
// creating the directory succeeded
System.out.println("directory was created successfully");
}
else
{
// creating the directory failed
System.out.println("failed trying to create the directory");
}
You can create your files inside your directory path from then on....
I want to make a program that you can email to someone and they can run it.
Right now my code for making a file is like this:
File f = new File("/Users/S0urceC0ded/Desktop/Code/project/JavaStuffs/src/axmlfile.xml);
f.createNewFile();
But what if someones username is not S0urceC0ded, or they put the project in a different place? How could I set the file path to the src folder plus the filename?
Leave the path off entirely, it will use the directory of the project.
Change
File f = new File("/Users/S0urceC0ded/Desktop/Code/project/JavaStuffs/src/axmlfile.xml");
To
File f = new File("axmlfile.xml");
I generally use code like this for temporary file storage, this way it gets cleaned up when the application finishes. If required you can allow the user to save a version of the file or move it to a permanent location.
try{
//create a temporary file
File temp = File.createTempFile("axmlfile", ".xml");
System.out.println("Location: " + temp.getAbsolutePath());
}catch(IOException e){
e.printStackTrace();
}
I've a .txt file ("file.txt") in my netbeans "/build/classes" directory.
In the same directory there is the .class file compiled for the following code:
try {
File f = new File("file.txt");
Scanner sc = new Scanner(f);
}
catch (IOException e) {
System.out.println(e);
}
Debugging the code (breakpoint in "Scanner sc ..") an exception is launched and the following is printed:
java.io.FileNotFoundException: file.txt (the system can't find the
specified file)
I also tried using "/file.txt" and "//file.txt" but same result.
Thank you in advance for any hint
If you just use new File("pathtofile") that path is relative to your current working directory, which is not at all necessarily where your class files are.
If you are sure that the file is somewhere on your classpath, you could use the following pattern instead:
URL path = ClassLoader.getSystemResource("file.txt");
if(path==null) {
//The file was not found, insert error handling here
}
File f = new File(path.toURI());
The JVM will look for the file in the current working directory.
Where this is depends on your IDE settings (how your program is executed).
To figure out where it expects file.txt to be located, you could do
System.out.println(new File("."));
If it for instance outputs
/some/path/project/build
you should place file.txt in the build directory (or specify the proper path relative to the build directory).
Try:
File f = new File("./build/classes/file.txt");
Use "." to denote the current directory
String path = "./build/classes/file.txt";
File f = new File(path);
File Object loads, looking for match in its current directory.... which is Directly in Your project folder where your class files are loaded not in your source ..... put the file directly in the project folder