Solved

How to reduce the size of File?

Posted on 1998-08-25
8
227 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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
How to execute a Python program and gather return output in Java 2 49
sql import cannot be resolved jsp 3 72
printf performancy 11 83
running on tomcat not jboss eap 7.0 3 28
In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

680 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