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

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

  1. Guest

    ... 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!
    */
    , Mar 5, 2013
    #1
    1. Advertising

  2. In article <>,
    wrote:

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

    [...]

    The array of bytes inherits toString() from java.lang.Object.

    > byte[] b = new byte[2];


    The method println invokes toString() to display 'a string that
    "textually represents" this object.'

    > System.out.println(b);


    You might try one of the java.util.Arrays.toString() variations.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Mar 5, 2013
    #2
    1. Advertising

  3. Eric Sosman Guest

    On 3/5/2013 11:44 AM, wrote:
    > .. but I can not see a byte that download ...
    > it displays this: [B @ 1f33675
    > continuously .... until the end
    > [...]
    > byte[] b = new byte[2];
    >[...]
    > while( (noOfBytes = fin.read(b)) != -1 )
    > {
    > System.out.println(b);


    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
    d
    Eric Sosman, Mar 5, 2013
    #3
  4. Guest

    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 :
    > .. 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!
    >
    > */
    , Mar 5, 2013
    #4
  5. Guest

    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 :
    > .. 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!
    >
    > */
    , Mar 5, 2013
    #5
  6. Lew Guest

    giovann...@ wrote:
    > 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 );


    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
    Lew, Mar 5, 2013
    #6
  7. Guest

    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 :
    > .. 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!
    >
    > */
    , Mar 6, 2013
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Carl
    Replies:
    4
    Views:
    401
    Peter Hansen
    May 21, 2004
  2. Daniel Crespo
    Replies:
    5
    Views:
    388
    Amaury
    Nov 13, 2005
  3. vijay
    Replies:
    8
    Views:
    679
  4. Replies:
    10
    Views:
    617
  5. Roy
    Replies:
    6
    Views:
    568
    Roedy Green
    Jan 7, 2008
Loading...

Share This Page