Results 1 to 9 of 9
  1. #1
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default 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:
    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);
    		}
    }
    The code does not work, although all the links are correct. Thanks a lot for your help!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

  3. #3
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: Reading content from an ftp file

    Fubarable. Thank you very much! I changed the code based on your suggestion:

    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);
    	}
    }
    No, it is not working either:

    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)
    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!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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?

  5. #5
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: Reading content from an ftp file

    Thanks. New version:
    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);
    Line 16 is pointing to the InputStream line (with the ftp address). I wonder if I am connecting to the ftp correctly...

  6. #6
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: Reading content from an ftp file

    This worked!!!!!!!
    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.");
    
    		}
    
    }
    Thanks for your support!

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

  8. #8
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Default Re: Reading content from an ftp file

    I did it. Great. Thank you so much!

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Reading content from an ftp file

    Great and you're welcome!

Similar Threads

  1. Replies: 15
    Last Post: 05-14-2012, 01:42 PM
  2. Reading structured content from PDF file
    By chanduk in forum Advanced Java
    Replies: 1
    Last Post: 12-09-2010, 02:03 PM
  3. how to read content of .xls file
    By kirtichopra2003 in forum Advanced Java
    Replies: 10
    Last Post: 09-11-2009, 02:03 PM
  4. Reading Body Content
    By muhfak in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 06-24-2008, 12:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •