Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 337
  • Last Modified:

Trouble with FileChannel

I get the following error message when I try to run ChannelDemo4 (listed below):

ChannelDemo4.java:27: transferTo(long,int,java.nio.channels.WritableByteChannel) in java.nio.channels.FileChannel cannot be applied to (int,long,java.nio.channels.FileChannel)
            fcin.transferTo(0, fcin.size(), fcout);

    import java.nio.*;
    import java.nio.channels.*;
    import java.io.*;
   
    public class ChannelDemo4 {
        public static void main(String args[])
                                   throws IOException {
   
            // check command-line arguments
   
            if (args.length != 2) {
                System.err.println("missing filenames");
                System.exit(1);
            }
   
            // get channels
   
            FileInputStream fis =
                          new FileInputStream(args[0]);
            FileOutputStream fos =
                         new FileOutputStream(args[1]);
            FileChannel fcin = fis.getChannel();
            FileChannel fcout = fos.getChannel();
   
            // do the file copy
   
            fcin.transferTo(0, fcin.size(), fcout);
   
            // finish up
   
            fcin.close();
            fcout.close();
            fis.close();
            fos.close();
        }
    }


Does anyone know how I can correct this?  Thanks!
0
cneaton
Asked:
cneaton
  • 3
  • 2
  • 2
1 Solution
 
hoomanvCommented:
I run the code without any error
0
 
hoomanvCommented:
> I get the following error message when I try to run ChannelDemo4
passing wrong types to methods is indeed a compile error rather than a runtime error
however looking at transferTo method signature

transferTo(long position, long count, WritableByteChannel target)

would bring in mind that there is somthing wrong with your java library

transferTo(long,int,java.nio.channels.WritableByteChannel)
0
 
CEHJCommented:
You're passing an int instead of long

>>fcin.transferTo(0, fcin.size(), fcout);

should be

fcin.transferTo(0L, fcin.size(), fcout);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
cneatonAuthor Commented:
Looks like my path variable was pointing to the wrong Java version.  hoomany, your comment made me check that so that's why you got the points.  Thanks everyone!
0
 
CEHJCommented:
?
0
 
CEHJCommented:
>>Looks like my path variable was pointing to the wrong Java version.

Your error was nothing to do with that
0
 
cneatonAuthor Commented:
You're right, CEHJ.  The error that I got says that I'm passing an int when I need a long.  But as hoomany pointed out, the transferTo that I wanted to use was of the form:

transferTo(long position, long count, WritableByteChannel target)

instead of

transferTo(int,long,java.nio.channels.FileChannel)

hoomany's comment that "there is something wrong with your java library" motivated me to investigate what might be wrong with it and voila, my problem was solved.  I can run the code above "as is" now.  Thanks for your comment, though.


0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now