A file reading method and apparatus

By matching the split file data with a buffer of set length and feature strings, the problems of data incompleteness and memory overflow in the prior art are solved, and efficient file reading and segmentation are achieved.

CN116150043BActive Publication Date: 2025-08-01CCB FINTECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202211478058.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-11-23
Publication Date
2025-08-01
Estimated Expiration
2042-11-23

AI Technical Summary

Technical Problem

In the prior art, when reading a file, the data may be incomplete when splitting data using newlines, and when using regular matching, it may cause memory overflow, and it is impossible to efficiently read and split file data in a small amount of memory.

Method used

Use a buffer of set length to read file data, and divide data through feature string matching, and use buffered arrays and indexes to optimize the matching process to avoid memory overflow.

Benefits of technology

It realizes efficient reading and segmentation of file data under a small amount of memory, avoids data incompleteness and memory overflow problems, and improves processing efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116150043B_ABST
    Figure CN116150043B_ABST
Patent Text Reader

Abstract

A file reading method and apparatus, which relate to the field of computer data processing and are used to quickly and efficiently read and split data in a file while using a small amount of memory. The method includes: reading a string of a set length from a file to be read located in the system memory into a set buffer in the application memory; matching the string in the set buffer with a feature string; the feature string includes a plurality of specified characters; if the match is successful, returning the string in the set buffer before the feature string as a piece of data to the application.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the technical field of computer data processing, and in particular, to a file reading method and apparatus. Background Art

[0002] In daily development, there is often a need to read data from a file for processing.

[0003] In the prior art, a method for reading a file is as follows: Each character in the file is sequentially read into the application memory, and it is sequentially determined whether the current character is a line feed character. If so, the data between the two line feed characters is output as a line of data. However, this way of reading a file only supports splitting data by line feed characters. If the data itself contains a line feed character, then each time the read data is not a complete piece of data.

[0004] Another method for reading a file is as follows: All the data in the file is read into the application memory, and the delimiter in the file is matched in a regular expression matching manner. When the delimiter is matched, the data on both sides of the delimiter is split. However, this way of reading a file will read a large amount of data at one time, and when the file size is very small, it may cause an out of memory (OOM) situation.

[0005] Therefore, there is an urgent need for a solution to quickly and efficiently read and split the data in a file with a small amount of memory used. Summary of the Invention

[0006] This application provides a file reading method and apparatus for quickly and efficiently reading and splitting the data in a file with a small amount of memory used.

[0007] In a first aspect, an embodiment of this application provides a file reading method, which includes: Reading a string with a set length from a file to be read located in the system memory into a set buffer area in the application memory; Matching the string in the set buffer area with a feature string; The feature string includes a plurality of specified characters; If the matching is successful, the string in the set buffer area before the feature string is returned to the application as a piece of data.

[0008] In the above technical solution, on the one hand, data can be split according to the specified feature string, avoiding the problem that when splitting data by line feed characters, when a piece of data itself contains a line feed character, a piece of data cannot be read completely. On the other hand, this solution can read and process a large amount of data with a small amount of memory used, avoiding the situation that when reading an extremely large file, all the data in the file is read into the application memory at one time, thereby causing an out of memory situation.

[0009] In a possible design, if the matching fails, a partial string is taken out from the set buffer for caching, and the step of reading a string of a set length from a file to be read in the system memory into the set buffer in the application memory is continued until the string in the set buffer matches the feature string successfully or the file to be read is completely read; when the matching is successful, the string before the feature string in the set buffer and the partial string are returned to the application as a piece of data.

[0010] In the above technical solution, since the size of the set buffer is limited, if the string in the set buffer does not match the feature string all the time, no data can be read into the set buffer after it is full. Therefore, it is necessary to take out a partial string from the set buffer for caching; and taking out a partial string for caching, that is, retaining a partial string in the buffer, can avoid the situation that when the last few bits of data in the set buffer include partial characters of the feature string, the data in the set buffer is directly output as a whole, resulting in missing the feature string.

[0011] In a possible design, the size of the set buffer is N and it is divided into a first part and a second part; the string of the set length occupies N / 2 of the size of the set buffer; the step of reading a string of a set length from a file to be read in the system memory into the set buffer in the application memory includes: reading a string of a set length from a file to be read in the system memory; writing the string of the set length into the first part or the second part according to the set writing order.

