[ad_1]
The day gone by, when I used to be having a look at loop over a record the use of CFLoop
, I got here throughout an previous put up of mine through which Raymond Camden discussed the fileSeek()
serve as. In all my years of ColdFusion, I have by no means used fileSeek()
– which permits us to leap to an arbitrary offset inside of a record. I consider that fileSeek()
works with each readable and writable recordsdata; on the other hand, the documentation on that is unclear. For this put up, I am having a look at the use of seekable learn recordsdata in ColdFusion.
With a view to use the fileSeek()
serve as, you will have to specify {that a} record is “seekable” whilst you name fileOpen()
. At that time, you’ll name fileSeek()
offering a 0-based, sure offset from the beginning of the record.
From the ColdFusion documentation, it is unclear if the offset is the choice of bytes or the choice of characters. If we take a look at the Java documentation for RandomAccessFile
, they outline .search()
as taking a choice of bytes. As such, I will suppose that ColdFusion could also be the use of the choice of bytes. This most likely has implications in case your record information incorporates multi-byte characters (like most of the emoji characters within the astral airplane).
ASIDE: Through having a look on the Lucee CFML source-code, it sort of feels that Lucee will create an example of
java.io.RandomAccessFile
on-the-fly whilst you try to namefileSeek()
for the primary time.
That stated, for this exploration, I am most effective going to handle vanilla, single-byte characters. In particular, the English alpha-numeric characters because the 26-letter alphabet and 10-digits give us simple offsets to play with.
Within the following ColdFusion code, I am merely going to open a seekable record, leap round to random-access issues, after which learn some information from the ones issues:
<cfscript>
// Create a demo record with a recognized development for checking out.
trail = expandPath( "./information.txt" );
information = (
// First 26 characters.
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" &
// 2d 26 characters, offset 26.
"abcdefghijklmnopqrstuvwxyz" &
// Final 10 characters, offset 52.
"1234567890"
);
fileWrite( trail, information, "utf-8" );
dataSource = fileOpen( trail, "learn", "utf-8", true ); // TRUE = Seekable.
take a look at {
echoLine( dataSource.learn( 3 ) );
echoLine( "Search to 26..." );
dataSource.search( 26 );
echoLine( dataSource.learn( 3 ) );
echoLine( "Search to 52..." );
dataSource.search( 52 );
echoLine( dataSource.learn( 3 ) );
echoLine( "Search to 0..." );
dataSource.search( 0 );
echoLine( dataSource.learn( 3 ) );
} in any case {
dataSource.shut();
}
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
public void serve as echoLine( required string worth ) {
writeOutput( worth & "<br />" );
}
</cfscript>
As you’ll see, I am leaping forward a couple of occasions sooner than in any case leaping again to the beginning of the record. And, once we run this ColdFusion code (in both Adobe ColdFusion or Lucee CFML), we get the next output:
ABC
Search to 26…
abc
Search to 52…
123
Search to 0…
ABC
As you’ll see, via the use of fileSeek()
, we have been in a position to leap round to arbitrary offsets throughout the record sooner than studying personality information. To be fair, I do not need a use-case for this that jumps to thoughts; however, now that I understand how this works, who is aware of what my mind will get a hold of.
Need to use code from this put up?
Take a look at the license.
[ad_2]