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:
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!
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.
Re: Reading content from an ftp file
Fubarable. Thank you very much! I changed the code based on your suggestion:
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:
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!
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?
Re: Reading content from an ftp file
Thanks. New version:
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...
Re: Reading content from an ftp file
This worked!!!!!!!
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!
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.
Re: Reading content from an ftp file
I did it. Great. Thank you so much!
Re: Reading content from an ftp file
Great and you're welcome!