Solved

How to reduce the size of File?

Posted on 1998-08-25
8
226 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
ID: 1222578
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
ID: 1222579
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
ID: 1222580
btw... My platform is NT 4.0 sp3
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 8

Accepted Solution

by:
diakov earned 200 total points
ID: 1222581
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
 
LVL 8

Expert Comment

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

Author Comment

by:threshold
ID: 1222583
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
ID: 1222584
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
ID: 1222585
Thanks, diakov. It's a big favor.
:-)

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 51
diffSum example 4 50
How to configure empty element in XML Document parser? 15 36
java mysql insert application 14 26
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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

839 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