[0012] In a possible design, before taking out a partial string from the set buffer for caching, it further includes: determining whether there is a remaining string in the first part of the set buffer; the step of taking out a partial string from the set buffer for caching includes: if there is a remaining string in the first part of the set buffer, taking out and caching the remaining string in the first part as the partial string.

[0013] In a possible design, the step of taking out a partial string from the set buffer for caching includes: taking out a partial string from the set buffer and caching it in a string builder; the step of returning the string before the feature string in the set buffer and the partial string to the application as a piece of data includes: splicing the partial string cached in the string builder and the string before the feature string in the set buffer into a piece of data and then returning it to the application.

[0014] In a possible design, the set buffer is a buffer array, and the buffer array has a start index and an end index; the start index is used to indicate the position of the starting character in the buffer array; the end index is used to indicate the position of the character to be matched in the buffer array; taking out the string before the feature string from the set buffer and returning it to the application includes: taking out the string excluding the feature string between the start index and the end index from the buffer array and returning it to the application; moving the start index and the end index to the next character of the feature string.

[0015] In the above technical solution, when reading a file, a buffer array is used to temporarily store the part of the string being processed, and the start index and the end index are used to indicate the position of the starting character and the position of the character to be matched in the buffer array respectively. In this way, when matching the feature string, the end index is moved each time a character is matched, which can avoid repeated comparison of characters, and thus can occupy less CPU time and have higher processing efficiency.

[0016] In a second aspect, an embodiment of the present application provides a file reading device, including:

[0017] A reading and writing module, configured to read a string of a set length from a file to be read located in the system memory into a set buffer in the application memory;

[0018] A matching module, configured to match the string in the set buffer with a feature string; the feature string includes a plurality of specified characters;

[0019] A processing module, configured to, if the matching is successful, return the string before the feature string in the set buffer to the application as a piece of data.

[0020] In a possible design, the reading and writing module is further configured to, if the matching is unsuccessful, take out a part of the string from the set buffer for caching, and continue to execute the step of reading a string of a set length from the file to be read located in the system memory into the set buffer in the application memory until the string in the set buffer matches the feature string successfully or the file to be read is read completely; the processing module is configured to, when the matching is successful, return the string before the feature string in the set buffer and the part of the string to the application as a piece of data.

[0021] In a possible design, the size of the set buffer is N and it is divided into a first part and a second part; the string of the set length occupies N / 2 of the size of the set buffer; the reading and writing module is further configured to read a string of a set length from a file to be read located in the system memory; and write the string of the set length into the first part or the second part according to a set writing order.

[0022] In a possible design, the processing module is further configured to determine whether there is a remaining string in the first part of the set buffer; if there is a remaining string in the first part of the set buffer, then take out and cache the remaining string in the first part as a partial string.

[0023] In a possible design, the processing module is further configured to take out a partial string from the set buffer and cache it in a string builder; the processing module is further configured to splice the partial string cached in the string builder and the string before the feature string in the set buffer into a piece of data and then return it to the application.

[0024] In a possible design, the set buffer is a buffer array, and the buffer array has a start index and an end index; the start index is used to indicate the position of the starting character in the buffer array; the end index is used to indicate the position of the character for matching in the buffer array; the processing module is further configured to take out and return the string except the feature string between the start index and the end index from the buffer array to the application; and move the start index and the end index to the next character of the feature string.

[0025] In a third aspect, an embodiment of the present application further provides a computing device, including:

[0026] A memory for storing program instructions;

[0027] A processor for calling the program instructions stored in the memory and executing the method described in any possible design of the first aspect according to the obtained program instructions.

[0028] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium, in which computer-readable instructions are stored. When a computer reads and executes the computer-readable instructions, the method described in any possible design of the first aspect is implemented.

[0029] In a fifth aspect, an embodiment of the present application further provides a computer program product, including computer-readable instructions. When the computer-readable instructions are executed by a processor, the method described in any possible design of the first aspect is implemented. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] To more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for the description of the embodiments. Obviously, the drawings in the following description are only some embodiments of the present application. For those of ordinary skill in the art, without creative efforts, other drawings can be obtained based on these drawings.

[0031] Figure 1 It is a schematic flowchart of a file reading method provided by an embodiment of the present application;

[0032] Figure 2 It is a schematic diagram of a specific process for reading a file provided by an embodiment of the present application;

[0033] Figure 3 It is a specific example provided by an embodiment of the present application;

[0034] Figure 4 It is a schematic structural diagram of a file reading device provided by an embodiment of the present application;

[0035] Figure 5 It is a schematic structural diagram of a computing device provided by an embodiment of the present application. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0036] To make the objectives, technical solutions and advantages of the present application clearer, the following further describes the present application in detail with reference to the drawings. Obviously, the described embodiments are only some embodiments of the present application, rather than all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative efforts fall within the protection scope of the present application.

[0037] In the embodiments of the present application, "a plurality of" means two or more. Terms such as "first" and "second" are only used for the purpose of distinguishing descriptions, and cannot be understood as indicating or implying relative importance, nor can they be understood as indicating or implying order.

[0038] First, introduce the application scenario of the embodiments of the present application. When an application needs to use a certain file, it will first read the file into the system memory, and then read the data in the file from the system memory into the application memory for processing.

[0039] In the prior art, a method for reading a file is as follows: Use the readline method of BufferedReader to sequentially read each character of the file into the application memory in the form of a byte stream, and sequentially determine whether the current character is a line feed character. If so, output the data between the previous line feed character and the current line feed character as a line. However, using the readline method of BufferedReader only supports splitting data by line feed characters. If the data itself contains line feed characters, then each time the read data is not a complete piece of data.

[0040] Another method for reading a file is as follows: Read the entire file into the application memory and use the split method of String to split the existing string, that is, split each piece of data from the existing string using regular matching. The split method of String is suitable for splitting strings. If it is needed to split a file, all the file content needs to be read into the memory at one time. Therefore, it is only applicable to the case where the file size is very small. When the file size is very large, it will cause an out-of-memory error.

[0041] Based on the above disadvantages of the prior art, the present application provides a file reading method to quickly and efficiently read and split the data in a file with a small amount of memory used.

[0042] Figure 1 Exemplarily, a flowchart of a file reading method provided by an embodiment of the present application is shown, as Figure 1 shown. The method includes the following steps:

[0043] Step 101: Read a string of a set length from the file to be read located in the system memory into a set buffer area in the application memory.

[0044] In an embodiment of the present application, the file type of the file to be read can be flat data files such as txt, csv, tsv, dat, etc. The file to be read can be read into the set buffer area in the application memory in the form of a data stream. Among them, the set buffer area is used to temporarily store a partial string to be processed in the file to be read.

[0045] Step 102: Match the string in the set buffer area with a feature string.

[0046] Among them, the feature string is used to separate two pieces of data, and the feature string can include multiple specified characters. The feature string can be artificially inserted or automatically inserted by a program between two pieces of data when the file to be read is created.

[0047] It should be noted that, in order to avoid the situation where a feature string is contained in a piece of data itself, resulting in an incomplete piece of data being returned to the application during reading, when setting the feature string in this application, multiple uncommon characters are usually specified to form the feature string. For example, the feature string can be set as #@#\, %#@\ or #@%&@*. This application does not make specific limitations on this, and the length of the feature string, the characters included, and the order between characters can be adjusted according to actual needs.

[0048] Step 103: If the match is successful, the string in the set buffer before the feature string is returned to the application as a piece of data.

[0049] If the match is unsuccessful, part of the string is taken out from the set buffer for caching, and step 101 is continued to read a string of a set length from the file to be read in the system memory into the set buffer in the application memory until the string in the set buffer matches the feature string or the file to be read is completely read.

[0050] When the match is successful, the string in the set buffer before the feature string and the previously cached part of the string are returned to the application as a piece of data. Or, when the file to be read is completely read, the remaining string in the set buffer and the previously cached part of the string are returned to the application as a piece of data.

[0051] [[ID=1))

[0052] In the above technical solution, since the size of the set buffer is limited, if the string in the set buffer never matches the feature string, no more data can be read into the set buffer after it is full. Therefore, part of the string needs to be taken out from the set buffer for caching; and taking out part of the string for caching, that is, retaining part of the string in the set buffer, can avoid the situation where when the last few bits of data in the set buffer include some characters of the feature string, the entire data in the set buffer is directly output, resulting in the omission of the feature string.

