I would like to just ask, how would one be able to loop through YAML files to find the needed data?
My situation: I have a Spigot/Bukkit server, and it has a folder filled with lots of files. What I'd need to do, is go through each of these files separately in the plugin to find which file contains the data I need. How could I achieve this?
You can loop through Files by using:
YamlConfiguration config = new YamlConfiguration();
File[] files = this.getDataFolder().listFiles();
for(File file : files){
try {
config.load(file);
if(config.contains("Path")){
//What you need to do.
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidConfigurationException e) {
e.printStackTrace();
}
}
loading them and then cycling through loading them, and then checking if they contain the path you need. However, you may need to specify a folder, to do that simply do a statement within the for loop like:
file = new File(file.getAbsolutePath() + File.separator + "FOLDER_NAME");
But really that last part is incase you have other types of files. You can end up getting an exception if you aren't careful. In general what you are doing isn't generally necessary and there is most likely a much better solution. Just answering your question though.
Related
I've seen this question, it's not a duplicate.
I'm pretty new to Android-Development, so my way to store personal data on the device might be not the best one.
However, when I update the App, the stored files aren't found anymore.
File path = myContext.getFilesDir();
File pathUse = new File(path, "lists.ser");
if(!pathUse.exists()) {
try {
pathUse.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try(ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(pathUse))) {
os.writeObject(myObject);
} catch(Exception e) {
e.printStackTrace();
}
How can I store the data that way, that it is kept after an update?
Internal files are removed only if you uninstall and reinstall. An upgrade will not remove internal files -- and it shouldn't. Consider that if it did that, your database would be removed for every upgrade as well.
Check the official documentation for more Info Click here
Hence storing the application specific data in external storage is not recommended because anyone can access it.
I am trying to delete a file and then recreate it. First I check to see if the file already exists, then, if it does, I delete it. Then I try to create a new file in the same place with the same name. When I do this I get this error:
java.nio.file.AccessDeniedException: inputLog.txt
However, if the file did not exist before running these three operations, then the file is created without issue.
Here is my code:
final Path INPUTLOGPATH = FileSystems.getDefault().getPath("inputLog.txt");
try {
reader = Files.newBufferedReader(INPUTLOGPATH, charset);
} catch (IOException e) {
reader = null;
}
if (reader != null) {
try {
Files.delete(INPUTLOGPATH);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Files.createFile(INPUTLOGPATH);
} catch (IOException e) {
e.printStackTrace();
}
First I check to see if the file already exists, then, if it does, I delete it.
Why? Opening the file for output will already do all that. You're just repeating work that the operating system already has to do. Remove all this. You're doing it wrong by not closing the file reader, but it's irrelevant. Don't write unnecessary code.
Then I try to create a new file in the same place with the same name
That is also unnecessary as shown. Just open the file for output when you need it.
As you have it now:
you're opening the file, which is a search, among many other things
you're deleting the file, which is another search
you're creating the file, which is another search
then presumably you're opening the file for output, which requires another search, another deletion, and another creation, internally to the operating system.
Don't do this. Just remove all this code. It accomplishes exactly nothing.
You're also introducing all kinds of timing-window problems by this approach, and you still have to deal with eventual failure at the point where you actually open the file for output.
I want to move files (images) from a folder to another:
For example:
/home/folder1/image.png
to
/home/folder1/folder2/image.png
And obviously remove the image from the folder1
I've trying to do it by reading the path and then modifying it, or using renameTo, but i can't do it.
I hope someone can help me a little with this, Thanks.
EDIT:
Well I can put the code but it's simple to explain what i did:
I just created a Folder class that has a File object of my folder (/home/folder1) , i read all the images inside and save it in an File array, then i scan it and try to change the path of every image file String to another
EDIT:
Thanks to all for the help, all are good examples, I was able to change my files to another location, there was a bunch of files I wanted to move so, I didn't want to create too many objects.
You said you tried renameTo and it didn't work, but this worked for me. After I renamed it I deleted the original file.
File a = new File("C:\\folderA\\A.txt");
a.renameTo(new File("C:\\folderB\\" + a.getName()));
a.delete();
In java 8+ you can simply use Files.move from nio:
try {
Path source = Paths.get("/home/folder1/image.png");
Path dest = Paths.get("/home/folder1/folder2/image.png");
Files.move(source, dest);
} catch (IOException e) {
...
}
The paths can even come from different file system providers (ie a ZipFileSystem).
Commons-io has a few methods in the FileUtils class that can help you.
http://commons.apache.org/proper/commons-io/javadocs/api-release/index.html?org/apache/commons/io/package-summary.html
Example:
FileUtils.moveFile(src, dest);
The usual approach to solving this is copying the file and then deleting it from the original location, but you can follow this tutorial for more information. Also, the platform(linux, windows, is not important).
I didn't run this, but it should work
File f1 = new File("/home/folder1/image.png");
File f2 = new File("/home/folder1/folder2/image.png");
f1.renameTo(f2);
There are many approaches for you to do that.
This snippet is one of them, you can move your files like this way:
try {
final File myFile = new File("C:\\folder1\\myfile.txt");
if(myFile.renameTo(new File("C:\\folder2\\" + myFile.getName()))) {
System.out.println("File is moved successful!");
} else {
System.out.println("File is failed to move!");
}
}catch(Exception e){
e.printStackTrace();
}
I hava a tomcat java web application. I want to create a file to store some json data in it. How do I create this file and where will this file be created?
public void insert(UserProfile profile) {
JSONObject jsUser = profile.asJSONObject();
try {
String path = profile.getUsername()+".json";
FileWriter fileWriter = new FileWriter(path);
fileWriter.write(jsUser.toJSONString());
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
How do I create this file ...
Use the standard Java I/O libraries to create and write the file; e.g. you could use a FileWriter.
... and where will this file be created?
It is entirely up to you to decide where you want the file to be created.
However, you need to beware of the possibility that someone might trash your web service by causing it to fill up the file system ... or by causing it to write stuff on top of some (important) existing file. You need a good strategy for dealing with these concerns, but that will depend on the purpose of these files, and the circumstances under which they are written.
I've been having problems exporting my java project to a jar (from Eclipse). There is a file that I've included in the jar named images. It contains all of the image files used by my project. The problem is, my references to those images only work when the project isn't in jar form. I can't figure out why and I'm wondering if I need to change the wording of the image references. This is what I'm doing to reference images:
URL treeURL = null;
Image tree = null;
File pathToTheTree = new File("images/tree.png");
try {
treeURL = pathToTheTree.toURL();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
tree = ImageIO.read(treeURL);
} catch(IOException e) {
e.printStackTrace();
}
Most likely it is a simple problem, as I'm a beginner at coding. What do I need to change to make these references work when it's all in a jar?
It is indeed simple: you use the various getResource() methods in java.lang.Class and java.lang.ClassLoader. For example, in your app, you could just write
treeURL = getClass().getResource("/images/tree.png");
This would find the file in an images directory at the root of the jar file. The nice thing about the getResource() methods is that they work whether the files are in a jar or not -- if the images directory is a real directory on disk, this will still work (as long as the parent of images is part of your class path.)