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> <file_destination>\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) ); } //
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> <file_destination>\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 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
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 ...