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


G

giovannich1965

... 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!
*/
 
Ad

Advertisements

J

John B. Matthews

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.
 
E

Eric Sosman

.. 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.
 
G

giovannich1965

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, (e-mail address removed) a écrit :
 
G

giovannich1965

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, (e-mail address removed) a écrit :
 
L

Lew

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.
 
Ad

Advertisements

G

giovannich1965

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, (e-mail address removed) a écrit :
 

Top