helloworld programmer
helloworld programmer

Reputation: 49

Readinga file in java

what i was trying to do was copying a content of file using File using File input stream and writing it in to another file

 InputStream is = null;
        FileOutputStream fos=null;
        try{
            is = new FileInputStream("D:\\helloworld.txt");
             fos = new FileOutputStream("D:\\helloworld1.txt");
            byte[] buffer = new byte[255];
            while(is.read(buffer)>-1){

               fos.write(is.read(buffer,0,buffer.length));
            }
            is.close();
           fos.close();
        }catch(Exception e){

        }
    }

content of helloworld:

vsdaewfscvadfdsohcdvwbuivbASJXBBfjbzx cidbv k ab SifvicvahisvcbxsiSDobhsxcb Z asvfuigevwifuvweivfb

output of helloworld is:

ÿ

Upvotes: 0

Views: 39

Answers (1)

JP Moresmau
JP Moresmau

Reputation: 7393

 int r=0;
 while((r=is.read(buffer))>-1){
      fos.write(buffer,0,r);
 }

You're reading into buffer twice and writing the number of bytes read...

Upvotes: 3

Related Questions