• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Is it possible to truncate some data from end of file?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am doing one program, in which I need to truncate some data from file. Is there any possible way, to remove some bytes from binary file? If yes, then please help me in this thing...
Thank you very much,
Samir
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samir you can take advantage of the skip method of InputStreamReader class to skip reading the specified number of bytes at any place in a file or any other sink.

Here is a sample program in which i will skip reading bytes in the middle of the file.

Would u tell me the what program u are developing & y u need this functioanlity. I know how to do this but i havent yet used it practically anywhere.

<code>
import java.io.*;
class SkipBytes
{
public static void main(String args[]) throws IOException
{
InputStream byteSkipper = new FileInputStream("c:autoexec.bat");

int size = byteSkipper.available();
System.out.println("Total number of bytes to read " + size);

int n = size/40 ;
System.out.println("Reading " + n + " bytes");
for(int i=0;i<n;i++)>
{
System.out.print((char)byteSkipper.read());
}

System.out.println(" ");

n = size/20;
System.out.println("Skipping next " + n + " bytes " );
byteSkipper.skip(n);
System.out.println("Reading remaining bytes ");
n = size/40;
for(int i=0;i<n;i++)>
{
System.out.print((char)byteSkipper.read());
}
}
}
</code>
------------------
Sayal
 
Jahanzeb Sayal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in my previous reply i have used the loops for(int i=0;i<n;i++)>
i dont know why they are not formated correctly ... any how it is the correction!
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will the approach of re-writing to a new file work for you?
 
Jahanzeb Sayal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can this be done by rewriting a file?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic