Two Laptop Bag*
The moose likes I/O and Streams and the fly likes Reading and writing an Excel file from one place to another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Reading and writing an Excel file from one place to another" Watch "Reading and writing an Excel file from one place to another" New topic
Author

Reading and writing an Excel file from one place to another

Sam Moran
Ranch Hand

Joined: Sep 28, 2002
Posts: 86
I have created a socket and am attempting to pull an excel file from an ftp website. I have a handle on the file and read it in using BufferedReader(new InputStreamReader(client.getInputStream());, When I write the file out to my local drive the file gets butchered and I can not open it! Does anyone have any ideas? I am compiling with 1.3.1. I have attempted a pdf file also and get the same results. I know how to read and write text files, that's a piece of cake. Thank you in advance for any help.


We make a living by what we get, we make a life by what we give!
Tom Ben
Ranch Hand

Joined: Aug 17, 2001
Posts: 109
It sounds as if you are reading the file in as binary and then writing it out as text. how are you writing this out? Please display that part of your code.


Sun Certified Programmer on the Java 2 Platform
Sam Moran
Ranch Hand

Joined: Sep 28, 2002
Posts: 86
I have tried 3 different ways of trying to do this and this is my latest. Each time I get the same results.
FileOutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[1024 * 8];
int bytesRead;
while (-1 != (bytesRead = in.read(buffer, 0, buffer.length))) {
out.write(buffer, 0, bytesRead);
}
Thank you very much for responding!
Tom Ben
Ranch Hand

Joined: Aug 17, 2001
Posts: 109
Please post your entire program so I can see it please. One way or another will figure this out.
Sam Moran
Ranch Hand

Joined: Sep 28, 2002
Posts: 86
This program started out as a learning exercise on Sockets, but a co-worker asked if I could help her with a program that would copy a file from a secure ftp site down to our local server. The file is an Excel file. I have done testing with the follwing code on a pdf, xls, and a gif. I get the same results, a butchered file. The pdf file was the best test because I could see the results easily in my Text Editor. Extra newlines appear.
Thank you again for looking at this, sorry about the code past I am not sure what to expect when I post this!
public class ProxyTest {
public static void main( String argv[] ) {
Socket mySocket = null;
PrintWriter cmdout = null;
String theurl = "myftpsite";
String fileName = "test.xls";
String dirName = "tmp/";
String file = "d:\\my.xls";
try {
mySocket = new Socket(theurl, 21);
cmdout = new PrintWriter(new OutputStreamWriter(mySocket.getOutputStream()));
String tmp = mySocket.getInetAddress().toString();
if (mySocket == null)
throw new IOException ("The Socket never opened");
System.out.println("Address : " + tmp);
URL url = new URL("myftpsite");
URLConnection connection = url.openConnection();
cmdout.println("USER myusername");
cmdout.flush();
cmdout.println("PASS mypassword");
cmdout.flush();
cmdout.println("CWD " + dirName);
cmdout.flush();
ServerSocket server = new ServerSocket(0);
String addr = InetAddress.getLocalHost().getHostAddress();
addr = addr.replace('.', ',');
int port = server.getLocalPort();
int portHi = port / 256;
int portLo = port % 256;
addr += "," + portHi + "," + portLo;
cmdout.println("PORT " + addr);
cmdout.flush();
cmdout.println("RETR " + fileName);
cmdout.flush();
Socket client = server.accept();
BufferedInputStream in = new BufferedInputStream(client.getInputStream());
FileOutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[1024 * 8];
int bytesRead;
while (-1 != (bytesRead = in.read(buffer, 0, buffer.length))) {
out.write(buffer, 0, bytesRead);
System.out.println("byte : " + bytesRead);
}
in.close();
out.close();
client.close();
server.close();
} catch (UnknownHostException e) {
System.err.println("Don't know about host: myweb.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: ftp server.");
System.exit(1);
}
try {
mySocket.close();
} catch (IOException e) {
System.err.println("Can not close");
System.exit(1);
}
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Reading and writing an Excel file from one place to another
 
Similar Threads
Reading and Writing from Excel Spreadsheet
Reading hindi text from excel file and getting its corresponding unicode
Excel file manipulation
Write Data to Excel
how to write and read excel file