-1

I am working with a large number of files and I need to know the each file physical offset (address) in the hard disk or flash memory

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • Can you at least show us what you tried? or what you did? I can't get a clear idea of what exactly you want when you ask for physical address, are you looking for C:/directory/ or something more like @as2452s memory locations Usually when you create a class and don't add an overriding toString() method and use print or toString on that object, you will get it's memory location – ThaBomb Nov 10 '13 at 15:10
  • There are millions of tutorials already available online. What have you tried? – Tdorno Nov 10 '13 at 15:11
  • 1
    Are you implementing a hard disk driver? in Java? If not, you don't want the "physical address" of the file, but its location on the file system, which is returned by methods of the java.io.File class. – JB Nizet Nov 10 '13 at 15:13
  • 1
    Working on 'physical offset/address" o file or its chunk is for driver level programming. Why you need that for Java to operate on files? – Artur Nov 10 '13 at 15:13
  • In Computer Science, it is often better to ask for the answer you've given (see the other comments). Here are some links that may be helpful as a reference: http://stackoverflow.com/questions/6403795/how-can-i-find-the-physical-address-of-a-file – Lan Nov 10 '13 at 15:15
  • This question has a pretty distinct wrongness-smell over it, one being that in no OS I know of files are guaranteed to be stored contiguously, so not only you'd need an offset, but the whole storage layout. Plus, all that stuff depends on the filesystem being used (FAT/NTFS/HFS/EXTx and many many others) And with all qualities Java may have, that kind of work is not something I'd do in Java. – fvu Nov 10 '13 at 15:16

2 Answers2

1

Files don't have one physical offset. Instead they are made up of blocks (unless your file is small in which case there is one block) You can read the raw file system on Linux if you are root, e.g /dev/sd1c but how you do this depends on the file system you have.

I suggest you read up the wikipedia page for the filesystem you are using.

However, it is more than likely you don't know the actual location and the virtual location is all you need. To do this you open the file with FileInputStream/FileOutputStream/RandomAccessFile and memory map the file. This will map the file into continuous virtual memory and you can access the contents of the file as if it were in memory (if you access the whole file there is a good chance it will be cached in memory)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • As I understood, you mean that a file's physical address is saved in a structure like linked list. So is it possible to have all addresses in the linked list like structure so that we can access the whole file's address? – Mehdi Ijadnazar Apr 27 '16 at 16:46
  • @Mahdi A file doesn't have an address. You can memory map the file to get is one or more addresses, in multiple regions e.g. ByteBuffers and stitch these together. I would use an ArrayList to make random access easier. Or you could create one very large memory mapping if you know how big you want it to be from the start. – Peter Lawrey Apr 27 '16 at 17:30
  • Thanks for your response – Mehdi Ijadnazar Apr 27 '16 at 18:13
0

Java cannot do this. The problem is too low-level. You would need to use a systems programming language.

Robin Green
  • 32,079
  • 16
  • 104
  • 187