Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
    Bookmark Topic Watch Topic
  • New Topic

copy a any type of file

 
sam liya
Ranch Hand
Posts: 1240
  • Mark post as helpful
  • send pies
  • Report post to moderator
i tried many times with below code.but it copy only the text files correctly.if file is non text it copy,but unable to open.
so please give me java code that copy any type of file(ex jpg,doc,war,mpg



package write;
import java.util.*;
import java.io.*;

class Read{
private static ArrayList<Byte> file = new ArrayList<Byte>();
private static String path="g:\\write\\name.txt";


private void reader()throws IOException //read a file and put to the arraylist
{
File f;
f=new File("g:\\read\\a.txt");

if(!f.exists()&& f.length()<0)
System.out.println("The specified file is not exist");

else{ ;


FileInputStream finp=new FileInputStream(f);

byte b=(byte)finp.read();

int i=0;
while(b!=-1)
{
file.add(b);
char d=(char)b;
Character ch=new Character(d);

String s=ch.toString();


b=(byte)finp.read();
i++;
}
finp.close();

}

}

private void writer()throws IOException //get byte by byte from arraylist and write to the file
{
File f=new File(path);
FileOutputStream fop=new FileOutputStream(f);

if(f.exists()){
String str="This data is written through the program";

Object []oa=file.toArray();
for(Object oa)
{
Byte b=(Byte)o;
fop.write(b);

}

fop.flush();
fop.close();

}
}


public static void main(String[] args) {
try{
Read r=new Read();
rd.reader();
rd.writer();
}catch(Exception e)
{
e.printStackTrace();
}
}
})
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Report post to moderator
Again, please Use Code Tags.

Also please Use One Thread Per Question. I don't see how this question is any different from this one.
 
    Bookmark Topic Watch Topic
  • New Topic