[0053] ​For example, assume that the feature string is #@#\, and the file to be read is ABCDEFGH#@#\IJKLMN. If the string in the set buffer is ABCDEFGH#@ at this time, if all the string ABCDEFGH#@ in the set buffer is output, the feature string #@#\ after H cannot be recognized. Therefore, it is necessary to take out a part of the string from the set buffer for caching. For example, take out the first half ABCDE for caching, and there is still FGH#@ left in the set buffer. Then read #\IJK into the set buffer. At this time, the string in the set buffer is FGH#@#\IJK. When performing feature string matching at this time, the feature string #@#\ after H can be recognized, and thus the situation of missing feature strings will not occur.

[0054] In step 101 above, there are multiple ways to read a string of a set length from the file to be read located in the system memory into the set buffer in the application memory.

[0055] In one example, the size of the set buffer is N and it is divided into a first part and a second part, and the string of the set length occupies N / 2 of the size of the set buffer.

[0056] Reading a string of a set length from the file to be read located in the system memory into the set buffer in the application memory includes: reading a string of a set length from the file to be read located in the system memory, and writing the string of the set length into the first part or the second part according to the set writing order.

[0057] Specifically, when starting to read the file, the set buffer is empty. Read N / 2 of the string from the file to be read into the first part of the set buffer, and determine whether the string in the set buffer matches the feature string. If the feature string is matched, the string before the feature string in the set buffer is returned to the application as a piece of data; if there is no string in the set buffer that matches the feature string, read N / 2 of the string from the file to be read into the second part of the set buffer, and continue to match the feature string starting from the position where the feature string was last matched. If it matches, the string before the feature string in the set buffer is returned to the application as a piece of data.

[0058] If it does not match, determine whether there is a remaining string in the first part of the set buffer. If there is a remaining string, cache the remaining string in the first part of the set buffer, copy the string in the second part to the first part, and then read N / 2 of the string from the file to be read into the second part of the set buffer, and continue to match the feature string starting from the position where the feature string was last matched.

[0059] If there is no remaining string in the first part of the buffer, directly copy the remaining string in the second part to the first part, then read N / 2 strings from the file to be read into the second part of the set buffer, and continue to match the feature string starting from the position where the feature string was last matched.

[0060] In another example, the size of the set buffer is N and it is divided into a first part, a second part, and a third part, and the string of the set length occupies N / 3 of the size of the set buffer.

[0061] Specifically, when starting to read the file, the set buffer is empty. Read N / 3 strings from the file to be read into the first part, the second part, and the third part of the set buffer respectively, and determine whether the strings in the set buffer match the feature string. If the feature string is matched, return the string before the feature string in the set buffer as a piece of data to the application;

[0062] If there is no string in the set buffer that matches the feature string, determine whether there is any remaining string in the first part and the second part of the set buffer. If there is remaining string, cache the remaining strings in the first part and the second part of the set buffer, copy the string in the third part to the first part, and then read N / 3 strings from the file to be read into the second part and the third part of the set buffer respectively, and continue to match the feature string starting from the position where the feature string was last matched.

[0063] If there is no remaining string in the first part and the second part of the buffer, directly copy the remaining string in the third part to the first part, and then read N / 3 strings from the file to be read into the second part and the third part of the set buffer respectively, and continue to match the feature string starting from the position where the feature string was last matched.

[0064] In yet another example, the size of the set buffer is N, the length of the feature string is M, and the string of the set length occupies N - M of the size of the set buffer.

[0065] Specifically, each time N - M strings are read into a set buffer array. When reading for the (i + 1)-th time, if the remaining strings in the strings read in the i-th time in the set buffer array are less than or equal to M, then move the remaining strings in the strings read in the i-th time to the starting position of the buffer array, and then read N - M strings from the file to be read into the set buffer array, and continue to match the feature strings starting from the position where the feature strings were last matched; if the remaining strings in the strings read in the i-th time in the set buffer array are greater than M, then retain the last M characters in the strings read in the i-th time, cache the remaining strings in the set buffer except the last M characters, move the last M characters to the starting position of the buffer array, and then read N - M strings from the file to be read into the set buffer, and continue to match the feature strings starting from the position where the feature strings were last matched.

[0066] In a possible implementation manner, the set buffer is a buffer array, and the buffer array has a start index and an end index. The start index is used to indicate the position of the starting character in the buffer array; the end index is used to indicate the position of the character for matching in the buffer array. Taking out the strings before the feature strings from the set buffer and returning them to the application includes: taking out the strings except the feature strings between the start index and the end index from the buffer array and returning them to the application, and then moving the start index and the end index to the next character of the feature strings.

