So I am trying to copy some files with Files.copy(), and while it is working fine in Mac, and works to copy text files on Windows, when I try to copy a binary file I just get the error:
java.nio.file.NoSuchFileException: C:\path\to\file
My function is:
private void copyFiles()
{
ArrayList<String> temp = new ArrayList<>(); //This is set up outside the function
temp.add("file1");
temp.add("file2"); //etc
String AlphaSimFileName = "folderName"; //This is actually set outside the function
String currentDir = System.getProperty("user.dir");
Path baseAlphaName = Paths.get(currentDir, AlphaSimFileName);
Path baseDirectoryName = Paths.get(currentDir, name);
System.out.println(""+baseAlphaName.toString());
System.out.println(""+baseDirectoryName.toString());
// for (String l: MyFunctions.getFilesAsString()) //gives the list of files to copy.
for (String l: temp)
{
Path p1 = Paths.get(baseAlphaName.toString(), l);
Path p2 = Paths.get(baseDirectoryName.toString(), l);
try
{
Files.copy(p1, p2, StandardCopyOption.REPLACE_EXISTING);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Anyone have any idea why this is happening?
(Posted on behalf of the OP).
The reason for this was that Windows was automatically hiding file extensions, so the file that I was trying to move (i.e. without the extension) didn't exist.
Try doing it manually first. Use \\ instead of \ when you hardcode the path for Windows using Java. Also, the file extension may be missing.
Related
Files inside the (Tracks)directory was not deleted. The method deletes the wav files stored in the directory.
public boolean deleteTrack(response) {
ListIterator<Track> trackListIterator = this.trackList.listIterator();
//tracklist is the linked list on which I'm using list iterator. I'm storing song which is a object inside it. this object has a fn() that returns content root path not absolute path.
String path = "";
while (trackListIterator.hasNext()) {
//RESPONSE == PARAMETER
if (trackListIterator.next().getTrackName().equals(response)) {
trackListIterator.previous();
path = trackListIterator.next().getTrackPath();//this is the fn() that
returns content root path example(src/Exploit/org/Trackstore/Music/Action Movie Music-FesliyanStudios.wav).
break;
}
}
File file = new File(path);
//here I'm taking absolute path for deleting actual wav file from the computer.
File toBeDeleted = new File(file.getAbsolutePath());
return toBeDeleted.delete();// returns false everytime.
}
The old API has many issues. For example, most methods return a boolean to indicate the result which is stupid and unjavalike - fortunately, there is a new API that fixes these issues.
Use it and you'll know WHY it failed. If that's too much effort, well, there isn't much to say. It didn't delete. No idea why, and there's no way to ask that API about why.
The new API lives in the java.nio.file package.
Replace this:
File f = new File("path/to/file");
if (!f.delete()) { ... it didn't work ... }
with:
Path p = Paths.get("path/to/file");
try {
Files.delete(p);
} catch (IOException e) {
// the exception will explain everything there is to be said about why it did not work!
}
I have a sqlite database file that i am accessing using a swing aplication. The jdbc driver and everyhting works. If i get the exact file location and paste it into the database url it will work. I tried using a relative file path and it does not work. I created a method to get the direct file path to the working directory of the program and craeted a map if you will of the folder and its contents. it should work, but doesn't, i was wondering if anyone could tell me why. here is the method
public String GetAbsPath(){
File workingDir=new File(".");
String absolute=workingDir.getAbsolutePath();
char[] absA=absolute.toCharArray();
ArrayList<String> list=new ArrayList<>();
for (int x = 0; x < absA.length; x++) {
String listPiece = Character.toString(absA[x]);
list.add(listPiece);
}
StringBuilder result = new StringBuilder();
for (int i = 0; i < list.size()-1; i++) {
if(list.get(i).equals("\\")){
list.set(i, "\\\\");
}
result.append(list.get(i));
}
String path=result.toString();
return path;
}
it returns the exact file path that i enter in manually, but will not find the file. both methods work with netbeans but only entering in the exact file path into a string works when i try to run the file outside of netbeans. This is where the method is called.
GetPath dir=new GetPath();
String dirFilePath = "jdbc:sqlite:"+dir.GetAbsPath()+"Office.sqlite";
database=dirFilePath;
This worked...
CodeSource codeSource = MainFrame.class.getProtectionDomain().getCodeSource();
File jarFile=null;
String jarDir=null;
try {
jarFile = new File(codeSource.getLocation().toURI().getPath());
jarDir = jarFile.getParentFile().getPath();
} catch (URISyntaxException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
database ="jdbc:sqlite:"+jarDir +"\\Office.sqlite";
To get the absolute path of the current working directory use:
System.getProperty("user.dir")
which returns a String.
EDIT:
If you want to replace the "\" with a "/", use this:
String cwd = System.getProperty("user.dir").replaceAll("\\\\", "/");
or
String cwd = System.getProperty("user.dir").replaceAll(Pattern.quote("\\"), "/");
For an explanation of the usage of the regex in the latter two statements, see here.
EDIT 2 (based off of comments):
Since the comparison of the two strings via .equals() return true, the only thing that I can think of is that this is an odd NetBeans issue. I suggest trying a different IDE, perhaps Eclipse.
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.
I have around 500 text files inside a directory with each with the same prefix in their filename, for example: dailyReport_.
The latter part of the file is the date of the file. (For example dailyReport_08262011.txt, dailyReport_08232011.txt)
I want to delete these files using a Java procedure. (I could go for a shell script and add it a job in the crontab but the application is meant to used by laymen).
I can delete a single file using something like this:
try{
File f=new File("dailyReport_08232011.txt");
f.delete();
}
catch(Exception e){
System.out.println(e);
}
but can I delete the files having a certain prefix? (e.g. dailyReport08 for the 8th month) I could easily do that in shell script by using rm -rf dailyReport08*.txt .
But File f=new File("dailyReport_08*.txt"); doesnt work in Java (as expected).
Now is anything similar possible in Java without running a loop that searches the directory for files?
Can I achieve this using some special characters similar to * used in shell script?
No, you can't. Java is rather low-level language -- comparing with shell-script -- so things like this must be done more explicetly. You should search for files with required mask with folder.listFiles(FilenameFilter), and iterate through returned array deleting each entry. Like this:
final File folder = ...
final File[] files = folder.listFiles( new FilenameFilter() {
#Override
public boolean accept( final File dir,
final String name ) {
return name.matches( "dailyReport_08.*\\.txt" );
}
} );
for ( final File file : files ) {
if ( !file.delete() ) {
System.err.println( "Can't remove " + file.getAbsolutePath() );
}
}
You can use a loop
for (File f : directory.listFiles()) {
if (f.getName().startsWith("dailyReport_")) {
f.delete();
}
}
Java 8 :
final File downloadDirectory = new File("directoryPath");
final File[] files = downloadDirectory.listFiles( (dir,name) -> name.matches("dailyReport_.*?" ));
Arrays.asList(files).stream().forEach(File::delete)
With Java 8:
public static boolean deleteFilesForPathByPrefix(final String path, final String prefix) {
boolean success = true;
try (DirectoryStream<Path> newDirectoryStream = Files.newDirectoryStream(Paths.get(path), prefix + "*")) {
for (final Path newDirectoryStreamItem : newDirectoryStream) {
Files.delete(newDirectoryStreamItem);
}
} catch (final Exception e) {
success = false;
e.printStackTrace();
}
return success;
}
Simple version:
public static void deleteFilesForPathByPrefix(final Path path, final String prefix) {
try (DirectoryStream<Path> newDirectoryStream = Files.newDirectoryStream(path, prefix + "*")) {
for (final Path newDirectoryStreamItem : newDirectoryStream) {
Files.delete(newDirectoryStreamItem);
}
} catch (final Exception e) { // empty
}
}
Modify the Path/String argument as needed. You can even convert between File and Path. Path is preferred for Java >= 8.
I know I'm late to the party. However, for future reference, I wanted to contribute a java 8 stream solution that doesn't involve a loop.
It may not be pretty. I welcome suggestions to make it look better. However, it does the job:
Files.list(deleteDirectory).filter(p -> p.toString().contains("dailyReport_08")).forEach((p) -> {
try {
Files.deleteIfExists(p);
} catch (Exception e) {
e.printStackTrace();
}
});
Alternatively, you can use Files.walk which will traverse the directory depth-first. That is, if the files are buried in different directories.
Use FileFilter like so:
File dir = new File(<path to dir>);
File[] toBeDeleted = dir.listFiles(new FileFilter() {
boolean accept(File pathname) {
return (pathname.getName().startsWith("dailyReport_08") && pathname.getName().endsWith(".txt"));
}
for (File f : toBeDeleted) {
f.delete();
}
There isn't a wildcard but you can implement a FilenameFilter and check the path with a startsWith("dailyReport_"). Then calling File.listFiles(filter) gives you an array of Files that you can loop through and call delete() on.
I agree with BegemoT.
However, just one optimization:
If you need a simple FilenameFilter, there is a class in the Google packages.
So, in this case you do not even have to create your own anonymous class.
import com.google.common.io.PatternFilenameFilter;
final File folder = ...
final File[] files = folder.listFiles(new PatternFilenameFilter("dailyReport_08.*\\.txt"));
// loop through the files
for ( final File file : files ) {
if ( !file.delete() ) {
System.err.println( "Can't remove " + file.getAbsolutePath() );
}
}
Enjoy !
You can't do it without a loop. But you can enhance this loop. First of all, ask you a question: "what's the problem with searching and removing in the loop?" If it's too slow for some reason, you can just run your loop in a separate thread, so that it will not affect your user interface.
Other advice - put your daily reports in a separate folder and then you will be able to remove this folder with all content.
or in scala
new java.io.File(<<pathStr>>).listFiles.filter(_.getName.endsWith(".txt")).foreach(_.delete())
Have a look at Apache FileUtils which offers many handy file manipulations.
I'm writing a simple command line Java utility. I would like the user to be able to pass in a file path relative to their home directory using the ~ operator. So something like ~/Documents/...
My question is is there a way to make Java resolve this type of path automatically? Or do I need to scan the file path for the ~ operator?
It seems like this type of functionality should be baked into the File object. But it doesn't seem to be.
A simple path = path.replaceFirst("^~", System.getProperty("user.home")); when it is gotten from the user (before making a File out of it) should be enough to work in most cases - because the tilde is only expanded to a home directory if it is the first character in a directory section of a path.
This is shell-specific expansion, so you need to replace it at the beginning of the line, if present:
String path = "~/xyz";
...
if (path.startsWith("~" + File.separator)) {
path = System.getProperty("user.home") + path.substring(1);
} else if (path.startsWith("~")) {
// here you can implement reading homedir of other users if you care
throw new UnsupportedOperationException("Home dir expansion not implemented for explicit usernames");
}
File f = new File(path);
...
As Edwin Buck pointed out in the comment to another answer, ~otheruser/Documents should also expand correctly. Here's a function that worked for me:
public String expandPath(String path) {
try {
String command = "ls -d " + path;
Process shellExec = Runtime.getRuntime().exec(
new String[]{"bash", "-c", command});
BufferedReader reader = new BufferedReader(
new InputStreamReader(shellExec.getInputStream()));
String expandedPath = reader.readLine();
// Only return a new value if expansion worked.
// We're reading from stdin. If there was a problem, it was written
// to stderr and our result will be null.
if (expandedPath != null) {
path = expandedPath;
}
} catch (java.io.IOException ex) {
// Just consider it unexpandable and return original path.
}
return path;
}
A fairly streamlined answer that works with paths with actual ~ characters in them:
String path = "~/Documents";
path.replaceFirst("^~", System.getProperty("user.home"));
Previously mentioned solutions do not behave as expected when user home contains '\' or other special chars. This works for me:
path = path.replaceFirst("^~", Matcher.quoteReplacement(System.getProperty("user.home")));