Results 1 to 9 of 9
Thread: Reading content from an ftp file
- 01-27-2013, 01:35 AM #1
Member
- Join Date
- Jan 2013
- Posts
- 25
- Rep Power
- 0
Reading content from an ftp file
Hello. I am trying to read a content of file downloaded from ftp and return it as a String. This is what I use:
The code does not work, although all the links are correct. Thanks a lot for your help!Java Code:import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import org.apache.commons.net.ftp.FTPClient; public class Test { public static void main(String [] args) throws IOException { FTPClient ftp = new FTPClient(); ftp.connect("ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/"); InputStream in = ftp.retrieveFileStream("KLAS.TXT"); BufferedInputStream inbf = new BufferedInputStream(in); int bytesRead; byte[] buffer=new byte[1024]; String fileContent=null; while((bytesRead=inbf.read(buffer))!=-1) { fileContent=new String(buffer,0,bytesRead); } System.out.println("File: " + fileContent); } }
-
Re: Reading content from an ftp file
For one thing, you're over-writing the content of the fileContent String with each iteration of the loop. For another, if the data is truly String data, consider wrapping your InputStream in a InputStreamReader and that in a BufferedReader which should make getting text data much easier and more intuitive and less error prone.
- 01-27-2013, 03:17 PM #3
Member
- Join Date
- Jan 2013
- Posts
- 25
- Rep Power
- 0
Re: Reading content from an ftp file
Fubarable. Thank you very much! I changed the code based on your suggestion:
No, it is not working either:Java Code:public static void main(String [] args) throws IOException { FTPClient ftp = new FTPClient(); InputStream in = ftp.retrieveFileStream("ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KLAX.TXT"); InputStreamReader isr = new InputStreamReader(in); BufferedReader br = new BufferedReader(isr); int bytesRead = 0; byte[] buffer=new byte[1024]; String fileContent=null; fileContent=new String(buffer,0,bytesRead); System.out.println(fileContent); } }
When I open the file in IE, the file opens just fine, so the link is good. No idea what I am doing wrong! Thanks!Java Code:Exception in thread "main" java.lang.NullPointerException at org.apache.commons.net.SocketClient.getRemoteAddress(SocketClient.java:651) at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:732) at org.apache.commons.net.ftp.FTPClient._retrieveFileStream(FTPClient.java:1853) at org.apache.commons.net.ftp.FTPClient.retrieveFileStream(FTPClient.java:1844) at Test.main(Test.java:16)
-
Re: Reading content from an ftp file
If you create a BufferedReader, consider using that object to read in your data rather than simply ignoring it. As to your NPE, what is happening on line 16 of Test.java?
- 01-27-2013, 05:07 PM #5
Member
- Join Date
- Jan 2013
- Posts
- 25
- Rep Power
- 0
Re: Reading content from an ftp file
Thanks. New version:
Line 16 is pointing to the InputStream line (with the ftp address). I wonder if I am connecting to the ftp correctly...Java Code:public static void main(String [] args) throws IOException { FTPClient ftp = new FTPClient(); InputStream in = ftp.retrieveFileStream("ftp://tgftp.nws.noaa.gov/data/observations/metar/stations/KLAX.TXT"); InputStreamReader isr = new InputStreamReader(in); BufferedReader br = new BufferedReader(isr); String fileContent= br.readLine(); System.out.println(fileContent);
- 01-27-2013, 05:17 PM #6
Member
- Join Date
- Jan 2013
- Posts
- 25
- Rep Power
- 0
Re: Reading content from an ftp file
This worked!!!!!!!
Thanks for your support!Java Code:public class Test1 { public final static String server = "ftp://tgftp.nws.noaa.gov/data/observations/metar/stations"; public final static String fileName = "UAAA.TXT"; public static void main(String[] args) { System.out.println("Connecting to FTP server..."); // Connection String URL url; try { url = new URL(server + "/" + fileName); URLConnection con = url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); System.out.println("Reading file start."); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); } catch (FileNotFoundException e) { System.out.println("File not find on server."); System.exit(0); }catch (Exception e) { e.printStackTrace(); } System.out.println("Read File Complete."); } }
-
Re: Reading content from an ftp file
I would declare the BufferedReader before the try block setting it initially to null, and would close the BufferedReader in a finally block after checking first that it's not null. Otherwise it looks good.
- 01-28-2013, 12:23 AM #8
Member
- Join Date
- Jan 2013
- Posts
- 25
- Rep Power
- 0
Re: Reading content from an ftp file
I did it. Great. Thank you so much!
-
Similar Threads
-
Reading a text file into an Array and spliting the content into another Array
By jtothemax in forum New To JavaReplies: 15Last Post: 05-14-2012, 12:42 PM -
Reading structured content from PDF file
By chanduk in forum Advanced JavaReplies: 1Last Post: 12-09-2010, 01:03 PM -
how to read content of .xls file
By kirtichopra2003 in forum Advanced JavaReplies: 10Last Post: 09-11-2009, 01:03 PM -
Reading Body Content
By muhfak in forum JavaServer Faces (JSF)Replies: 1Last Post: 06-24-2008, 11:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks