當前位置:首頁 » 安卓系統 » android文件流

android文件流

發布時間: 2022-07-23 22:14:39

『壹』 android下可以使用文件流讀取數據嗎

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String fileName = "/sdcard/y.txt";//文件路徑 // 也可以用String fileName = "mnt/sdcard/Y.txt"; String res = ""; try { FileInputStream fin = new FileInputStream(fileName); // FileInputStream fin = openFileInput(fileName); // 用這個就不行了,必須用FileInputStream int length = fin.available(); byte[] buffer = new byte[length]; fin.read(buffer); res = EncodingUtils.getString(buffer, "UTF-8");////依Y.txt的編碼類型選擇合適的編碼,如果不調整會亂碼 fin.close();//關閉資源 System.out.println("res--->"+res); int a=Integer.parseInt(res.substring(3, 5)); int b=Integer.parseInt(res.substring(8, 10)); System.out.println(a+"res--->"+b);//獲取的a.b } catch (Exception e) { e.printStackTrace(); } }

『貳』 Android,用文件輸入流讀取文件的疑問,求解。

第一個問題,OpenFileInput方法裡面調用了FileInputStream構造方法
第二個問題:byte[]作為讀文件時的一個緩沖,讀文件時會將文件內容讀到這個緩沖中,緩沖讀之後,再通知內核同步到內存中,減少底層的調用,提高性能
第三個問題:還是第二個問題,為了性能

『叄』 android 如何獲取項目內部文件輸出流OutputStream

this.getResources().openRawResource(R.raw.filename)這個是讀取res文件夾下的raw文件夾內的內容的,對應的打開方式?你是指輸出流?這個不能寫入的,就跟r.drawable.*一樣。只能讀

『肆』 android 將數據寫入文件中並導出。

java">@Override
publicvoidonClick(Viewview){
Stringstate=Environment.getExternalStorageState();//獲取外部設備狀態

//檢測外部設備是否可用
if(!state.equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(this,"外部設備不可用",Toast.LENGTH_SHORT).show();
return;
}

//創建文件
FilesdCard=Environment.getExternalStorageDirectory();//獲取外部設備的目錄
Filefile=newFile(sdCard,"文件名.txt");//文件位置
try{
FileOutputStreamoutputStream=newFileOutputStream(file);//打開文件輸出流
BufferedWriterwriter=newBufferedWriter(newOutputStreamWriter(outputStream));//寫入到緩存
writer.write("這里是要寫入到文件的數據");//從從緩存流寫入
writer.close();//關閉流
Toast.makeText(this,"輸出成功",Toast.LENGTH_SHORT).show();
}
catch(Exceptionexception){
Toast.makeText(this,"輸出失敗",Toast.LENGTH_SHORT).show();
}
}

寫入到文件管理時需要許可權

<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

『伍』 android 怎麼用二進制流上傳圖片與音頻文件

QusetionForm qusetionForm = (QusetionForm)form; FormFile imageTopic= qusetionForm.getImageTopic(); String name=imageTopic.getFileName(); String url=(servlet.getServletConfig().getServletContext().getRealPath("\\")+name).replace('\\','/');

『陸』 將android怎麼把數據流打到buffer上

在android中的文件放在不同位置,它們的讀取方式也有一些不同。
本文對android中對資源文件的讀取、數據區文件的讀取、SD卡文件的讀取及RandomAccessFile的方式和方法進行了整理。供參考。

一、資源文件的讀取:apk中資源文件
1) 從resource的raw中讀取文件數據:

try{

//得到資源中的Raw數據流
InputStream in = getResources().openRawResource(R.raw.test);

//得到數據的大小
int length = in.available();

byte [] buffer = new byte[length];

//讀取數據
in.read(buffer);

//依test.txt的編碼類型選擇合適的編碼,如果不調整會亂碼
res = EncodingUtils.getString(buffer, "BIG5");

//關閉
in.close();

}catch(Exception e){
e.printStackTrace();
}

2) 從resource的asset中讀取文件數據

String fileName = "test.txt"; //文件名字
String res="";
try{

//得到資源中的asset數據流
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/<應用程序名>目錄下的文件:


//寫數據
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();
}
}

