• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

skip() method of InputStream???

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.");
os.close();
// Send back a response message to the application
response.setContentType("text/plain");
PrintStream pos = new PrintStream(response.getOutputStream());
pos.println(tot);
}
}
In my Front end Interface:-
=========================
........
try
{
BufferedReader input = new BufferedReader (new InputStreamReader(uc.getInputStream ()));
int str;
while((str=input.read()) !=-1)
{
System.out.println(str);
}
}
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
is.skip(str);
// 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");
is.close();
.............
Regards
Bikash
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
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
Prashanth
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
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.
HTH.
Roopa
 
Bikash Paul
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Regards
Bikash
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic