hello here is a copy of a copy program on the net .

Discussion in 'Java' started by giovannich1965, Mar 5, 2013.

  1. ... but I can not see a byte that download ...
    it displays this: [B @ 1f33675
      continuously .... until the end

    if you can help me thank you ...

    package fichier;

    import java.io.FileNotFoundException;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;


    public class fichier {

    public static void main(String[] args) {

    String strSourceFile="C:/temp/demo.txt";
    String strDestinationFile="C:/temp/demo2.txt";

    try
    {
    //create FileInputStream object for source file
    FileInputStream fin = new FileInputStream(strSourceFile);

    //create FileOutputStream object for destination file
    FileOutputStream fout = new FileOutputStream(strDestinationFile);

    byte[] b = new byte[2];
    int noOfBytes = 0;

    System.out.println("Copying file using streams");

    //read bytes from source file and write to destination file
    while( (noOfBytes = fin.read(b)) != -1 )
    {
    System.out.println(b);
    fout.write(b, 0, noOfBytes);
    }

    System.out.println("File copied!");

    //close the streams
    fin.close();
    fout.close();

    }
    catch(FileNotFoundException fnf)
    {
    System.out.println("Specified file not found :" + fnf);
    }
    catch(IOException ioe)
    {
    System.out.println("Error while copying file :" + ioe);
    }
    }
    }

    /*
    Typical output would be
    Copying file using streams
    File copied!
    */
     
    giovannich1965, Mar 5, 2013
    #1
    1. Advertisements

  2. [...]

    The array of bytes inherits toString() from java.lang.Object.
    The method println invokes toString() to display 'a string that
    "textually represents" this object.'
    You might try one of the java.util.Arrays.toString() variations.
     
    John B. Matthews, Mar 5, 2013
    #2
    1. Advertisements

  3. giovannich1965

    Eric Sosman Guest

    You are printing the String representation of the `b' array,
    not representations of its content. Instead, you need something
    more like

    for (int i = 0; i < noOfBytes; ++i)
    System.out.println(b);

    .... to print the array's bytes one by one.

    Even that is not likely to please you, though, since the
    output will be a whole lot of numbers, one per line. A Java
    byte is an integer in the range -128 through +127, and the
    output will show those numeric values.

    The fact that you are using a two-byte array suggests that
    you may be trying to print characters, and that you have heard
    somewhere that a Java character is a two-byte value. That is
    true, as far as it goes, but this doesn't mean you magically get
    a `char' by reading two `byte's from a file. There's a translation
    between internal and external representation of characters, and
    the fact that Java uses a value from 0 through 65535 to represent
    a character doesn't mean that the text file on your disk does the
    same. If you want to copy and print characters, as opposed to
    "binary" bytes, consider using one of the xxxReader classes: They
    know how to do the necessary translations.
     
    Eric Sosman, Mar 5, 2013
    #3
  4. thank you
    display is just a control to see if read
    but if I wanted to see how hexaa instead of decimal?
    And how to change the value read
    for example by inverting the value?

    b = - (b );



    Le mardi 5 mars 2013 17:44:01 UTC+1, a écrit :
     
    giovannich1965, Mar 5, 2013
    #4
  5. thank you
    display is just a control to see if read
    but if I wanted to see how hexaa instead of decimal?
    And how to change the value read
    for example by inverting the value?

    b = - (b );


    ps: escuse me error, translate in english me french me langage


    Le mardi 5 mars 2013 17:44:01 UTC+1, a écrit :
     
    giovannich1965, Mar 5, 2013
    #5
  6. giovannich1965

    Lew Guest



    http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format(java.lang.String, java.lang.Object...)
    http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax
    http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

    String rep = "";
    for ( byte by : b )
    {
    rep += String.format( "%2.2x ", by);
    }

    b = - (b );
    is a perfectly valid expression. It has gotchas; the right-hand side is evaluated as an 'int'
    and undergoes a narrowing conversion on the assignment.
     
    Lew, Mar 5, 2013
    #6
  7. I place or that?

    while( (noOfBytes = fin.read(b)) != -1 )
    {
    System.out.println(b);
    fout.write(b, 0, noOfBytes);
    String rep = "";
    for ( byte by : b )
    {
    rep += String.format( "%2.2x ", by);
    }

    b = - (b );


    }


    Le mardi 5 mars 2013 17:44:01 UTC+1, a écrit :
     
    giovannich1965, Mar 6, 2013
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.