I'm reading the content from a file located on a server with the FTPClient from Apache Commons Net. It works fine when only reading once. But when I'm trying to read a second file, the InputStream of my FTPClient returns null. This is my code:
FTPClient ftpClient = new FTPClient();
ftpClient.connect("myhostname");
ftpClient.login("myusername", "mypassword");
// read InputStream from file
InputStream inputStream = ftpClient.retrieveFileStream("/my/firstfile.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
// read every line...
// close everything
inputStream.close();
bufferedReader.close();
// second try
inputStream = ftpClient.retrieveFileStream("/my/secondfile.txt");
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
// ...
inputStream.close();
bufferedReader.close();
What am I doing wrong?
After closing the InputStream, do the following:
ftpClient.completePendingCommand();
You can find more information in the javadoc of FTPClient#retrieveFileStream:
To finalize the file transfer you must call completePendingCommand and check its return value to verify success. If this is not done, subsequent commands may behave unexpectedly.
Related
I have a Problem with my Printwriter in some of my Projects. I always have to close it because otherwise the send message of the Serversockets doesn´t come to the wished server.
Socket socket = serverSocket.accept();
console.writeMessageinConsole("Client"+socket.getInetAddress()+" verbindet!");
OutputStream outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
PrintWriter printWriter = new PrintWriter(outputStream, true);
System.out.println("da");
printWriter.write("dadad");
printWriter.close();
Can you plase help me?
The only reason to use a BufferedWriter is to buffer the output. If you want to send immediately, don't use buffering.
The only reason to use a PrintWriter is to add println and printf. If you want to use write() you don't need a PrintWriter.
So you can either;
drop the BufferedWriter.
use println, or
flush() the output
I am trying to create an android client and java server application using socket programming. I need to retrieve the file send from java server but i dont want to write that content into another file in client side. Instead i want to create a listbox with the contents in the received file. I can find code for write these contents in a file but I dont know how to access the contents as strings.
Here is the code i tried:
Android client
client=new Socket("10.0.2.2", 7575);
writer=new PrintWriter(client.getOutputStream(),true);
writer.write(mMsg);
writer.flush();
writer.close();
InputStream is=client.getInputStream();
bytesread=is.read(mybytearray,0,mybytearray.length);
I changed my code as follws but it is not working.
InputStream is=client.getInputStream();
BufferedReader bf=new BufferedReader(new InputStreamReader(is));
String value=bf.readLine();
Wrap your inputstream into a BufferedReader
BufferedReader d
= new BufferedReader(new InputStreamReader(is));
and use readLine() method.
Reads a line of text. A line is considered to be terminated by any one
of a line feed ('\n'), a carriage return ('\r'), or a carriage return
followed immediately by a linefeed.
Try this:
InputStream is=client.getInputStream();
bytesread=is.read(mybytearray,0,mybytearray.length);
// Create a new String out of the bytes read
String data = new String(mybytearray, 0, bytesread, CharSet);
OR
Wrap the InputStream using BufferedReader and read the data line by line (using readLine()) in String format.
Using buffered reader is best or you can try the code below:
After the last statement use the byte[] "mybytearray" to convert to string as below:
if(bytesread > 0)
String result = new String(mybytearray);
How do I print to stdout the output string obtained executing a command?
So Runtime.getRuntime().exec() would return a Process, and by calling getOutputStream(), I can obtain the object as follows, but how do I display the content of it to stdout?
OutputStream out = Runtime.getRuntime().exec("ls").getOutputStream();
Thanks
I believe you are trying to get the output from process, and for that you should get the InputStream.
InputStream is = Runtime.getRuntime().exec("ls").getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader buff = new BufferedReader (isr);
String line;
while((line = buff.readLine()) != null)
System.out.print(line);
You get the OutputStream when you want to write/ send some output to Process.
Convert the stream to string as discussed in Get an OutputStream into a String and simply use Sysrem.out.print()
I have a file and I want to parse data from this file. I tried but I can't get data which I want. I tried to copy/paste of my .data file but it didn't work because of some character that are included in my file.
Link of my file http://bit.ly/11meGwG
I don't know which type of file is this?
How to decode this?
InputStream is = null;
is = getResources().openRawResource(R.raw.myfile);
InputStreamReader isr = null;
isr = new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
StringBuilder sb=new StringBuilder();
String line;
try {
while((line=br.readLine())!=null){
System.out.println(line);
sb.append(line+"\n");
}
isr.close();
You are using openRawResource(). This has nothing to do with your assets/ folder. openRawResource() is for your res/raw/ folder.
Use getAssets().open() to open a file within assets/.
I'm trying to read a text file line by line using InputStream from the assets directory in Android.
I want to convert the InputStream to a BufferedReader to be able to use the readLine().
I have the following code:
InputStream is;
is = myContext.getAssets().open ("file.txt");
BufferedReader br = new BufferedReader (is);
The third line drops the following error:
Multiple markers at this line
The constructor BufferedReader (InputStream) is undefinded.
What I'm trying to do in C++ would be something like:
StreamReader file;
file = File.OpenText ("file.txt");
line = file.ReadLine();
line = file.ReadLine();
...
What am I doing wrong or how should I do that? Thanks!
BufferedReader can't wrap an InputStream directly. It wraps another Reader. In this case you'd want to do something like:
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
A BufferedReader constructor takes a reader as argument, not an InputStream. You should first create a Reader from your stream, like so:
Reader reader = new InputStreamReader(is);
BufferedReader br = new BufferedReader(reader);
Preferrably, you also provide a Charset or character encoding name to the StreamReader constructor. Since a stream just provides bytes, converting these to text means the encoding must be known. If you don't specify it, the system default is assumed.
InputStream is;
InputStreamReader r = new InputStreamReader(is);
BufferedReader br = new BufferedReader(r);