Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Samir Patel
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
 
Jahanzeb Sayal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Tom Hennigan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
How can this be done by rewriting a file?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!