skip 記錄指針往下移動一條
創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供景泰網(wǎng)站建設(shè)、景泰做網(wǎng)站、景泰網(wǎng)站設(shè)計、景泰網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、景泰企業(yè)網(wǎng)站模板建站服務(wù),10余年景泰做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
skip 1 記錄指針往下移動一條,同上
skip -10 記錄指針往上移動10條(若碰到文件首,則是移動幾條算幾條,停在首部)
注:如果使用了索引,則按索引的順序(非實(shí)際記錄號)
早期foxbase等最常用于以下結(jié)構(gòu):
do while .net.eof()
......
skip
enddo
現(xiàn)在多用于按鈕,比如【下一條、上一條、下十條、上十條】
舉一個【下十條】的語句:
if !eof()
thisform.......等處理語句
skip 10
else
=messagebox('已經(jīng)到了文件尾部,不能再往下移動了!')
endif
【以下是手冊內(nèi)容】:
使記錄指針在表中向前移動或向后移動。
SKIP [nRecords] [IN nWorkArea | cTableAlias]
參數(shù)
nRecords
指定記錄指針需要移動的記錄數(shù)。
使用不帶 nRecords 參數(shù)的 SKIP 命令將使記錄指針走到下一個記錄。如果 nRecords 為正數(shù),記錄指針向文件尾移動 nRecords
個記錄;如果 nRecords 為負(fù)數(shù),記錄指針將向文件頭移動 nRecords 個記錄。
如果記錄指針指向表的最后一個記錄,并且執(zhí)行不帶參數(shù)的 SKIP 命令時,RECNO( ) 函數(shù)返回值比表中記錄總數(shù)大 1,EOF( )
函數(shù)返回“真”(.T.);如果記錄指針指向表的第一個記錄,并且執(zhí)行 SKIP 1 命令,則 RECNO( ) 函數(shù)返回 1,BOF( )
函數(shù)返回“真”(.T.)。
IN nWorkArea | cTableAlias
在指定工作區(qū)的表中移動記錄指針。nWorkArea 指定工作區(qū)編號,cTableAlias 指定一個表或工作區(qū)的別名。
備注
如果表有一個主控索引名或索引文件,使用 SKIP 命令將使記錄指針移動到索引序列決定的記錄上。
在android中的文件放在不同位置,它們的讀取方式也有一些不同。
本文對android中對資源文件的讀取、數(shù)據(jù)區(qū)文件的讀取、SD卡文件的讀取及RandomAccessFile的方式和方法進(jìn)行了整理。供參考。
一、資源文件的讀取:apk中資源文件
1) 從resource的raw中讀取文件數(shù)據(jù):
try{
//得到資源中的Raw數(shù)據(jù)流
InputStream in = getResources().openRawResource(R.raw.test);
//得到數(shù)據(jù)的大小
int length = in.available();
byte [] buffer = new byte[length];
//讀取數(shù)據(jù)
in.read(buffer);
//依test.txt的編碼類型選擇合適的編碼,如果不調(diào)整會亂碼
res = EncodingUtils.getString(buffer, "BIG5");
//關(guān)閉
in.close();
}catch(Exception e){
e.printStackTrace();
}
2) 從resource的asset中讀取文件數(shù)據(jù)
String fileName = "test.txt"; //文件名字
String res="";
try{
//得到資源中的asset數(shù)據(jù)流
InputStream in = getResources().getAssets().open(fileName);
int length = in.available();
byte [] buffer = new byte[length];
in.read(buffer);
in.close();
res = EncodingUtils.getString(buffer, "UTF-8");
}catch(Exception e){
e.printStackTrace();
}
二、讀寫/data/data/應(yīng)用程序名目錄下的文件:
//寫數(shù)據(jù)
public void writeFile(String fileName,String writestr) throws IOException{
try{
FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);
byte [] bytes = writestr.getBytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){
e.printStackTrace();
}
}
//讀數(shù)據(jù)
public String readFile(String fileName) throws IOException{
String res="";
try{
FileInputStream fin = openFileInput(fileName);
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
三、讀寫SD卡中的文件。也就是/mnt/sdcard/目錄下面的文件 :
//寫數(shù)據(jù)到SD中的文件
public void writeFileSdcardFile(String fileName,String write_str) throws IOException{
try{
FileOutputStream fout = new FileOutputStream(fileName);
byte [] bytes = write_str.getBytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){
e.printStackTrace();
}
}
//讀SD中的文件
public String readFileSdcardFile(String fileName) throws IOException{
String res="";
try{
FileInputStream fin = new FileInputStream(fileName);
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
四、使用File類進(jìn)行文件的讀寫:
//讀文件
public String readSDFile(String fileName) throws IOException {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
int length = fis.available();
byte [] buffer = new byte[length];
fis.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fis.close();
return res;
}
//寫文件
public void writeSDFile(String fileName, String write_str) throws IOException{
File file = new File(fileName);
FileOutputStream fos = new FileOutputStream(file);
byte [] bytes = write_str.getBytes();
fos.write(bytes);
fos.close();
}
五、另外,F(xiàn)ile類還有下面一些常用的操作:
String Name = File.getName(); //獲得文件或文件夾的名稱:
String parentPath = File.getParent(); //獲得文件或文件夾的父目錄
String path = File.getAbsoultePath();//絕對路經(jīng)
String path = File.getPath();//相對路經(jīng)
File.createNewFile();//建立文件
File.mkDir(); //建立文件夾
File.isDirectory(); //判斷是文件或文件夾
File[] files = File.listFiles(); //列出文件夾下的所有文件和文件夾名
File.renameTo(dest); //修改文件夾和文件名
File.delete(); //刪除文件夾或文件
六、使用RandomAccessFile進(jìn)行文件的讀寫:
RandomAccessFile的使用方法比較靈活,功能也比較多,可以使用類似seek的方式可以跳轉(zhuǎn)到文件的任意位置,從文件指示器當(dāng)前位置開始讀寫。
它有兩種構(gòu)造方法
new RandomAccessFile(f,"rw");//讀寫方式
new RandomAccessFile(f,"r");//只讀方式
使用事例:
/*
* 程序功能:演示了RandomAccessFile類的操作,同時實(shí)現(xiàn)了一個文件復(fù)制操作。
*/
import java.io.*;
public class RandomAccessFileDemo {
public static void main(String[] args) throws Exception {
RandomAccessFile file = new RandomAccessFile("file", "rw");
// 以下向file文件中寫數(shù)據(jù)
file.writeInt(20);// 占4個字節(jié)
file.writeDouble(8.236598);// 占8個字節(jié)
file.writeUTF("這是一個UTF字符串");// 這個長度寫在當(dāng)前文件指針的前兩個字節(jié)處,可用readShort()讀取
file.writeBoolean(true);// 占1個字節(jié)
file.writeShort(395);// 占2個字節(jié)
file.writeLong(2325451l);// 占8個字節(jié)
file.writeUTF("又是一個UTF字符串");
file.writeFloat(35.5f);// 占4個字節(jié)
file.writeChar('a');// 占2個字節(jié)
file.seek(0);// 把文件指針位置設(shè)置到文件起始處
// 以下從file文件中讀數(shù)據(jù),要注意文件指針的位置
System.out.println("——————從file文件指定位置讀數(shù)據(jù)——————");
System.out.println(file.readInt());
System.out.println(file.readDouble());
System.out.println(file.readUTF());
file.skipBytes(3);// 將文件指針跳過3個字節(jié),本例中即跳過了一個boolean值和short值。
System.out.println(file.readLong());
file.skipBytes(file.readShort()); // 跳過文件中“又是一個UTF字符串”所占字節(jié),注意readShort()方法會移動文件指針,所以不用加2。
System.out.println(file.readFloat());
//以下演示文件復(fù)制操作
System.out.println("——————文件復(fù)制(從file到fileCopy)——————");
file.seek(0);
RandomAccessFile fileCopy=new RandomAccessFile("fileCopy","rw");
int len=(int)file.length();//取得文件長度(字節(jié)數(shù))
byte[] b=new byte[len];
file.readFully(b);
fileCopy.write(b);
System.out.println("復(fù)制完成!");
}
}
七、讀取資源文件時能否實(shí)現(xiàn)類似于seek的方式可以跳轉(zhuǎn)到文件的任意位置,從指定的位置開始讀取指定的字節(jié)數(shù)呢?
答案是可以的。
在FileInputStream和InputStream中都有下面的函數(shù):
public long skip (long byteCount); //從數(shù)據(jù)流中跳過n個字節(jié)
public int read (byte[] buffer, int offset, int length); //從數(shù)據(jù)流中讀取length數(shù)據(jù)存在buffer的offset開始的位置。offset是相對于buffer的開始位置的,不是數(shù)據(jù)流。
可以使用這兩個函數(shù)來實(shí)現(xiàn)類似于seek的操作,請看下面的測試代碼:
//其中read_raw是一個txt文件,存放在raw目錄下。
//read_raw.txt文件的內(nèi)容是:"ABCDEFGHIJKLMNOPQRST"
public String getRawString() throws IOException {
String str = null;
InputStream in = getResources().openRawResource(R.raw.read_raw);
int length = in.available();
byte[] buffer = new byte[length];
in.skip(2); //跳過兩個字節(jié)
in.read(buffer,0,3); //讀三個字節(jié)
in.skip(3); //跳過三個字節(jié)
in.read(buffer,0,3); //讀三個字節(jié)
//最后str="IJK"
str = EncodingUtils.getString(buffer, "BIG5");
in.close();
return str;
}
可以使用這兩個函數(shù)來實(shí)現(xiàn)類似于seek的操作,請看下面的測試代碼:
從上面的實(shí)例可以看出skip函數(shù)有點(diǎn)類似于C語言中的seek操作,但它們之間有些不同。
需要注意的是:
1、skip函數(shù)始終是從當(dāng)前位置開始跳的。在實(shí)際應(yīng)用當(dāng)中還要再判斷一下該函數(shù)的返回值。
2、read函數(shù)也始終是當(dāng)前位置開始讀的。
3、另外,還可以使用reset函數(shù)將文件的當(dāng)前位置重置為0,也就是文件的開始位置。
如何得到文件的當(dāng)前位置?
我沒有找到相關(guān)的函數(shù)和方法,不知道怎么樣才能得到文件的當(dāng)前位置,貌似它也并不是太重要。
八、APK資源文件的大小不能超過1M,如果超過了怎么辦?我們可以將這個數(shù)據(jù)再復(fù)制到data目錄下,然后再使用。復(fù)制數(shù)據(jù)的代碼如下:
public boolean assetsCopyData(String strAssetsFilePath, String strDesFilePath){
boolean bIsSuc = true;
InputStream inputStream = null;
OutputStream outputStream = null;
File file = new File(strDesFilePath);
if (!file.exists()){
try {
file.createNewFile();
Runtime.getRuntime().exec("chmod 766 " + file);
} catch (IOException e) {
bIsSuc = false;
}
}else{//存在
return true;
}
try {
inputStream = getAssets().open(strAssetsFilePath);
outputStream = new FileOutputStream(file);
int nLen = 0 ;
byte[] buff = new byte[1024*1];
while((nLen = inputStream.read(buff)) 0){
outputStream.write(buff, 0, nLen);
}
//完成
} catch (IOException e) {
bIsSuc = false;
}finally{
try {
if (outputStream != null){
outputStream.close();
}
if (inputStream != null){
inputStream.close();
}
} catch (IOException e) {
bIsSuc = false;
}
}
return bIsSuc;
}
總結(jié):
1、apk中有兩種資源文件,使用兩種不同的方式進(jìn)行打開使用。
raw使用InputStream in = getResources().openRawResource(R.raw.test);
asset使用InputStream in = getResources().getAssets().open(fileName);
這些數(shù)據(jù)只能讀取,不能寫入。更重要的是該目錄下的文件大小不能超過1M。
同時,需要注意的是,在使用InputStream的時候需要在函數(shù)名稱后加上throws IOException。
2、SD卡中的文件使用FileInputStream和FileOutputStream進(jìn)行文件的操作。
3、存放在數(shù)據(jù)區(qū)(/data/data/..)的文件只能使用openFileOutput和openFileInput進(jìn)行操作。
或者使用BufferedReader,BufferedWriter 進(jìn)行讀寫,方便按行操作。
4、RandomAccessFile類僅限于文件的操作,不能訪問其他IO設(shè)備。它可以跳轉(zhuǎn)到文件的任意位置,從當(dāng)前位置開始讀寫。
5、InputStream和FileInputStream都可以使用skip和read(buffre,offset,length)函數(shù)來實(shí)現(xiàn)文件的隨機(jī)讀取。
scanf和printf函數(shù)是系統(tǒng)定義的函數(shù),函數(shù)的首部和函數(shù)體在定義的時候有。我們用的時候一直看到的是已經(jīng)定義好了的,系統(tǒng)默認(rèn)有效的。
所以不需要在程序開頭重新寫出來函數(shù)頭部分。
擴(kuò)展資料:
scanf()是C語言中的一個輸入函數(shù)。與printf函數(shù)一樣,都被聲明在頭文件stdio.h里,因此在使用scanf函數(shù)時要加上#include stdio.h。(在有一些實(shí)現(xiàn)中,printf函數(shù)與scanf函數(shù)在使用時可以不使用預(yù)編譯命令#include stdio.h。)它是格式輸入函數(shù),即按用戶指定的格式從鍵盤上把數(shù)據(jù)輸入到指定的變量之中。
printf:
格式輸出,它是c語言中產(chǎn)生格式化輸出的函數(shù)(在 stdio.h 中定義)。用于向終端(顯示器、控制臺等)輸出字符。格式控制由要輸出的文字和數(shù)據(jù)格式說明組成。
分享題目:C語言中skip函數(shù) c語言position函數(shù)
當(dāng)前網(wǎng)址:http://www.chinadenli.net/article22/dddoejc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、建站公司、定制網(wǎng)站、搜索引擎優(yōu)化、網(wǎng)站制作、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)