Solved

How to reduce the size of File?

Posted on 1998-08-25
8
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java exception bubble up 2 26
Better way to make a string with template variables in java 3 52
java example issue 5 65
DTD and JAVA versions 1 55
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

732 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