Hey
I have a folder in my current working path and I want to make a copy of a file inside it to the the current working path. Its kind of a dumb question, I know, but how do it do it?
Thank you.
Hey
I have a folder in my current working path and I want to make a copy of a file inside it to the the current working path. Its kind of a dumb question, I know, but how do it do it?
Thank you.
The error you hit ("Source ... does not exist") almost always means the path you actually handed to the copy routine isn't the file you think it is. In the thread you printed two different paths (one that includes the Tomcat bin folder and one that doesn't) — that mismatch is the likely root cause. Building paths by hand with string concatenation is fragile: prefer the File API, check existence early, and print canonical paths for debugging.
Use File objects to derive parents and build children, then assert existence before copying. Example (Java 6-compatible):
File cwd = new File(System.getProperty("user.dir"));
File parent = cwd.getParentFile(); // one-up reliably
File src = new File(parent, "234_99_165544300_1" + File.separator + "234_99_165544300.bmp");
File dst = new File(parent, "234_99_165544300.bmp");
if (!src.isFile()) {
System.err.println("Missing source: " + src.getCanonicalPath());
} else {
copyFile(src, dst);
}
private static void copyFile(File src, File dst) throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(src));
OutputStream out = new BufferedOutputStream(new FileOutputStream(dst));
byte[] buf = new byte[8192];
int n;
while ((n = in.read(buf)) > 0) out.write(buf, 0, n);
in.close();
out.close();
} Troubleshooting checklist: 1) print src.getCanonicalPath() and src.exists()/isFile(); 2) avoid mixing separators — use File/ File.separator or new File(base, child); 3) confirm the resource is not packed inside a ZIP/JAR (if it is, open it with ZipFile rather than File); 4) consider Apache Commons IO’s FileUtils if you can add it. ’s getParent suggestion and ’s lastIndexOf idea are valid, but using the File API is clearer and less error-prone. ultimately fixed this by extracting the BMP from the ZIP — that confirms the file content/location was the actual issue rather than Java path syntax.
Jump to Post— DavidKroukamp 105Hey
I have a folder in my current working path and I want to make a copy of a file inside it to the the current working path. Its kind of a dumb question, I know, but how do it do it?
Thank you.
maybe this:
Jump to Post— DavidKroukamp 105If Im located there, how would I go up a directory (To "C:\Archivos de programa\MyEclipse for Spring\Common\plugins\com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201109141806\tomcat" ) ?
You could feed the path into [regexp (.+)/.+] to take off the last folder in the path.
[edit] and here's a way to copy in java 6:
Jump to Post— DavidKroukamp 105Ah, regular expresions......OK.......
But what line exactly would I use? Not too sure...
what do you mean what line?
You could also just use a lastIndexOf("\\"), which will get the the position of the last '\' and then you just replace it so then you;d have:C:\Archivos de programa\MyEclipse for …
Hey
I have a folder in my current working path and I want to make a copy of a file inside it to the the current working path. Its kind of a dumb question, I know, but how do it do it?
Thank you.
maybe this:http://edgblog.wordpress.com/2010/02/20/how-to-copy-a-file-in-java/ and this:http://www.java7developer.com/blog/?p=334 and this: just for a variety :P
Java 6 only.
The thing is I cant get the directoy where it saves it which is
"C:\Archivos de programa\MyEclipse for Spring\Common\plugins\com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201109141806\tomcat\bin"
It is driving me nuts.
If Im located there, how would I go up a directory (To "C:\Archivos de programa\MyEclipse for Spring\Common\plugins\com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201109141806\tomcat" ) ?
If Im located there, how would I go up a directory (To "C:\Archivos de programa\MyEclipse for Spring\Common\plugins\com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201109141806\tomcat" ) ?
You could feed the path into [regexp (.+)/.+] to take off the last folder in the path.
[edit] and here's a way to copy in java 6:http://www.roseindia.net/java/beginners/CopyFile.shtml
You could feed the path into [regexp (.+)/.+] to take off the last folder in the path.
How do I do that? Dont understand that exactly....
What is regexp?
Ah, regular expresions......OK.......
But what line exactly would I use? Not too sure...
Ah, regular expresions......OK.......
But what line exactly would I use? Not too sure...
what do you mean what line?
You could also just use a lastIndexOf("\\"), which will get the the position of the last '\' and then you just replace it so then you;d have:C:\Archivos de programa\MyEclipse for Spring\Common\plugins\com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201109141806
what do you mean what line?
You could also just use a lastIndexOf("\\"), which will get the the position of the last '\' and then you just replace it so then you;d have:C:\Archivos de programa\MyEclipse for Spring\Common\plugins\com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201109141806
What instruction line do I use?
Yeah, used lastindex as a alternative I suppose but after finding the path, I cant get it to copy. It says that it cant find the path when the path is perfectly correct as I print it out before I do it and when the exception jumps, the path is exactly the same.....
What could be wrong? Man this is killing me :(
What instruction line do I use?
Yeah, used lastindex as a alternative I suppose but after finding the path, I cant get it to copy. It says that it cant find the path when the path is perfectly correct as I print it out before I do it and when the exception jumps, the path is exactly the same.....
What could be wrong? Man this is killing me :(
well it would help posting your code, and the error message and line number and the output of your new path you are trying...
well it would help posting your code, and the error message and line number and the output of your new path you are trying...
Its a lot of code and kind of complicated but I think it is around here:
String ruta= System.getProperty("user.dir")+ "\\"; //ruta=ruta+ "\\"; System.out.println("La ruta es: " + ruta); System.out.println("Voy a crear un zip " + System.getProperty("user.dir") + " " + binpath + " " + projectpath + " " + realprojectpath ); Crear_Zip z=new Crear_Zip(); String[] trozos=nombreprograma.split("_"); String nombrebmp=trozos[0]+"_"+trozos[1]+"_"+trozos[2]; System.out.println("El string del BMP es: " + nombrebmp); //FileUtils.copyFile(new File(nombreprograma+"\\"+nombrebmp+".bmp"), new File(nombrebmp+".bmp")); System.out.println("Voy a copiar desde: " + ruta+nombreprograma+"\\"+nombrebmp+".bmp"); System.out.println("Voy a copiar ha: " + nombrebmp+".bmp"); //String desde=ruta+nombreprograma+"\\"+nombrebmp+".bmp"; String nuevodesde=realprojectpath+nombreprograma+"/"+nombrebmp+".bmp"; System.out.println("nuevo: " + nuevodesde); FileUtils.copyFile(new File(nuevodesde), new File(nombrebmp+".bmp")); And there is the output:
La ruta es: C:\Archivos de programa\MyEclipse for Spring\Common\plugins\com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201109141806\tomcat\bin\
Voy a crear un zip C:\Archivos de programa\MyEclipse for Spring\Common\plugins\com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201109141806\tomcat\bin file:/C:/Archivos de programa/MyEclipse for Spring/Common/plugins/com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201109141806/tomcat/lib file:/C:/Archivos de programa/MyEclipse for Spring/Common/plugins/com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201109141806/tomcat/ C:/Archivos de programa/MyEclipse for Spring/Common/plugins/com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201109141806/tomcat/
El string del BMP es: 234_99_165544300
Voy a copiar desde: C:\Archivos de programa\MyEclipse for Spring\Common\plugins\com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201109141806\tomcat\bin\234_99_165544300_1\234_99_165544300.bmp
Voy a copiar ha: 234_99_165544300.bmp
nuevo: C:/Archivos de programa/MyEclipse for Spring/Common/plugins/com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201109141806/tomcat/234_99_165544300_1/234_99_165544300.bmp
Ha fallado: Rollback time!
2012-02-16 JUE 16:55:44.300 : Ha ocurrido un error generico: Source 'C:\Archivos de programa\MyEclipse for Spring\Common\plugins\com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201109141806\tomcat\234_99_165544300_1\234_99_165544300.bmp' does not exist
If Im located there, how would I go up a directory (To "C:\Archivos de programa\MyEclipse for Spring\Common\plugins\com.genuitec.eclipse.easie.tomcat.myeclipse_9.0.0.me201109141806\tomcat" ) ?
You don't need all that manual "last index" parsing pain.
Just create a File object to the original dir then use getParent() to give you the parent (one-up) dir.
eg
File f = new File("c:/abc/def/ghi/jk");
System.out.println(f.getParent()); prints
c:\abc\def\ghi FINALLY......what I did is that I unzipped the file that I originally created by searching for that BMP inside the ZIP file. That creates it back to where I need and i creates the PDF perfectly.
Ive been about a week with this so finally.....
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.