The Artima Developer Community
Sponsored Link

Java Answers Forum
copy file

3 replies on 1 page. Most recent reply: Oct 28, 2004 3:55 PM by Muhammad Zaman

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Maysoon

Posts: 64
Nickname: hm
Registered: Mar, 2002

copy file Posted: Aug 9, 2002 11:21 PM
Reply to this message Reply
Advertisement
hello all..


i have the following program that can copy any sort of file to another
file ,it works fine:






import java.io.*;

public class attachment1{

public static final int BUFFER_SIZE = 4096;
public static final int ERR = 1;
public static final int EOF = -1;
public static final int FILE_ENDED = 0;
public static final String ENCODING = "US-ASCII";
public static final String USAGE = "Usage :\njava TestFileReader <file_source&gt; <file_destination&gt;\n\n That's it ...\n";

public static void main (String[] args)
{
if (args.length != 2)
{
usage();
System.exit (ERR); }

try {
DataInputStream in = new DataInputStream(new FileInputStream(args[0]));
DataOutputStream out = new DataOutputStream (new FileOutputStream(args[1]));
int read; byte[] b,b1;
int i;
int c=0;
String all="";

while (( read = in.available ()) != FILE_ENDED)
{

b = new byte[read];
in.read (b);
out.write(b);
}


out.close ();
in.close ();
if (check(args[0], args[1]))
System.out.print("ok"); else
System.out.print("no");
}catch(Exception e)
{
} }
public static void usage ()
{
}
public static boolean check(String srcFileName, String destFileName)
{ try {
DataInputStream srcData = new DataInputStream (new FileInputStream (srcFileName)); DataInputStream destData = new DataInputStream (new FileInputStream (destFileName));
BufferedInputStream srcBIS = new BufferedInputStream (srcData);
BufferedInputStream destBIS = new BufferedInputStream (destData);
StringBuffer srcBuf = new StringBuffer(), destBuf = new StringBuffer ();
String s = null;
int read = 0;
byte [] b;
while ((read = srcBIS.available ()) != FILE_ENDED)
{
b = new byte [read];
srcBIS.read (b);
srcBuf.append ( new String (b, ENCODING ));
}
read = 0;
while (( read = destBIS.available ()) != FILE_ENDED)
{
b = new byte [read];
destBIS.read (b);
destBuf.append ( new String (b, ENCODING) );
} //

if (destBuf.toString ().equals (srcBuf.toString ()))
return true;
else
return false; } catch (Exception ex)
{
return false; }

} }


but in my client/server application i want to transfer this file from client to server i want to convert the b (array of bytes) to string
and then convert this string to array of bytes(b1)
and store b1 ,but the copy is not correct and the valuse of b1 do not equal b why?please i need help ,the following is changes i have made







import java.io.*;

public class attachment1{

public static final int BUFFER_SIZE = 4096;
public static final int ERR = 1;
public static final int EOF = -1;
public static final int FILE_ENDED = 0;
public static final String ENCODING = "US-ASCII";
public static final String USAGE = "Usage :\njava TestFileReader <file_source&gt; <file_destination&gt;\n\n That's it ...\n";

public static void main (String[] args)
{
if (args.length != 2)
{
usage();
System.exit (ERR); }

try {
DataInputStream in = new DataInputStream(new FileInputStream(args[0]));
DataOutputStream out = new DataOutputStream (new FileOutputStream(args[1]));
int read; byte[] b,b1;
int i;
int c=0;
String all="";

while (( read = in.available ()) != FILE_ENDED)
{

b = new byte[read];
in.read (b);
String se=new String(b, ENCODING);
b1=new byte[read];
b1=se.getBytes(ENCODING)
// the value of b!=b1 why
out.write(b1);
}


out.close ();
in.close ();
if (check(args[0], args[1]))
System.out.print("ok"); else
System.out.print("no");
}catch(Exception e)
{
} }
public static void usage ()
{
}
public static boolean check(String srcFileName, String destFileName)
{ try {
DataInputStream srcData = new DataInputStream (new FileInputStream (srcFileName)); DataInputStream destData = new DataInputStream (new FileInputStream (destFileName));
BufferedInputStream srcBIS = new BufferedInputStream (srcData);
BufferedInputStream destBIS = new BufferedInputStream (destData);
StringBuffer srcBuf = new StringBuffer(), destBuf = new StringBuffer ();
String s = null;
int read = 0;
byte [] b;
while ((read = srcBIS.available ()) != FILE_ENDED)
{
b = new byte [read];
srcBIS.read (b);
srcBuf.append ( new String (b, ENCODING ));
}
read = 0;
while (( read = destBIS.available ()) != FILE_ENDED)
{
b = new byte [read];
destBIS.read (b);
destBuf.append ( new String (b, ENCODING) );
} //

if (destBuf.toString ().equals (srcBuf.toString ()))
return true;
else
return false; } catch (Exception ex)
{
return false; }

} }


please please i need help..


Muhammad Zaman

Posts: 3
Nickname: mzamanm
Registered: Oct, 2004

Re: copy file Posted: Oct 26, 2004 1:33 PM
Reply to this message Reply
If you have got it donw please inform me,how did you done it.i have tried my best to do so but nothing.Please tell me how to send and recieve it over network.
THanX

Guillaume Taglang

Posts: 18
Nickname: gouyou
Registered: Jul, 2003

Re: copy file Posted: Oct 28, 2004 9:19 AM
Reply to this message Reply
Did you try out with something like base64 encoding ? These is the usual way of transmitting data as text. If you are in need of an Open Source implementation of Base64, just google it ...

Cheers,
Guillaume

Muhammad Zaman

Posts: 3
Nickname: mzamanm
Registered: Oct, 2004

Re: copy file Posted: Oct 28, 2004 3:55 PM
Reply to this message Reply
i have done it .wanna take source files .give me your mail add.i will send it to you.
Pleasures.

Flat View: This topic has 3 replies on 1 page
Topic: faq's in java Previous Topic   Next Topic Topic: my cellRenderer won't work

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use