String, Dictionary, array written to local files and read from local files, String Array

Source: Internet
Author: User

String, Dictionary, array written to local files and read from local files, String Array
I. Strings, dictionaries, and arrays are stored in local files.
The process of storing strings, arrays, and dictionaries to local files is the same, but the data types to be stored are different. Here, we use strings to store local files as an example.

NSString * content = @ "storing strings to local files ";

(1) obtain the Documents folder path

Parameter: (1) Specify the folder, (2) set the search domain, (3) whether to use the detailed path

NSString * documentsPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

(2) the path of the file to be stored on the stitching (added automatically before/). If this file is not available, the system will automatically create

NSString * newFielPath = [documentsPathstringByAppendingPathComponent: @ "aa.txt"];


(3) store the content to the specified file path

NSError * error = nil;

Parameters for writing strings to local files: (1) file path of the content to be stored, (2) whether to use atomic properties, (3) Storage Format

BOOL isSucceed = [contentwriteToFile: newFielPath atomically: YESencoding: NSUTF8StringEncoding error: & error];



2. String, Dictionary, array, read from local file
The process of reading strings, dictionaries, and arrays from local files is the same, but the data types to be read are different. Here we take reading strings from local files as an example, as follows:

1. Get the path to read the file

(1) obtain the Documents folder path

NSString * documentsPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

(2) the path of the file to be stored on the stitching (added automatically before/). If this file is not available, the system will automatically create

NSString * newFielPath = [documentsPathstringByAppendingPathComponent: @ "aa.txt"];


(3) read data

NSError * error = nil; (you can also & error)

NSString * content = [NSString stringWithContentsOfFile: newFielPath encoding: NSUTF8StringEncoding error: nil]



Iii. Summary

Currently, only NSString, NSArray, NSDictionary, NSData, and their subclasses are supported.

File Writing Method: writeToFiel: object calling Method

File Reading: each class comes with a method that can read files based on the file path: [class name + WithContentOfFiel], as follows:

NSString: [NSString StringWithContentOfFile:]

NSArry: [NSArry arrayWithContentOfFiel:]
NSDictionary: [NSDictionary dictionaryWithOfContentFile:]
Binary stream: [NSData dataWithOfContentFiel:] For NSArray and NSDictionary containers, internal members must also be one of the eight classes that can implement file read/write.

Can C or C ++ read strings in files and write them into string variables or arrays?

Design:
1. Single Row Storage. Each row in student.txt stores the information of a student, that is, the information of each student is differentiated by a line break. During reading, one row is extracted from the file each time for Attribute judgment and filtering;
2. tab properties. Of course, other symbols can also be used, but generally, the table symbol is not used in the student information content, you can use the table symbol to separate each attribute (student ID, name, etc.) in the student information of each row.

In this example, you can use one or more table symbols to locate the attributes of interest and determine whether the four bytes of the address are "Guangzhou" and whether the Gender attribute is "female ). You can also select some output attributes during the output.

Key code:
// 1. read a single line from a file
Char * getFileLine (FILE * argInputFile, char * argLine ){
Int c;
......
For (int I = 0; (c = getc (argInputFile ))! = EOF & c! = '\ N'; I ++ ){
ArgLine [I] = c;
}
ArgLine [I] = '\ 0 ';
......
Return argLine;
}

// 2. read a single property from the information line
/**
* @ Param * argStuInfo: some student's information which was read by function 'getfileline ()'
* @ Param argPropertyNo: the position of the property to be read. e.g .:
* If the properties's order is like this: StuNo. (\ t) Name (\ t) Gender (\ t) Addr (\ t) others
* In the student info, and you want to read the gender of this student, then
* You shoshould call this function like this: getProperty (stuInfoLine, 3, gender );
*/
Char * getProperty (const char * argStuInfo, const int argPropertyNo, char * argProperty ){
......
Count = 1;
For (int I = 0; (c = getc (argInputFile ))! = '\ 0' & count <argPropertyNo; I ++ ){
If (c = '\ t '){
Count ++;
}
}
For (int I = 0; (c = getc (argInputFile ))! = '\ 0' & c! = '\ T'; I ++ ){
ArgPro... the remaining full text>

Java implementation, String Array written into a local text file program

String a [] = new String [] {"a", "B", "c "};
FileOutputStream fos = null;
Try {
Fos = new FileOutputStream ("D: \ temp \ 1.txt", true); // The second parameter sets whether to append an object.
PrintWriter pw = new PrintWriter (fos );
For (int I = 0; I <a. length; I ++ ){
Pw. write (a [I] + "\ r \ n ");
}
Pw. flush ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Finally {
Try {
Fos. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.