字串,字典,數組寫入本地檔案和從本地檔案讀取,字串數組
一.字串,字典,數組儲存到本地檔案
字串,數組,字典儲存到本地檔案過程一樣,只是要儲存的資料類型不同而已,這裡以字串儲存到本地檔案為例,如下
NSString *content =@"將字串儲存到本地檔案";
(1)擷取Documents檔案夾路徑
參數:(1)指定檔案夾,(2)設定尋找域,(3)是否使用詳細路徑
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];
(2)拼接上要儲存的檔案路徑(前面自動加上/),如果沒有此檔案系統會自動建立一個
NSString *newFielPath = [documentsPathstringByAppendingPathComponent:@"aa.txt”];
(3)將內容儲存到指定檔案路徑
NSError *error =nil;
字串寫入本地檔案參數:(1)要儲存內容的檔案路徑,(2)是否使用原子特性,(3)儲存格式
BOOL isSucceed = [contentwriteToFile:newFielPath atomically:YESencoding:NSUTF8StringEncoding error:&error];
二.字串,字典,數組,從本地檔案讀取
字串,字典,數組從本地檔案讀取的過程一樣,只是要讀取的資料類型不同而已,這裡以從本地檔案讀取字串為例,如下
1.擷取讀取檔案的路徑
(1)擷取Documents檔案夾路徑
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];
(2)拼接上要儲存的檔案路徑(前面自動加上/),如果沒有此檔案系統會自動建立一個
NSString *newFielPath = [documentsPathstringByAppendingPathComponent:@"aa.txt”];
(3)讀取資料
NSError *error =nil;(最後也可以&error)
NSString *content = [NSString stringWithContentsOfFile:newFielPath encoding:NSUTF8StringEncoding error:nil]
三.總結
檔案讀寫暫時只支援,NSString,NSArray,NSDictionary,NSData以及他們的子類
寫入檔案方法:writeToFiel:對象調用方法
讀取檔案:每個類內建的能夠根據檔案路徑讀取檔案的方法:[類名 + WithContentOfFiel],如下
NSString :[NSString StringWithContentOfFile:]
NSArry :[NSArry arrayWithContentOfFiel:]
NSDictionary :[NSDictionary dictionaryWithOfContentFile:]
二進位流:[NSData dataWithOfContentFiel:] 對於NSArray,NSDictionary ,這種容器,內部成員也必須是能夠實現檔案讀寫的八個類之一
在C或C++中可以在檔案中讀取字串寫入字串變數或數組?
設計:
1. 單行存放。student.txt中每行存放一個學生的各項資訊,即以分行符號為區別各條學生資訊。讀取的時候從檔案中每次提取一行來做屬性判斷,並篩選;
2. tab隔屬性。當然也可以以其他符號,但一般來說,在學生資訊內容中,一般不會使用table符號,則可以用table符號來在每行學生資訊中分隔每項屬性(學號、姓名等),這
樣可以通過躍進一個或多個table符號,找到關心的屬性,並進行判斷(住址頭4個位元組是否是“廣州”、性別屬性是否為“女”等)。同時也可以在輸出時選擇輸出部分屬性了。
關鍵代碼:
//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 should 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......餘下全文>>
java中實現,字串數組寫進本地一文字檔程式
String a[]=new String[]{"a","b","c"};
FileOutputStream fos=null;
try {
fos=new FileOutputStream("D:\\temp\\1.txt",true);//第二個參數設定是否追加檔案
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();
}
}