[0067] It can be understood that when multiple files are read concurrently, each file corresponds to its own buffer array, feature strings, and string builder.

[0068] To better explain the embodiments of the present application, taking the size of the set buffer as N and the strings of the set length occupying N / 2 of the size of the set buffer as an example, the specific process of reading the file will be described below.

[0069] Figure 2 Exemplarily, a schematic diagram of a specific process of reading a file provided by the embodiments of the present application is shown, and the specific steps are as follows.

[0070] Step 201: Read N / 2 strings into the first part of the buffer array.

[0071] When starting to read the file, the buffer array is empty, and N / 2 strings are read from the file to be read into the first part of the buffer array. The initial values of the start index and the end index are both 0, and both point to the first character in the buffer array.

[0072] Step 202: Start matching the feature strings from the position indicated by the end index.

[0073] Starting from the position indicated by the end index (i.e., starting from the last character that matched the feature string), sequentially traverse the strings in the buffer array to check if they match the feature string. For each matched string, increment the end index by 1.

[0074] Step 203, determine whether the feature string is matched.

[0075] Determine whether the string in the buffer array matches the feature string. If so, execute step 204; otherwise, execute step 206.

[0076] Step 204, return the string before the feature string in the buffer array to the application as a piece of data.

[0077] Specifically, the string between the start index and the end index in the buffer array (excluding the feature string) can be input into the string builder, and the string builder returns this string to the application as a piece of data.

[0078] Step 205, move the start index and the end index to the next character of the feature string.

[0079] Move the start index and the end index in the buffer array to the next character of the matched feature string, and then repeat step 202.

[0080] It should be noted that the execution order between step 204 and step 205 is not sequential. Step 204 can be executed first and then step 205, or step 205 can be executed first and then step 204.

[0081] Step 206, read N / 2 strings into the second part of the buffer array.

[0082] Step 207, start matching the feature string from the position indicated by the end index.

[0083] Step 208, determine whether the feature string is matched.

[0084] Determine whether the string in the buffer array matches the feature string. If so, execute step 209; otherwise, execute step 211.

[0085] Step 209, return the string before the feature string in the buffer array to the application as a piece of data.

[0086] Specifically, the string between the start index and the end index in the buffer array (excluding the feature string) can be input into the string builder, and the string builder returns the previously cached string and this string concatenated as a piece of data to the application.

[0087] Step 210, move the start index and the end index to the next character of the feature string.

[0088] It should be noted that the execution order between Step 209 and Step 210 is not in a specific sequence. One can execute Step 209 first and then Step 210, or execute Step 210 first and then Step 209.

[0089] Step 211, determine whether the first part of the buffer array is empty.

[0090] Determine whether the first part of the buffer array is empty; if so, execute Step 213; otherwise, execute Step 212.

[0091] Step 212, cache the string in the first part of the buffer array.

[0092] Specifically, the string in the first part of the buffer array can be input into a string builder for caching.

[0093] Step 213, copy the string in the second part of the buffer array to the first part of the buffer array.

[0094] After executing Step 213, continue to execute Step 206 until the content in the file is completely read.

[0095] To better explain the embodiments of the present application, a specific example is given below.

[0096] As Figure 3 shown, assume the data in the file to be read is: ACDE#@#\FGH#@#\IJKLMNOPQ#@#\RSTUVWXYZABCDEFGHIJKLMNOPQRS#@#\TUVWXYZ. And assume the size of the buffer is set to 40 characters, and each time 1 / 2 of the set buffer size of data is read from the file to be read, that is, 20 characters are read from the file to be read each time.

[0097] For the first read, 20 characters ABCDE#@#\FGH#@#\IJKL are read from the file to be read into the first part of the buffer array. Determine whether the string in the buffer array matches the feature string. After matching the feature string #@#\, the string ABCDE before the feature string is output to the string builder, and the string builder returns this string ABCDE as a piece of data to the application. At this time, the remaining string FGH#@#\IJKL is in the buffer array.

[0098] Continue to match the string in the buffer array with the feature string starting from character F. After matching the feature string #@#, output the string FGH before the feature string to the string builder. The string builder returns this string FGH as a piece of data to the application. At this time, the remaining string in the buffer array is IJKL. Then continue to match the string in the buffer array with the feature string starting from character I. Since the feature string #@# is not matched at this time, continue to read the file to be read.