//讀數據
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/目錄下面的文件 :

//寫數據到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類進行文件的讀寫:

//讀文件
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();
}

五、另外,File類還有下面一些常用的操作:


String Name = File.getName(); //獲得文件或文件夾的名稱:
String parentPath = File.getParent(); //獲得文件或文件夾的父目錄
String path = File.getAbsoultePath();//絕對路經
String path = File.getPath();//相對路經
File.createNewFile();//建立文件
File.mkDir(); //建立文件夾
File.isDirectory(); //判斷是文件或文件夾
File[] files = File.listFiles(); //列出文件夾下的所有文件和文件夾名
File.renameTo(dest); //修改文件夾和文件名
File.delete(); //刪除文件夾或文件

六、使用RandomAccessFile進行文件的讀寫:
RandomAccessFile的使用方法比較靈活,功能也比較多,可以使用類似seek的方式可以跳轉到文件的任意位置,從文件指示器當前位置開始讀寫。
它有兩種構造方法
new RandomAccessFile(f,"rw");//讀寫方式
new RandomAccessFile(f,"r");//只讀方式
使用事例:


/*
* 程序功能:演示了RandomAccessFile類的操作,同時實現了一個文件復制操作。
*/

import java.io.*;

public class RandomAccessFileDemo {
public static void main(String[] args) throws Exception {
RandomAccessFile file = new RandomAccessFile("file", "rw");
// 以下向file文件中寫數據
file.writeInt(20);// 佔4個位元組
file.writeDouble(8.236598);// 佔8個位元組
file.writeUTF("這是一個UTF字元串");// 這個長度寫在當前文件指針的前兩個位元組處,可用readShort()讀取
file.writeBoolean(true);// 佔1個位元組
file.writeShort(395);// 佔2個位元組
file.writeLong(2325451l);// 佔8個位元組
file.writeUTF("又是一個UTF字元串");
file.writeFloat(35.5f);// 佔4個位元組
file.writeChar('a');// 佔2個位元組

file.seek(0);// 把文件指針位置設置到文件起始處

// 以下從file文件中讀數據,要注意文件指針的位置
System.out.println("——————從file文件指定位置讀數據——————");
System.out.println(file.readInt());
System.out.println(file.readDouble());
System.out.println(file.readUTF());

file.skipBytes(3);// 將文件指針跳過3個位元組,本例中即跳過了一個boolean值和short值。
System.out.println(file.readLong());

file.skipBytes(file.readShort()); // 跳過文件中「又是一個UTF字元串」所佔位元組,注意readShort()方法會移動文件指針,所以不用加2。
System.out.println(file.readFloat());

//以下演示文件復制操作
System.out.println("——————文件復制(從file到fileCopy)——————");
file.seek(0);
RandomAccessFile fileCopy=new RandomAccessFile("fileCopy","rw");
int len=(int)file.length();//取得文件長度(位元組數)
byte[] b=new byte[len];
file.readFully(b);
fileCopy.write(b);
System.out.println("復制完成!");
}
}

七、讀取資源文件時能否實現類似於seek的方式可以跳轉到文件的任意位置,從指定的位置開始讀取指定的位元組數呢?
答案是可以的。
在FileInputStream和InputStream中都有下面的函數:


public long skip (long byteCount); //從數據流中跳過n個位元組
public int read (byte[] buffer, int offset, int length); //從數據流中讀取length數據存在buffer的offset開始的位置。offset是相對於buffer的開始位置的,不是數據流。

可以使用這兩個函數來實現類似於seek的操作,請看下面的測試代碼:
//其中read_raw是一個txt文件,存放在raw目錄下。
//read_raw.txt文件的內容是:"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); //跳過兩個位元組
in.read(buffer,0,3); //讀三個位元組

in.skip(3); //跳過三個位元組
in.read(buffer,0,3); //讀三個位元組

//最後str="IJK"
str = EncodingUtils.getString(buffer, "BIG5");

in.close();

return str;
}

可以使用這兩個函數來實現類似於seek的操作,請看下面的測試代碼:

