Solved

How to reduce the size of File?

Posted on 1998-08-25
8
223 Views
Last Modified: 2010-03-30
In my project, I read/write the content of a file with RandomAccessFile.
Sometime, I need to truncate the unused part of the file and reduce the file's size.
I don't want to recreate a new file, because the file will be very big (~200MB)
Is there any method to resize an existing file?

Any idea?
0
Comment
Question by:threshold
  • 4
  • 4
8 Comments
 
LVL 8

Expert Comment

by:diakov
Comment Utility
Hi,
I'm afraid it is not possible to truncate file from java.
You have to define some abstract operation 'compact', which you will call when you decide that the internal fragmentation of the file is to big. And you have to create another file and delete the first one.
By the way, there are some very interesting algorythms for swap file managemet. For example, there is a way to swap large blocks of data within a file, without increasing it's size or increasing it with the most optimal size.

Regards,
  Nikolay
----------------------------
e-mail: diakov@theoffice.net
0
 
LVL 2

Author Comment

by:threshold
Comment Utility
I said, I am finding a way to reduce the size of a big file without recreating file.
The file is too big, I can't recreate another one again, just to reduce several MB s.
Maybe, there are no way in pure java, the native method that can work is acceptable.
0
 
LVL 2

Author Comment

by:threshold
Comment Utility
btw... My platform is NT 4.0 sp3
0
 
LVL 8

Accepted Solution

by:
diakov earned 200 total points
Comment Utility
Hi 'threshold',

OK, then, native calls.
For more information on JNI, go to:
http://java.sun.com/products/jdk/1.2/docs/guide/jni/spec/jniTOC.doc.html

Then, what you need is to implement a small shared .dll which implements the methods declared native and generated from 'javah'. Lets say you got the "c:\t.txt", which you want to truncate. You pass it to the method and there you have to open the file with 'GENERIC_WRITE' file access and then use a call to SetEndOfFile function. This function is in 'winbase.h', and is a kernel function. It is supported in NT. It does exactly what you want.

I wrote a small console application that truncates an existing file to 1 byte:

#include "windows.h"
#include "stdio.h"


void main()
{

      HANDLE h;
      
      h = CreateFile("c:\\t.txt",      // pointer to name of the file
    GENERIC_WRITE,      // access (read-write) mode
    0,      // share mode, 0 - cannot be shared
    NULL,      // pointer to security descriptor, not needed
    OPEN_EXISTING,      // how to create
    0,      // file attributes
    NULL      // handle to file with attributes to copy  
   );
      if (h == 0)
      {
            printf("Error openning the file!");
      }
      else
      {
            DWORD pos = SetFilePointer(
          h,      // handle of file
            1,      // number of bytes to move file pointer
            NULL,      // address of high-order word of distance to move  
            FILE_BEGIN       // how to move
            );
            if (pos == 0xFFFFFFFF)
            {
                  printf("Could not set file pos to 1.");
            }
            else
                   if (!SetEndOfFile(h))
                  {
                        printf("Could not truncate file!");
                  };

            CloseHandle(h);
      }
}

Success,
  Nik.

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:diakov
Comment Utility
Btw, this question appeared to be good one for section C++ :-)
0
 
LVL 2

Author Comment

by:threshold
Comment Utility
Thank you, diakov...

Maybe, I don't need to use JNI to truncate a file. All I need is a simple .exe file. I can use it in this way below.

c:> filetrancate c:\temp\myDATA.dat 323780000

Then, I can do the trancation in Java with Runtime.exec(...);

Can you modify it for me?
0
 
LVL 8

Expert Comment

by:diakov
Comment Utility
Hi 'threshold',
Not a bad idea. Here is your code.

//------------
#include "windows.h"
#include "stdio.h"

#define SUCCESS 0
#define ERR_INVALID_PARAMS 1
#define ERR_FILE_ERROR 2


int main( int argc, char *argv[])
{

  HANDLE h;
      
      
  //some parameter handling
  if (argc < 3)
  {      
    printf("Usage: filetruncate <full path> <new size>\n");
    printf("Usage: example - filetruncate c:\temp\myDATA.dat 323780000\n");
    return ERR_INVALID_PARAMS;
  }

  printf("Processing parameters: \'%s\', \'%s\' \n", argv[1], argv[2]);

  char *path = argv[1];
  long new_size = atol(argv[2]);
      
      
  h = CreateFile(path,      // pointer to name of the file
    GENERIC_WRITE,      // access (read-write) mode
    0,      // share mode, 0 - cannot be shared
    NULL,      // pointer to security descriptor, not needed
    OPEN_EXISTING,      // how to create
    0,      // file attributes
    NULL      // handle to file with attributes to copy  
   );
  if (h == 0)
  {
    printf("Error openning the file!\n");
    return ERR_FILE_ERROR;
  }
  else
  {
    DWORD pos = SetFilePointer(
    h,      // handle of file
    new_size,      // number of bytes to move file pointer
    NULL,      // address of high-order word of distance to move  
    FILE_BEGIN       // how to move
    );
    if (pos == 0xFFFFFFFF)
    {
      printf("Could not set file pos to 1.\n");
      return ERR_FILE_ERROR;
    }    
    else
      if (!SetEndOfFile(h))
      {
        printf("Could not truncate file!\n");
        return ERR_FILE_ERROR;
      };

    CloseHandle(h);
  }
  return SUCCESS;
}
//---------------------
Cheers,
  Nik
0
 
LVL 2

Author Comment

by:threshold
Comment Utility
Thanks, diakov. It's a big favor.
:-)

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now