[0099] During the second read, continue to read 20 characters MNOPQ#@#\RSTUVWXYZAB from the file to be read into the second part of the buffer array. At this time, the string in the buffer array is IJKLMNOPQ#@#\RSTUVWXYZAB. Determine whether the string in the buffer array matches the feature string. After matching the feature string #@#, output the string IJKLMNOPQ before the feature string to the string builder. The string builder returns this string IJKLMNOPQ as a piece of data to the application. At this time, the remaining string in the second part of the buffer array is RSTUVWXYZAB, and the first part of the buffer array is empty. Move the remaining string RSTUVWXYZAB in the second part to the first part of the buffer array, and continue to read the file to be read.

[0100] During the third read of the file, continue to read 20 characters CDEFGHIJKLMNOPQRS#@# from the file to be read into the second part of the buffer array. At this time, the string in the buffer array is RSTUVWXYZABCDEFGHIJKLMNOPQRS#@#. Determine whether the string in the buffer array matches the feature string. At this time, there is no matching string in the buffer array, and there are still remaining strings in the first part of the buffer array. Therefore, buffer the remaining string RSTUVWXYZAB in the first part of the buffer array to the string builder. Since the feature string is not matched, the string builder does not return data to the application at this time. Then move the remaining string CDEFGHIJKLMNOPQRS#@# in the second part of the buffer array to the first part of the buffer array, and continue to read the file to be read.

[0101] When reading the file for the fourth time, the remaining characters \TUVWXYZ in the file to be read are read into the second part of the buffer array. At this time, the string in the buffer array is CDEFGHIJKLMNOPQRS#@#\TUVWXYZ. Determine whether the string in the buffer array matches the feature string. After matching the feature string #@#\, the string CDEFGHIJKLMNOPQRS before the feature string is output to the string builder. At this time, the string builder stores the string RSTUVWXYZABCDEFGHIJKLMNOPQRS. The string builder returns this string RSTUVWXYZABCDEFGHIJKLMNOPQRS to the application as a piece of data. At this time, the remaining string in the buffer array is TUVWXYZ, and the feature string #@#\ is not matched, and all the data in the file to be read has been read into the buffer array. Therefore, the remaining string TUVWXYZ in the first part of the buffer array is output to the string builder, and the string builder returns this string TUVWXYZ to the application as a piece of data.

[0102] The present application provides a file reading method. On the one hand, it can split data according to a specified feature string, avoiding the problem that when using a newline character to split data, when a piece of data itself contains a newline character, it is impossible to read a piece of data completely. On the other hand, this solution can read and process a large amount of data with a small amount of memory, avoiding the situation where all the data in the file is read into the application memory at one time when reading an extremely large file, which may lead to an out-of-memory situation.

[0103] Based on the same technical concept, Figure 4 Exemplarily, a file reading device provided by an embodiment of the present application is shown. As Figure 4 shown, the device 400 includes:

[0104] A reading and writing module 401, configured to read a string of a set length from a file to be read located in the system memory into a set buffer area in the application memory;

[0105] A matching module 402, configured to match the string in the set buffer area with a feature string; the feature string includes a plurality of specified characters;

[0106] A processing module 403, configured to, if the matching is successful, return the string before the feature string in the set buffer area to the application as a piece of data.

[0107] In a possible design, the reading and writing module 401 is further configured to, if the matching fails, take out a partial string from the set buffer for caching, and continue to execute the step of reading a string of a set length from a file to be read located in the system memory into the set buffer in the application memory until the string in the set buffer matches the feature string or the file to be read is completely read; the processing module 403 is configured to, when the matching is successful, return the string before the feature string and the partial string in the set buffer to the application as a piece of data.

[0108] In a possible design, the size of the set buffer is N and it is divided into a first part and a second part; the string of the set length occupies N / 2 of the size of the set buffer; the reading and writing module 401 is further configured to read a string of a set length from a file to be read located in the system memory; and write the string of the set length into the first part or the second part according to a set writing order.

[0109] In a possible design, the processing module 403 is further configured to determine whether there is a remaining string in the first part of the set buffer; if there is a remaining string in the first part of the set buffer, take out and cache the remaining string in the first part as a partial string.

