導航:首頁 > 操作系統 > android寫txt文件

android寫txt文件

發布時間:2022-07-19 02:49:04

android 在手機中創建了txt文件,卻無法寫入數據,是為什麼

你是說不能編輯吧!在手機上下載一個文本編輯器,例如WPS OFFICE,下載安裝完成後,點擊進入首頁。進入後點下方的」+「號,點擊後就會彈出一個下拉框,選擇新建文檔,點擊後就會進入新建文檔模式,進入後點上面的」新建空白,點入後就會彈出編輯頁面,編輯好的文檔傳輸給電腦用WORD和WPS都能打開。

② Android 根目錄下讀寫.txt文件

java">//根目錄許可權不允許,放到/data/packeg_dir下或SD卡中

packagecom.example.demo;

Filedir=Environment.getDataDirectory();//獲取data目錄
//Environment.getExternalStorageDirectory();//獲取SD卡目錄
FileoutFile=newFile(dir,"/data/com.example.demo/text.txt");//只能在自己的程序包里建立文件,這是許可權問題

③ Android開發問一下現在7.0怎麼寫txt文件

StringBuffer sb = new StringBuffer(); File file = new File("myfile.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String line = ""; while((line = br.readLine())!=null){ sb.append(line); } br.close(); (TextView)findViewById(R.id.text1).setText(sb.toString()); 第二行,創建文件對象,指向需要讀取的文件 第三行,創建文件Reader對象,讀取指定的文件 第四五行,創建一個line接受讀取的文件內容,因為是文本文件,所以一行一行讀 第八行,關閉文件讀取對象 第九行,將文本文件內容寫入到TextVIew中

④ 求android 創建 txt 文件

  1. 打開手機助手

  2. 搜索txt編輯

  3. 安裝就可以

  4. 然後運行,就可以新建,打開txt文件。

  5. 也可以所有office的word新建。這個系統自帶。

⑤ Android寫入txt文件

分以下幾個步驟:

  1. 首先對manifest注冊SD卡讀寫許可權

    AndroidManifest.xml
    <?xmlversion="1.0"encoding="utf-8"?>
    <manifestxmlns:android="

    package="com.tes.textsd"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16"/>
    <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
    android:name="com.tes.textsd.FileOperateActivity"
    android:label="@string/app_name">
    <intent-filter>
    <actionandroid:name="android.intent.action.MAIN"/>
    <categoryandroid:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
    </activity>
    </application>
    </manifest>
  2. 創建一個對SD卡中文件讀寫的類

    FileHelper.java
    /**
    *@Title:FileHelper.java
    *@Packagecom.tes.textsd
    *@Description:TODO(用一句話描述該文件做什麼)
    *@authorAlex.Z
    *@date2013-2-26下午5:45:40
    *@versionV1.0
    */
    packagecom.tes.textsd;
    importjava.io.DataOutputStream;
    importjava.io.File;
    importjava.io.FileOutputStream;
    importjava.io.FileWriter;
    importjava.io.FileInputStream;
    importjava.io.FileNotFoundException;
    importjava.io.IOException;
    importandroid.content.Context;
    importandroid.os.Environment;
    publicclassFileHelper{
    privateContextcontext;
    /**SD卡是否存在**/
    privatebooleanhasSD=false;
    /**SD卡的路徑**/
    privateStringSDPATH;
    /**當前程序包的路徑**/
    privateStringFILESPATH;
    publicFileHelper(Contextcontext){
    this.context=context;
    hasSD=Environment.getExternalStorageState().equals(
    android.os.Environment.MEDIA_MOUNTED);
    SDPATH=Environment.getExternalStorageDirectory().getPath();
    FILESPATH=this.context.getFilesDir().getPath();
    }
    /**
    *在SD卡上創建文件
    *
    *@throwsIOException
    */
    publicFilecreateSDFile(StringfileName)throwsIOException{
    Filefile=newFile(SDPATH+"//"+fileName);
    if(!file.exists()){
    file.createNewFile();
    }
    returnfile;
    }
    /**
    *刪除SD卡上的文件
    *
    *@paramfileName
    */
    publicbooleandeleteSDFile(StringfileName){
    Filefile=newFile(SDPATH+"//"+fileName);
    if(file==null||!file.exists()||file.isDirectory())
    returnfalse;
    returnfile.delete();
    }
    /**
    *寫入內容到SD卡中的txt文本中
    *str為內容
    */
    publicvoidwriteSDFile(Stringstr,StringfileName)
    {
    try{
    FileWriterfw=newFileWriter(SDPATH+"//"+fileName);
    Filef=newFile(SDPATH+"//"+fileName);
    fw.write(str);
    FileOutputStreamos=newFileOutputStream(f);
    DataOutputStreamout=newDataOutputStream(os);
    out.writeShort(2);
    out.writeUTF("");
    System.out.println(out);
    fw.flush();
    fw.close();
    System.out.println(fw);
    }catch(Exceptione){
    }
    }
    /**
    *讀取SD卡中文本文件
    *
    *@paramfileName
    *@return
    */
    publicStringreadSDFile(StringfileName){
    StringBuffersb=newStringBuffer();
    Filefile=newFile(SDPATH+"//"+fileName);
    try{
    FileInputStreamfis=newFileInputStream(file);
    intc;
    while((c=fis.read())!=-1){
    sb.append((char)c);
    }
    fis.close();
    }catch(FileNotFoundExceptione){
    e.printStackTrace();
    }catch(IOExceptione){
    e.printStackTrace();
    }
    returnsb.toString();
    }
    publicStringgetFILESPATH(){
    returnFILESPATH;
    }
    publicStringgetSDPATH(){
    returnSDPATH;
    }
    publicbooleanhasSD(){
    returnhasSD;
    }
    }
  3. 寫一個用於檢測讀寫功能的的布局

    main.xml
    <?xmlversion="1.0"encoding="utf-8"?>
    <LinearLayoutxmlns:android="

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView
    android:id="@+id/hasSDTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="hello"/>
    <TextView
    android:id="@+id/SDPathTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="hello"/>
    <TextView
    android:id="@+id/FILESpathTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="hello"/>
    <TextView
    android:id="@+id/createFileTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="false"/>
    <TextView
    android:id="@+id/readFileTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="false"/>
    <TextView
    android:id="@+id/deleteFileTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="false"/>
    </LinearLayout>
  4. 就是UI的類了

    FileOperateActivity.class
    /**
    *@Title:FileOperateActivity.java
    *@Packagecom.tes.textsd
    *@Description:TODO(用一句話描述該文件做什麼)
    *@authorAlex.Z
    *@date2013-2-26下午5:47:28
    *@versionV1.0
    */
    packagecom.tes.textsd;
    importjava.io.IOException;
    importandroid.app.Activity;
    importandroid.os.Bundle;
    importandroid.widget.TextView;
    {
    privateTextViewhasSDTextView;
    privateTextViewSDPathTextView;
    ;
    ;
    ;
    ;
    privateFileHelperhelper;
    @Override
    publicvoidonCreate(BundlesavedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    hasSDTextView=(TextView)findViewById(R.id.hasSDTextView);
    SDPathTextView=(TextView)findViewById(R.id.SDPathTextView);
    FILESpathTextView=(TextView)findViewById(R.id.FILESpathTextView);
    createFileTextView=(TextView)findViewById(R.id.createFileTextView);
    readFileTextView=(TextView)findViewById(R.id.readFileTextView);
    deleteFileTextView=(TextView)findViewById(R.id.deleteFileTextView);
    helper=newFileHelper(getApplicationContext());
    hasSDTextView.setText("SD卡是否存在:"+helper.hasSD());
    SDPathTextView.setText("SD卡路徑:"+helper.getSDPATH());
    FILESpathTextView.setText("包路徑:"+helper.getFILESPATH());
    try{
    createFileTextView.setText("創建文件:"
    +helper.createSDFile("test.txt").getAbsolutePath());
    }catch(IOExceptione){
    e.printStackTrace();
    }
    deleteFileTextView.setText("刪除文件是否成功:"
    +helper.deleteSDFile("xx.txt"));
    helper.writeSDFile("1213212","test.txt");
    readFileTextView.setText("讀取文件:"+helper.readSDFile("test.txt"));
    }
    }

⑥ android 怎麼創建txt文件

可以安裝920文本編輯器,可以很方便創建文本文件

⑦ 你好,請問在android裡面怎麼新建TXT文件呢

你可以是用一些移動辦公軟體,這樣就可以像電腦一樣新建了。

⑧ Android能直接編輯TXT文檔嗎

luyongfeng是的,Andriod系統畢竟比Windows Mobile系統差一些。Andriod強在界面美觀,便於用手指操作。而windows mobile系統接近於電腦的使用,象windows Xp一樣。功能確實是強大的。比如這個TXt文本編輯,在Windows mobile系統中是小菜一碟,在Andriod系統中就會有麻煩,主要就是亂碼問題。 還有一個看圖軟體,Andriod系統也是極為弱智。急盼改進。

⑨ android 如何讀寫文件

讀文件:

1、通過File獲取文件

2、打開輸入流,讀取文件

寫文件:

1、創建文件

2、打開輸出流,寫入文件內容


示例:

讀文件:
Stringcontent="";//文件內容字元串
//通過路徑/sdcard/foo.txt打開文件
Filefile=newFile("/sdcard/foo.txt");
try{
InputStreaminstream=newFileInputStream(file);//讀取輸入流
InputStreamReaderinputreader=newInputStreamReader(instream);//設置流讀取方式
BufferedReaderbuffreader=newBufferedReader(inputreader);
while((line=buffreader.readLine())!=null){
content+=line+" ";//讀取的文件內容
}
}catch(Exceptionex){
}
寫文件:
Filefile=newFile("/sdcard/foo.txt");//
if(!file.exists())
file.createNewFile();//如果文件不存在,創建foo.txt
try{
OutputStreamoutstream=newFileOutputStream(file);//設置輸出流
OutputStreamWriterout=newOutputStreamWriter(outstream);//設置內容輸出方式
out.write("文字內容");//輸出內容到文件中
out.close();
}catch(java.io.IOExceptione){
e.printStackTrace();
}

⑩ 在android創建一個.txt文件怎麼樣寫文件路徑

filename = getSDPath() + "/" + formatter.format(currentTime) + ".txt";
filename = getSDPath() + "/" + getSystemTime();/////////////
file =new File(filename);
fileOutputStream = new FileOutputStream(file, true);

閱讀全文

與android寫txt文件相關的資料

熱點內容
如何做一個系統u盤文件夾名字 瀏覽:966
如何確認哪個ip重啟了伺服器 瀏覽:128
照片壓縮軟體綠色版 瀏覽:107
pgp基於什麼體系加密 瀏覽:635
python合法賦值語句格式 瀏覽:711
程序員數學線性代數 瀏覽:624
看幀率app如何使用 瀏覽:525
從DHC伺服器租用IP地址 瀏覽:475
編譯怎麼學 瀏覽:331
數碼管顯示0到9plc編程 瀏覽:667
伺服器是為什麼服務的 瀏覽:767
java定義數據類型 瀏覽:878
安卓pdf手寫 瀏覽:431
什麼是app開發者 瀏覽:288
android鬧鍾重啟 瀏覽:105
程序員失職 瀏覽:522
在雲伺服器怎麼改密碼 瀏覽:588
伺服器pb什麼意思 瀏覽:944
51駕駛員的是什麼app 瀏覽:674
php靜態變數銷毀 瀏覽:890