Large file read/write methods, file read/write Methods
Import java. io. *; class Test {public static void main (String args []) {FileInputStream FD = null; FileOutputStream fos = null; try {FD = new FileInputStream ("F: /Android/Java4Android/33/src/a.txt "); // assume that 1024 bytes are read each time, byte [] B = new byte [1024]; fos = new FileOutputStream (" F: /Android/Java4Android/33/src/B .txt "); // read the data of a large file multiple times using a loop while (true) {int bLen = FCM. read (B, 0, B. length); // when the program has read the data, return-1if (bLen =-1) {break;} fos. write (B, 0, bLen) ;}} catch (Exception e) {System. out. println (e);} finally {try {// close the IO stream. close (); fos. close ();} catch (Exception e) {System. out. println (e );}}}}
When the data of a file is large, it is impossible to read and retrieve all the data at a time by using word throttling. data can be read and written in multiple cycles. The same is true for ghost streams!
How to read large files
The following code example shows how to read binary data from a binary file. Two classes in the system. io namespace are used: filestream and binaryreader. Filestream indicates the actual file. Binaryreader provides an interface for streams that allow binary access. The following code example uses a file named data. bin created by compiling the code in a binary file. Example // binary_read.cpp // compile with:/clr # using <system. dll> using namespace system; using namespace system: io; int main () {string ^ filename = "data. bin "; try {filestream ^ fs = gcnew filestream (filename, filemode: open); binaryreader ^ br = gcnew binaryreader (fs); console :: writeline ("contents of {0}:", filename); while (br-> basestream-> position <br-> basestream-> length) console :: writeline (br-> readint32 (). tostring (); fs-> close ();} catch (exception ^ e) {if (dynamic_cast <filenotfoundexception ^> (e) console :: writeline ("file '{0}' not found", filename); else console: writeline ("exception: ({0})", e); return-1 ;} return 0 ;}★★Supplement★★A vb project on hand (found in the process ,. net is really good), you need to implement the http download function in an activex, is implemented using the internetreadfile api, the first code is to write the function gethttpdownload (surl as string) as boolen dim s as string dim hopen as long dim hopenurl as long dim bdoloop as boolean dim bret as boolean dim sreadbuffer as string * 2048 dim lnumberofbytesread as long hopen = internetopen (scuseragent, guest, vbnullstring, vbnullstring, 0) hopenurl = internetopenurl (hopen, surl, vbnullstring, 0, internet_flag_reload, 0) bdoloop = true do while bdoloop sreadbuffer = ...... remaining full text>
To process big data and read and write files larger than 4 GB, do not tell me how to use memory ing;
You can use CreateFile, WriteFile, and ReadFile to operate files larger than 4 GB.
The following code I have tested OK (create a 10g file on D: disk)
HANDLE file = CreateFile ("d: \ big. bin", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL );
If (file! = INVALID_HANDLE_VALUE)
{
DWORD cb;
BYTE * d = new BYTE [512*1024*1024];
For (int I = 0; I <20; I ++)
{
WriteFile (file, d, 512*1024*1024, & cb, NULL );
}
Delete [] d;
CloseHandle (file );
}
However, you must note that the file system of the target Logical Disk for storing files must be NTFS,
Because the FAT32 system itself does not support files larger than 4G