[0110] In a possible design, the processing module 403 is further configured to take out a partial string from the set buffer and cache it in a string builder; the processing module 403 is further configured to splice the partial string cached in the string builder and the string before the feature string in the set buffer into a piece of data and then return it to the application.

[0111] In a possible design, the set buffer is a buffer array, and the buffer array has a start index and an end index; the start index is used to indicate the position of the starting character in the buffer array; the end index is used to indicate the position of the character for matching in the buffer array; the processing module 403 is further configured to take out and return the string except the feature string between the start index and the end index from the buffer array to the application; and move the start index and the end index to the next character of the feature string.

[0112] Based on the same technical concept, an embodiment of the present application provides a computing device, as Figure 5 shown, including at least one processor 501 and a memory 502 connected to at least one processor. In the embodiment of the present application, the specific connection medium between the processor 501 and the memory 502 is not limited. Figure 5Take the example where the middle processor 501 and the memory 502 are connected via a bus. The bus can be divided into an address bus, a data bus, a control bus, etc.

[0113] In the embodiment of the present application, the memory 502 stores instructions executable by at least one processor 501. By executing the instructions stored in the memory 502, the at least one processor 501 can execute the above file reading method.

[0114] Among them, the processor 501 is the control center of the computing device. It can use various interfaces and lines to connect various parts of the computer device. By running or executing the instructions stored in the memory 502 and calling the data stored in the memory 502, resource settings can be performed.

[0115] Optionally, the processor 501 may include one or more processing units. The processor 501 may integrate an application processor and a modem processor. Among them, the application processor mainly processes the operating system, user interface, application programs, etc., and the modem processor mainly processes wireless communication. It can be understood that the above modem processor may not be integrated into the processor 501. In some embodiments, the processor 501 and the memory 502 can be implemented on the same chip. In some embodiments, they can also be separately implemented on independent chips.

[0116] The processor 501 can be a general-purpose processor, such as a central processing unit (CPU), a digital signal processor, an application specific integrated circuit (ASIC), a field programmable gate array, or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, and can implement or execute the various methods, steps, and logic block diagrams disclosed in the embodiments of the present application. The general-purpose processor can be a microprocessor or any conventional processor, etc. The steps of the method disclosed in combination with the embodiments of the present application can be directly embodied as being executed by a hardware processor, or executed by a combination of hardware and software modules in the processor.

[0117] The memory 502, as a non-volatile computer-readable storage medium, can be used to store non-volatile software programs, non-volatile computer-executable programs, and modules. The memory 502 can include at least one type of storage medium. For example, it can include flash memory, hard disks, multimedia cards, card-type memories, random access memory (RAM), static random access memory (SRAM), programmable read-only memory (PROM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), magnetic memories, magnetic disks, optical disks, and so on. The memory 502 is any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited thereto. The memory 502 in the embodiments of the present application can also be a circuit or any other device capable of implementing a storage function, for storing program instructions and / or data.

[0118] Based on the same technical concept, the embodiments of the present application also provide a computer-readable storage medium, which stores a computer-executable program for causing a computer to execute the file reading method listed in any of the above manners.

[0119] Based on the same technical concept, the embodiments of the present application also provide a computer program product, including computer-readable instructions, which, when executed by a processor, cause the file reading method listed in any of the above manners to be implemented.

[0120] Those skilled in the art should understand that the embodiments of the present application can be provided as a method, a system, or a computer program product. Therefore, the present application can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present application can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk memories, CD-ROMs, optical memories, etc.) that contain computer-usable program code.

[0121] This application is described with reference to the flowcharts and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the application. It should be understood that each flow and / or block in the flowchart and / or block diagram, and the combination of flows and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to the processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing device to generate a machine, such that the instructions executed by the processor of the computer or other programmable data processing device produce a means for implementing the specified functions in the process Figure 1 one process or multiple processes and / or blocks Figure 1 or a means for implementing the specified functions in multiple blocks.

[0122] These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, such that the instructions stored in the computer-readable memory produce a manufactured article including an instruction means that implements the specified functions in the process Figure 1 one process or multiple processes and / or blocks Figure 1 or a means for implementing the specified functions in multiple blocks.

[0123] These computer program instructions can also be loaded onto a computer or other programmable data processing device, such that a series of operation steps are executed on the computer or other programmable device to generate a computer-implemented process, and thus the instructions executed on the computer or other programmable device provide steps for implementing the specified functions in the process Figure 1 one process or multiple processes and / or blocks Figure 1 or a means for implementing the specified functions in multiple blocks.

