jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes skip() method of InputStream??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "skip() method of InputStream???" Watch "skip() method of InputStream???" New topic

skip() method of InputStream???

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi all friends,
I am facing with one problem.Iam writing file to remote destination. front end is swing interface
and backend is servlet.Now I can stop writing of file in between and can start new file from scratch.But problem with restart that file which I stopped in between for that I take the length of remote file from server and now I want to
skip that portion which server already have.But I couldn't skip that portion it starts writing from
scratch again.Can any one plz tell me where I done a mistake.For that Iam pasting those portion of my codes:
In my servlet:-
// create a DataInputStream to read from sender
DataInputStream dis;
dis = new DataInputStream(request.getInputStream());
OutputStream os;
// create file output stream to write the output file
System.err.println("Using output file " + outfile);
os = new BufferedOutputStream(new FileOutputStream(outfile));
int cc;
byte [] buf = new byte[512];
long lim, tot = 0;
// read the number of bytes we should expect
lim = dis.readLong();
// read from the input stream and write to the file
try {
System.err.println("Reading " + lim + " bytes...");
for(cc = dis.read(buf, 0, 512);
cc > 0 && tot < lim;
cc = dis.read(buf, 0, 512))
os.write(buf, 0, cc);
tot += cc;
System.err.print("+" + cc + "(" + tot + ")");
if (tot >= lim) break;
catch (IOException ie) {
try { os.close(); } catch (Exception e2) { }
System.err.println("Exception during file transfer!");
// done reading and writing, close the file output stream
System.err.println("Read " + tot + " bytes.");
// Send back a response message to the application
PrintStream pos = new PrintStream(response.getOutputStream());
In my Front end Interface:-
BufferedReader input = new BufferedReader (new InputStreamReader(uc.getInputStream ()));
int str;
while((str=input.read()) !=-1)
catch(Exception serv)
System.out.println("Error from Servlet"+serv.toString());
try {
// step 7 - open the input stream
log("step 7 - open the input stream");
InputStream is = uc.getInputStream();
//skip the bytes already have in server
// step 8 - read the input stream
int cc;
byte [] buf = new byte[512];
log("step 8 - read the input stream");
for(cc = is.read(buf, 0, 512); cc > 0; cc = is.read(buf, 0, 512)) {
log(new String(buf, 0, cc));
// step 9 - close the input stream
log("step 9 - close the input stream");
Prashanth menon
Ranch Hand

Joined: Feb 20, 2001
Posts: 65
The FileOutputStream constructor used above will overwrite the existing contents.
I think you must use
os = new BufferedOutputStream(new FileOutputStream(outfile,true));
This will append to the existing file
roopa sudhi

Joined: Oct 31, 2001
Posts: 16
I think you can use RandomAccessFile and its methods.
get the length of the file and seek the position.
[seek(position)]now your file pointer will be at the point where you stopped.
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi Prashanth,
Thanks for ur guide.But for that I have to put condition in my servlet coz If I want to write a new file from scratch then this FileOutput constructor is ok
[os = new BufferedOutputStream(new FileOutputStream(outfile));]
If I want to append in that file which I stopped in between then ur suggested constructor will work.But I couldn't decide how I put condition for this two cases in my servlet.Can any one plz guide me for that.
I agree. Here's the link: http://aspose.com/file-tools
subject: skip() method of InputStream???
It's not a secret anymore!