So far I have a list of images and I want to rename them based on information I get from a database.
List of images:
IBImages = ["foo1", "foo2", "foo3"]
private static void buildTheme(ArrayList<String> IBImages) {
String bundlesPath = "/a/long/path/with/dest/here";
for (int image = 0; image < IBImages.size(); image++) {
String folder = bundlesPath + "/" + image;
File destFolder = new File(folder);
// Create a new folder with the image name if it doesn't already exist
if (!destFolder.exists()) {
destFolder.mkdirs();
// Copy image here and rename based on a list returned from a database.
}
}
}
The JSON you get from the database might look something like this. I want to rename the one image that I have to all of the names in the list of icon_names
{
"icon_name": [
"Icon-40.png",
"Icon-40#2x.png",
"Icon-40#3x.png",
"Icon-Small.png",
"Icon-Small#2x.png",
]
}
You can't have into directory few files with same name at once. You need to either copy your file once and rename it, or create empty file with new name and copy bits from original file into it. Second approach is quite easy with Files class and its copy(source, target, copyOptions...) method.
Here is simple example of copying one file located in images/source/image.jpg to new files in image/target directory while giving them new names.
String[] newNames = { "foo.jpg", "bar.jpg", "baz.jpg" };
Path source = Paths.get("images/source/image.jpg"); //original file
Path targetDir = Paths.get("images/target");
Files.createDirectories(targetDir);//in case target directory didn't exist
for (String name : newNames) {
Path target = targetDir.resolve(name);// create new path ending with `name` content
System.out.println("copying into " + target);
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
// I decided to replace already existing files with same name
}
Related
I have a simple text file called small_reports.txt that looks like:
report_2021_05_02.csv
report_2021_05_05.csv
report_2021_06_08.csv
report_2021_06_25.csv
report_2021_07_02.csv
This reported is generated with my java code and takes in each of these files from the directory /work/dir1/reports and writes them into the file combined_reports.txt and then places the txt file back into /work/dir1/reports.
My question is, for each line in small_reports.txt, find that same file (line) in /work/dir1/reports and then COPY them to a new directory called /work/dir1/smallreports?
Using Java 8 & NIO (which is really helpful and good) I have tried:
Path source = Paths.get("/work/dir1/reports/combined_reports.txt");
Path target = Paths.get("/work/dir1/smallreports/", "combined_reports.txt");
if (Files.notExists(target) && target != null) {
Files.createDirectories(Paths.get(target.toString()));
}
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
But this is just copying the actual txt file combined_reports.txt into the new directory and not the contents inside like i thought it would.
final String SOURCE_DIR = "/tmp";
final String TARGET_DIR = "/tmp/root/delme";
List<String> csvFileNames = Files.readAllLines(FileSystems.getDefault().getPath("small_reports.txt"), Charset.forName("UTF-8"));
for (String csvFileName : csvFileNames) {
Path source = Paths.get(SOURCE_DIR, csvFileName);
Path target = Paths.get(TARGET_DIR, csvFileName);
if (Files.notExists(target) && target != null) {
Files.createDirectories(Paths.get(target.toString()));
}
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
}
Should do it for you. Obviously change the constants appropriately
How can I set a last modified date of a file using jimfs?
I have smth. like this:
final FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix());
Path rootPath = Files.createDirectories(fileSystem.getPath("root/path/to/directory"));
Path filePath = rootPath.resolve("test1.pdf");
Path anotherFilePath = rootPath.resolve("test2.pdf");
After creating the stuff I then create a directory iterator like:
try (final DirectoryStream<Path> dirStream = Files.newDirectoryStream(rootPath, "*.pdf")) {
final Iterator<Path> pathIterator = dirStream.iterator();
}
After that I iterate over the files and read the last modified file, which I then return:
Path resolveLastModified(Iterator<Path> dirStreamIterator){
long lastModified = Long.MIN_VALUE;
File lastModifiedFile = null;
while (dirStreamIterator.hasNext()) {
File file = new File(dirStreamIterator.next().toString());
final long actualLastModified = file.lastModified();
if (actualLastModified > lastModified) {
lastModifiedFile = file;
lastModified = actualLastModified;
}
}
return lastModifiedFile.toPath();
}
The problem is that both files "test1.pdf" and "test2.pdf" have lastModified being "0" so I actually can't really test the behavior as the method would always return the first file in the directory. I tried doing:
File file = new File(filePath.toString());
file.setLastModified(1);
but the method returns false.
UDPATE
I just saw that File#getLastModified() uses the default file system. This means that the default local file system will be used to read the time stamp. And this means I am not able to create a temp file using Jimfs, read the last modified and then assert the paths of those files. The one will have jimfs:// as uri scheme and the another will have OS dependent scheme.
Jimfs uses the Java 7 file API. It doesn't really mix with the old File API, as File objects are always tied to the default file system. So don't use File.
If you have a Path, you should use the java.nio.file.Files class for most operations on it. In this case, you just need to use
Files.setLastModifiedTime(path, FileTime.fromMillis(millis));
i am newbie in this but here is my point of view if you choose 1 specific FOLDER and you want to extract the last file from it.
public static void main(String args[]) {
//choose a FOLDER
File folderX = new File("/home/andy/Downloads");
//extract all de files from that FOLDER
File[] all_files_from_folderX = folderX.listFiles();
System.out.println("all_files_from_folderXDirectories = " +
Arrays.toString(all_files_from_folderX));
//we gonna need a new file
File a_simple_new_file = new File("");
// set to 0L (1JAN1970)
a_simple_new_file.setLastModified(0L);
//check 1 by 1 if is bigger or no
for (File temp : all_files_from_folderX) {
if (temp.lastModified() > a_simple_new_file.lastModified()) {
a_simple_new_file = temp;
}
//at the end the newest will be printed
System.out.println("a_simple_new_file = "+a_simple_new_file.getPath());
}
}}
I am trying to create a method that searches inside a folder for .png files and returns a String array with the respective path to each file. It must look inside a resource folder placed NOT in the src, but in project.
The following code works when running from within Eclipse:
// Analyzes specified folder and returns a file array
// populated with the .png files found in that folder
private File[] imageReader(String filePath) {
File folder = new File(filePath);
return folder.listFiles (new FilenameFilter() {
public boolean accept(File filePath, String filename)
{ return filename.endsWith(".png"); }
});
}
// Converts the file array into a string array
private String[] listPngFiles(String filePath) {
File[] imagesFileArray = imageReader(filePath); // file array
String[] imagesStringArray = new String[imagesFileArray.length];
for(int i=0; i<imagesFileArray.length; i++) {
imagesStringArray[i] = "" + imagesFileArray[i];
imagesStringArray[i] = imagesStringArray[i].substring(6); // discards "/images" from directory string
}
return imagesStringArray;
}
However it is not working when I run the exported executable JAR file. This is my current project setup:
.
I have tried the following code but it did not work either:
ClassLoader classLoader = getClass().getClassLoader();
File folder = new File(classLoader.getResource(filePath).getFile());
The reason I am doing this is because I want to have a JButton display an icon chosen from one of the sub folders inside the images resource folder. My JButton already has the following code:
.setIcon(new ImageIcon(GameLogic.class.getResource(**insert listPngFiles array element here**)));
Your help on the matter would be greatly appreciated.
You can achieve this by using Reflections, take a look at getResources
I am using the NIO libraries but I am getting a strange error when I try to move files from one directory to another.
String yearNow = new SimpleDateFormat("yyyy").format(
Calendar.getInstance().getTime());
try {
DirectoryStream<Path> curYearStream =
Files.newDirectoryStream(sourceDir, "{" + yearNow + "*}");
//Glob for current year
Path newDir = Paths.get(sourceDir + "//" + yearNow);
if (!Files.exists(newDir) || !Files.isDirectory(newDir)) {
Files.createDirectory(newDir);
//create 2014 directory if it doesn't exist
}
}
Iterate over elements that start with "2014" and move them in the new directory (newDir, which is also called 2014)
for (Path p : curYearStream) {
System.out.println(p); //it prints out exactly the files that I need to move
Files.move(p, newDir); //java.nio.file.FileAlreadyExistsException
}
I get the java.nio.file.FileAlreadyExistsException because my folder (2014) already exists. What I actually want to do is move all the files that start with "2014" INSIDE the 2014 directory.
Better not going back to java.io.File and using NIO instead:
Path sourceDir = Paths.get("c:\\source");
Path destinationDir = Paths.get("c:\\dest");
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(sourceDir)) {
for (Path path : directoryStream) {
System.out.println("copying " + path.toString());
Path d2 = destinationDir.resolve(path.getFileName());
System.out.println("destination File=" + d2);
Files.move(path, d2, REPLACE_EXISTING);
}
} catch (IOException ex) {
ex.printStackTrace();
}
Files.move is not equivalent to the mv command. It won't detect that the destination is a directory and move files into there.
You have to construct the full destination path, file by file. If you want to copy /src/a.txt to /dest/2014/, the destination path needs to be /dest/2014/a.txt.
You may want to do something like this:
File srcFile = new File("/src/a.txt");
File destDir = new File("/dest/2014");
Path src = srcFile.toPath();
Path dest = new File(destDir, srcFile.getName()).toPath(); // "/dest/2014/a.txt"
Continue with #Andrew's answer
If we use only Files.move(src, dst, StandardCopyOption.REPLACE_EXISTING); then it will delete source directory because we only provide a directory path not an absolute path of a particular file. So it will also delete a source directory when operation will be done.
Let's say source path is /opt/src which contains a csv files and destination path is /opt/dst and I want to move all files from src to dst and I'm using Files.move(src, dst, StandardCopyOption.REPLACE_EXISTING); this then it will move all the files to dst but it will delete a src directory after moving all files because we didn't provide an absolute path of a each file for src as well as dst. We should have to provide src path like /opt/src/foo.csv and dst path like /opt/dst/foo.csv then and then it will not delete a source directory.
DirectoryStream used to iterate over the entries in a directory. A directory stream allows for the convenient use of the for-each construct to iterate over a directory. So we get an absolute path for src and we use resolve method for resolving an absolute path for dst.
Please refer DirectoryStream for more information.
Try this code:
public class App
{
public void moveFromSourceToDestination(String sourceName,String destinationName)
{
File mydir = new File(sourceName);
if (mydir.isDirectory())
{
File[] myContent = mydir.listFiles();
for(int i = 0; i < myContent.length; i++)
{
File file1 = myContent[i];
file1.renameTo(new File(destinationName+file1.getName()));
}
}
}
public static void main(String [] args)
{
App app = new App();
String sourceName = "C:\\Users\\SourceFolder";
String destinationName = "C:\\Users\\DestinationFolder\\";
app.moveFromSourceToDestination(sourceName,destinationName);
}
}
Using java.io.File, its as simple as this:
File srcFile = new File(srcDir, fileName);
srcFile.renameTo(new File(destDir, "a.txt"));
I am currently working on a project for school, it is Java based and I am using Eclipse on Linux Mint to write it. The assignment says use the statement String[] filenames = new java.io.File("icons).list(); to create an array of file names.
The problem is I am not sure what to do with this, I have spent the past few hours searching the Internet and my textbook, but to no avail. Does it need to be a separate method?
Below is my guess for the needed code in the model (the project is to make a matching game, with a GUI) the names will have to be converted later on into actual icons, but I am pretty sure I have that part figured out, I just can't seem to get the darn files into the array!!
Thanks in advance,
public String[] list() {
String[] fileNames = new java.io.File("icons").list();
return fileNames;
}
In Java, the File class does not necessary represent an "existing" file on the file system. For example:
File f = new File("some_unknown_unexisting_file.bob");
System.out.println(f.exists()); // most likely will print 'false'
Also, the class resolves the file from the current working directory. You may get this directory with
System.out.println(new File(".").getAbsolutePath());
In your case, if you can, I would suggest getting a File[] array with :
File[] files = new File("icons").listFiles(new FileFilter() {
#Override
public boolean accept(File f) {
return !f.isDirectory() && f.canRead();
}
});
for (File f : files) {
System.out.println(f.getAbsolutePath());
}
which will return an array of File objects which are not folders and that you can open for reading (note that this is not always true, but is just fine in your case).
But if you have to use list(), then this is equivalent :
File parent = new File("icons");
String[] fileStr = parent.list(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
File f = new File(dir, name);
return !f.isDirectory() && f.canRead();
}
});
for (String f : fileStr) {
System.out.println(new File(parent, f).getAbsolutePath());
}
Also, with your list of files (String[]), you can create an icon using :
String filename = fileStr[i]; // some file name within the array
ImageIcon icon = new ImageIcon("icons" + File.separator + filename);
or with your list of files (File[]), it is cleaner :
File file = files[i]; // some file within the File[] array
ImageIcon icon = new ImageIcon(file.getAbsolutePath());
Good luck.
The code you wrote looks okay. Are you sure the folder "icons" exists where Java is looking?
Try this:
File f = new File("icons");
System.out.println("Does icons exist?" + f.exists());
System.out.println("Is it a dir?" + f.isDirectory());
System.out.println("How many files does it contain?" + f.list().length);
Good luck!
I've had the same problem. When I tried moving the icons folder into the folder just before the src folder, it seems to work. Not sure what I will do when I submit the assignment, as for it to work in JCreator, I believe it has to be with the .java files.