[0124] Although the preferred embodiments of the application have been described, those skilled in the art can make additional changes and modifications once they learn the basic creative concepts. Therefore, the appended claims are intended to be construed as including the preferred embodiments and all changes and modifications that fall within the scope of the application.

[0125] Obviously, those skilled in the art can make various changes and modifications to the application without departing from the spirit and scope of the application. Thus, if these modifications and variations of the application fall within the scope of the claims of the application and their equivalent technologies, the application is also intended to include these changes and modifications.

Claims

1. A file reading method, characterized in that, The method includes: Reading a string of a set length from a file to be read located in the system memory into the first part of a set buffer in the application memory; Matching all the strings in the set buffer with a feature string; the feature string includes a plurality of specified characters; if the match is unsuccessful, the string in the first part of the set buffer is cached to a string builder; if the match is successful, the string in front of the feature string in the set buffer is returned to the application through the string builder, and the remaining strings in the set buffer are repeatedly matched and the data is returned until the feature string cannot be matched or there are no remaining strings in the set buffer; Continuing to read a string of a set length from the file to be read into the second part of the set buffer, and repeatedly performing the above steps of matching, caching, and returning data. After completion, if there are still strings in the first part, the strings in the first part are cached to the string builder, and the strings in the second part of the set buffer are moved to the first part of the set buffer, and the above steps of reading data, matching, caching, and returning data are repeatedly performed until all the strings in the file to be read are read.

2. The method according to claim 1, characterized in that, The returning the string in front of the feature string in the set buffer to the application through the string builder includes: Concatenating the string originally cached in the string builder, the string in front of the feature string in the set buffer, and the originally cached string into a piece of data and returning it to the application.

3. The method according to claim 1, characterized in that, The lengths of the first part and the second part are both half of the length of the set buffer, and the set length is equal to the length of the first part.

4. The method according to claim 1, wherein The reading the string of a set length into the set buffer in the application memory includes: Reading the string of the set length from the file to be read; Writing the string of the set length into the set buffer according to the set writing order.

5. The method according to claim 1, characterized in that, The set buffer is a buffer array, and the buffer array has a start index and an end index; the start index is used to indicate the position of the starting character in the buffer array; the end index is used to indicate the position of the character for matching in the buffer array; The returning the string in front of the feature string in the set buffer to the application through the string builder includes: Taking out and returning the string except the feature string between the start index and the end index from the buffer array to the application; Moving the start index and the end index to the next character of the feature string.

6. A file reading device, characterized in that, Includes: A reading and writing module for reading a string of a set length from a file to be read located in the system memory into the first part of a set buffer in the application memory; A matching module for matching all the strings in the set buffer with a feature string; The feature string includes a plurality of specified characters; A processing module, configured to, if the matching fails, cache the string in the first part of the set buffer to a string builder; if the matching succeeds, return the string in the set buffer before the feature string to the application through the string builder, and repeatedly execute the operations of matching and data return on the remaining string in the set buffer until the feature string cannot be matched or there is no remaining string in the set buffer; Continue to read a string of a set length from the file to be read into the second part of the set buffer, and repeatedly execute the above steps of matching, caching, and returning data. After the execution is completed, if there is still a string in the first part, cache the string in the first part to the string builder, and move the string in the second part of the set buffer to the first part of the set buffer, and repeatedly execute the above steps of reading data, matching, caching, and returning data until all the strings in the file to be read are read.

7. A computing device, characterized in that, Comprising: A memory for storing program instructions; A processor for calling the program instructions stored in the memory and executing the method according to any one of claims 1 to 5 according to the obtained program instructions.

8. A computer-readable storage medium, characterized in that, Comprising computer-readable instructions, when the computer reads and executes the computer-readable instructions, the method according to any one of claims 1 to 5 is implemented.

9. A computer program product, characterized in that, Comprising computer-readable instructions, when the computer-readable instructions are executed by a processor, the method according to any one of claims 1 to 5 is implemented.

Citation Information

Patent Citations

  • Method, device and equipment for searching specified words in document and storage medium

    CN109657109A

  • Method and device for extracting persistent hotspot phrases from text corpus and storage medium

    CN112966505A