從上面的實例可以看出skip函數有點類似於C語言中的seek操作,但它們之間有些不同。
需要注意的是:
1、skip函數始終是從當前位置開始跳的。在實際應用當中還要再判斷一下該函數的返回值。
2、read函數也始終是當前位置開始讀的。
3、另外,還可以使用reset函數將文件的當前位置重置為0,也就是文件的開始位置。
如何得到文件的當前位置?
我沒有找到相關的函數和方法,不知道怎麼樣才能得到文件的當前位置,貌似它也並不是太重要。

八、APK資源文件的大小不能超過1M,如果超過了怎麼辦?我們可以將這個數據再復制到data目錄下,然後再使用。復制數據的代碼如下:

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;
}

總結:
1、apk中有兩種資源文件,使用兩種不同的方式進行打開使用。
raw使用InputStream in = getResources().openRawResource(R.raw.test);
asset使用InputStream in = getResources().getAssets().open(fileName);
這些數據只能讀取,不能寫入。更重要的是該目錄下的文件大小不能超過1M。
同時,需要注意的是,在使用InputStream的時候需要在函數名稱後加上throws IOException。

2、SD卡中的文件使用FileInputStream和FileOutputStream進行文件的操作。

3、存放在數據區(/data/data/..)的文件只能使用openFileOutput和openFileInput進行操作。
或者使用BufferedReader,BufferedWriter 進行讀寫,方便按行操作。
4、RandomAccessFile類僅限於文件的操作,不能訪問其他IO設備。它可以跳轉到文件的任意位置,從當前位置開始讀寫。

5、InputStream和FileInputStream都可以使用skip和read(buffre,offset,length)函數來實現文件的隨機讀取。

『柒』 Android中 java io流如何獲取本地txt文件,並能對其進行閱讀

可以通過BufferedReader 流的形式進行流讀取,之後通過readLine方法獲取到讀取的內容。
BufferedReader bre = null;
try {
String file = "D:/test/test.txt";
bre = new BufferedReader(new FileReader(file));//此時獲取到的bre就是整個文件的緩存流
while ((str = bre.readLine())!= null) // 判斷最後一行不存在,為空結束循環
{
System.out.println(str);//原樣輸出讀到的內容
};
備註: 流用完之後必須close掉,如上面的就應該是:bre.close(),否則bre流會一直存在,直到程序運行結束。

『捌』 Android Studio的OutputStream輸出流輸出的文件在哪裡

1、書上沒說錯,確實在哪個目錄下

按照我的步驟做,然後去分析你的問題是什麼原因

1、在Applacation下寫

try {
FileOutputStream outputStream1 = getApplicationContext().openFileOutput("my.txt", Context.MODE_APPEND);
BufferedWriter outputStream=new BufferedWriter(new OutputStreamWriter(outputStream1));
outputStream.write("aaaaaaaaa");
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

2、使用模擬器運行,然後打開Device Monitor

4、如果還要問題,可以留下聯系方式或者追問

『玖』 Android中read和write那個算文件輸入流,那個輸出流,

要判斷輸入流和輸出流,首先要有參照物也就是我們的對象。我們在寫程序時,是以程序作為參照物,也就是針對程序(軟體)而言的。所以程序從文件里讀取信息是屬與輸入流(read).相反的程序將信息寫入磁碟或文件是屬與輸出流(write).

熱點內容
江西電商存儲設備怎麼樣 發布:2025-03-16 07:32:10 瀏覽:302
中信電話密碼是多少 發布:2025-03-16 07:32:08 瀏覽:364
怎麼樣學好編程 發布:2025-03-16 07:31:24 瀏覽:568
python輸出素數 發布:2025-03-16 07:31:21 瀏覽:417
顯卡加速編譯 發布:2025-03-16 07:28:53 瀏覽:682
javadebug 發布:2025-03-16 07:16:21 瀏覽:283
怎麼搭建linux伺服器ftp 發布:2025-03-16 07:07:38 瀏覽:988
晶元存儲原理 發布:2025-03-16 06:58:21 瀏覽:284
c語言中的整型 發布:2025-03-16 06:40:48 瀏覽:184
分部資料庫伺服器的IP地址有效 發布:2025-03-16 06:33:40 瀏覽:193