Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Copy folder from jar into temp directory

807599Mar 30 2007 — edited Jul 24 2008
There are some files inside the folder folder kept in the jar file, which I am copying one by one to temp directory as shown in my below code. Could someone please tell me how do I use a loop to copy all the files from the folder folder to temp directory
String path = System.getProperty("java.io.tmpdir");
File dir = new File(path+"\\folder");  
dir.mkdirs();
InputStream is1=Main.class.getClass().getResourceAsStream("/input/folder/file1.txt");
try 
{
	File.createTempFile("file1", ".txt",dir.getCanonicalFile());        
} 
catch (IOException e) 
{ 
	e.printStackTrace(); 
}
File dstfile1=new File(dir,"\\file1.txt");
dstfile1.deleteOnExit();
FileOutputStream fos1 = new FileOutputStream(dstfile1);
int b1;
while((b1 = is1.read()) != -1) 
{                
	fos1.write(b1);
}
fos1.close();  

.
.
.

InputStream is2=Main.class.getClass().getResourceAsStream("/input/folder/file2.txt");
try 
{
	File.createTempFile("file2", ".txt",dir.getCanonicalFile());        
} 
catch (IOException e) 
{ 
	e.printStackTrace(); 
}
File dstfile2=new File(dir,"\\file2.txt");
dstfile1.deleteOnExit();
FileOutputStream fos2 = new FileOutputStream(dstfile2);
int b2;
while((b2 = is2.read()) != -1) 
{                
	fos2.write(b2);
}
fos2.close();
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 21 2008
Added on Mar 30 2007
15 comments
7,069 views