I am trying to copy and paste a Minecraft World from a template into the server folder and then load the world. I am getting this error:
java.io.FileNotFoundException:
.\C:\Users\Archie\Desktop\Server.b571c7a6-3297-48eb-ac24-1bac65ef9727\session.lock
(The filename, directory name, or volume label syntax is incorrect)
So here is what I tried:
Printing out the source of the path and then the place I am copying it to, to console to check it is the correct path, here is what was printed:
C:\Users\Archie\Desktop\Server.b571c7a6-3297-48eb-ac24-1bac65ef9727 (dir to copy to)
C:\Users\Archie\Desktop\Server\plugins\Solus\gameworld (src)
That is correct. And in that folder the session.lock is literally there:
http://prntscr.com/klc7xp
So I am really confused as to why it is throwing a file not found exception, I've googled it but there doesn't really seem to be a fix.
Here is the code:
private void loadWorld() {
File game = new File(Solus.get().getServer().getWorldContainer().getAbsolutePath() + uuid.toString());
if (!game.mkdir()) {
System.out.println("Couldn't generate the game: " + uuid.toString());
}
File srcDir = new File(Solus.get().getDataFolder().getAbsolutePath() + File.separator + "gameworld");
System.out.println(game.getPath() + " : " + srcDir.getAbsolutePath());
try {
FileUtils.copyDirectory(srcDir, game);
} catch (IOException e) {
e.printStackTrace();
}
WorldCreator wc = new WorldCreator(game.getAbsolutePath());
wc.createWorld();
this.world = Bukkit.getServer().createWorld(wc);
}
Code explanation:
Creates the folder to store the world data inside.
Creates the folder.
Finds the gameworld file that needs to be copied and replicated.
Copies the folder into the directory created earlier.
( Spigot ) Loads the world.
Related
My goal is to move a file from one directory to another. The source is on a local drive and the destination is on a network drive. It doesn't matter whether I move or I copy then delete source. The file is approx 6GB.
What I've tried:
// C:\path\to\dir\file.bak
File source = new File(localRoot + backup);
// \\192.168.1.100\path\to\dir\file.bak
File dest = new File(storageRoot + "/" + storagePath + "/" + backup);
try {
log("Copying");
// I've tried copyFile as well.
FileUtils.copyFileToDirectory(source, dest);
log("copied");
} catch (Exception e) {
e.printStackTrace();
}
File source = new File(localRoot + backup);
File dest = new File(storageRoot + "/" + storagePath + "/" + backup);
try {
log("Copying");
// I've tried move and creating Paths instead of Files as well.
Files.copy(source.toPath(), dest.toPath());
log("copied")
} catch (Exception e) {
e.printStackTrace();
}
I've tried as well a manual method using Input, OutputStreams and reading bytes.
The results is that a file is created in the destination with the correct filename with 0 bytes, and the source file is rewritten from 6GB to 0 bytes. This happens for all methods I've tried, the only exception is that when I tried move, the source file was deleted rather than rewritten.
All code is in early development, please refrain from commenting on best practices.
Thank you, and what am I missing or what else can I try?
I have following pieces of code:
if (e.getSource() == theView.addButton) {
System.out.println("Add Button clicked");
theView.setBotTextArea("Adding category...");
File directory = new File(theModel.getDirectory() + theView.getCategoryNameInput());
boolean isDirectoryCreated = directory.mkdir();
if(isDirectoryCreated) {
System.out.println("Created new directory in: " + directory);
} else if (directory.exists()) {
System.out.println("Category already exists!");
}
}
This is part of the ActionListener's ActionPerformed() method.
private File directory = new File("C:/Users/Lotix/Desktop/TestFolder/");
public File getDirectory() {
return directory;
}
What i expect this method to do is to create a subfolder in the chosen directory. However, for some reason unknown to me, it creates completly another folder on my desktop instead of TestFolder.
I tried theModel.getDirectory().toString() and manipulating the variable but to no avail. The solution i came up with is to simply add forward slash between
theModel.getDirectory() and theView.getCategoryNameInput() such as this:
File directory = new File(theModel.getDirectory() + "/" + theView.getCategoryNameInput());
However, when i concatenate File variable with another String it works perfectly fine.
What gives?
Any file you create in your own Desktop directory appears on your desktop. That's what the folder is for.
This doesnt have anything to do with 'interaction with String and File', or Java.
Path to = Paths.get("C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission15\\OO Java Bootcamp - Java and NetBeans - Copy\\test\\oojavabootcamp\\OOJavaBootcampTestMasase - Copy");
Path from = Paths.get("C:\\submissions\\SOFT222\\Assessment1\\OOJavaBootcampTest - Copy");
void Paths() throws IOException {
File submissionFolder = new File("C:\\submissions\\SOFT222\\Assessment1\\10403435\\Submission15\\OO Java Bootcamp - Java and NetBeans - Copy");
if (submissionFolder.exists()) {
System.out.println("Folder exists");
try {
// Remove test files
Files.deleteIfExists(Paths.get(to + ".java"));
Files.copy(Paths.get(from + ".java"), Paths.get(to + ".java"));
} catch (IOException ex) {
System.out.println("Original file copy did not work.");
}
}
}
This code deletes the .java file but doesn't seem to copy the .java file(OOJavaBootcampTest - Copy) to the 'to' variable cos the name remains the same after deletion has occurred. I think its deleting it and making a copy of it instead of replacing it with the .java in from variable. Please what am I doing wrong
You don't tell what the path is which raises the error, so it's hard to tell.
Note that you can:
Files.deleteIfExists(somePath);
so you may want to use this instead.
It's hard to tell what you really want so here I'll just assume that you want to overwrite to with from; in this case, do:
final Path parentDir = to.getParent();
Files.createDirectories(parentDir);
Files.copy(from, to, StandardCopyOption.REPLACE_EXISTING);
So i'm working on a simple Windows Explorer replacement. I want to add the ability to create Folders and Files. For some reason, it only works when i'm in my root or c:/ folder, but as soon as it's somewhere else (for example C:\Program Files (x86)) it doesn't work. I either get a java.io.IOException: Access Denied when i create a File and when i try to create a folder, no Exception comes up, but no folder is created.
This is my code for a new file:
String location = getPath();
String name = JOptionPane.showInputDialog("Fill in the name of the new file. \nDon't forget to add file type (.txt, .pdf).", null);
if(name == null){
}
else {
File newFile = new File(location + "\\" + name);
boolean flag = false;
try {
flag = newFile.createNewFile();
} catch (IOException Io) {
JFrame messageDialog = new JFrame("Error!");
JOptionPane.showMessageDialog(messageDialog, "File creation failed with the following reason: \n" + Io);
}
}
This is my code for a new Folder:
String location = getPath();
String name = JOptionPane.showInputDialog("Fill in the name of the new folder.", null);
if(name == null){
}
else {
File newFolder = new File(location + "\\" + name);
boolean flag = false;
try {
flag = newFolder.mkdir();
} catch (SecurityException Se) {
JFrame messageDialog = new JFrame("Error!");
JOptionPane.showMessageDialog(messageDialog, "Folder creation failed with the following reason: \n" + Se);
}
}
I'm stuck right now and i have no idea what i'm doing wrong to get rid of the access denied error.
Short explenation of how this program works:
My program shows a list of all folders and files from a selected File.
That File is a field in the class JXploreFile called "currentFile", which behaves almost the same as a File.
When browsing through the folders, the currentFile is set to a new JXploreFile, containing the new folder you are in as File.
When creating a new folder/file, my program ask the path the user is currently browsing in with the method getPath().
Thanks for the help!
Image of my program:
Before you try to make any I/O operation just check if you have the permission
go to the parent directory (your case location)
then do something like
File f = new File(location);
if(f.canWrite()) {
/*your full folder creation code here */
} else {
}
try to put
String location ="c:\\user\<<youruser>>\\my documents"
or a folder with full perission to write
I am trying to recursively iterate through the entire root directory that I arrive at after login to the FTP server.
I am able to connect, all I really want to do from there is recurse through the entire structure and and download each file and folder and have it in the same structure as it is on the FTP. What I have so far is a working download method, it goes to the server and gets my entire structure of files, which is brilliant, except it fails on the first attempt, then works the second time around. The error I get is as follows:
java.io.FileNotFoundException: output-directory\test\testFile.png
(The system cannot find the path specified)
I managed to do upload functionality of a directory that I have locally, but can't quite get downloading to work, after numerous attempts I really need some help.
public static void download(String filename, String base)
{
File basedir = new File(base);
basedir.mkdirs();
try
{
FTPFile[] ftpFiles = ftpClient.listFiles();
for (FTPFile file : ftpFiles)
{
if (!file.getName().equals(".") && !file.getName().equals("..")) {
// If Dealing with a directory, change to it and call the function again
if (file.isDirectory())
{
// Change working Directory to this directory.
ftpClient.changeWorkingDirectory(file.getName());
// Recursive call to this method.
download(ftpClient.printWorkingDirectory(), base);
// Create the directory locally - in the right place
File newDir = new File (base + "/" + ftpClient.printWorkingDirectory());
newDir.mkdirs();
// Come back out to the parent level.
ftpClient.changeToParentDirectory();
}
else
{
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
String remoteFile1 = ftpClient.printWorkingDirectory() + "/" + file.getName();
File downloadFile1 = new File(base + "/" + ftpClient.printWorkingDirectory() + "/" + file.getName());
OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
outputStream1.close();
}
}
}
}
catch(IOException ex)
{
System.out.println(ex);
}
}
Your problem (well, your current problem after we got rid of the . and .. and you got past the binary issue) is that you are doing the recursion step before calling newDir.mkdirs().
So suppose you have a tree like
.
..
someDir
.
..
someFile.txt
someOtherDir
.
..
someOtherFile.png
What you do is skip the dot files, see that someDir is a directory, then immediately go inside it, skip its dot files, and see someFile.txt, and process it. You have not created someDir locally as yet, so you get an exception.
Your exception handler does not stop execution, so control goes back to the upper level of the recursion. At this point it creates the directory.
So next time you run your program, the local someDir directory is already created from the previous run, and you see no problem.
Basically, you should change your code to:
if (file.isDirectory())
{
// Change working Directory to this directory.
ftpClient.changeWorkingDirectory(file.getName());
// Create the directory locally - in the right place
File newDir = new File (base + "/" + ftpClient.printWorkingDirectory());
newDir.mkdirs();
// Recursive call to this method.
download(ftpClient.printWorkingDirectory(), base);
// Come back out to the parent level.
ftpClient.changeToParentDirectory();
}
A complete standalone code to download all files recursively from an FTP folder:
private static void downloadFolder(
FTPClient ftpClient, String remotePath, String localPath) throws IOException
{
System.out.println("Downloading folder " + remotePath + " to " + localPath);
FTPFile[] remoteFiles = ftpClient.listFiles(remotePath);
for (FTPFile remoteFile : remoteFiles)
{
if (!remoteFile.getName().equals(".") && !remoteFile.getName().equals(".."))
{
String remoteFilePath = remotePath + "/" + remoteFile.getName();
String localFilePath = localPath + "/" + remoteFile.getName();
if (remoteFile.isDirectory())
{
new File(localFilePath).mkdirs();
downloadFolder(ftpClient, remoteFilePath, localFilePath);
}
else
{
System.out.println("Downloading file " + remoteFilePath + " to " +
localFilePath);
OutputStream outputStream =
new BufferedOutputStream(new FileOutputStream(localFilePath));
if (!ftpClient.retrieveFile(remoteFilePath, outputStream))
{
System.out.println("Failed to download file " + remoteFilePath);
}
outputStream.close();
}
}
}
}