導航:首頁 > 操作系統 > androidfilewriter

androidfilewriter

發布時間:2022-12-21 02:11:08

android 錯誤日誌 哪裡

1、創建MyCrashHandler類
package com.example.yu.myapplication;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.Date;

/**
* 全局捕獲導常,保存到本地錯誤日誌。日誌
* 路徑位於sdcard/錯誤日誌Log/myErrorLog下。
*/
public class MyCrashHandler implements UncaughtExceptionHandler {

private static MyCrashHandler instance;

public static MyCrashHandler getInstance() {
if (instance == null) {
instance = new MyCrashHandler();
}
return instance;
}

public void init(Context ctx) {
Thread.(this);
}

/**
* 核心方法,當程序crash 會回調此方法, Throwable中存放這錯誤日誌
*/
@Override
public void uncaughtException(Thread arg0, Throwable arg1) {

String logPath;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
logPath = Environment.getExternalStorageDirectory()
.getAbsolutePath()
+ File.separator
+ File.separator
+ "錯誤日誌Log";

File file = new File(logPath);
if (!file.exists()) {
file.mkdirs();
}
try {
FileWriter fw = new FileWriter(logPath + File.separator
+ "myErrorlog.log", true);
fw.write(new Date() + "錯誤原因:\n");
// 錯誤信息
// 這里還可以加上當前的系統版本,機型型號 等等信息
StackTraceElement[] stackTrace = arg1.getStackTrace();
fw.write(arg1.getMessage() + "\n");
for (int i = 0; i < stackTrace.length; i++) {
fw.write("file:" + stackTrace[i].getFileName() + " class:"
+ stackTrace[i].getClassName() + " method:"
+ stackTrace[i].getMethodName() + " line:"
+ stackTrace[i].getLineNumber() + "\n");
}
fw.write("\n");
fw.close();
// 上傳錯誤信息到伺服器
// uploadToServer();
} catch (IOException e) {
Log.e("crash handler", "load file failed...", e.getCause());
}
}
arg1.printStackTrace();
android.os.Process.killProcess(android.os.Process.myPid());
}
}

⑵ android開發中,做一個了注冊的界面後,如何將注冊的信息以TXT文件形式保存在SD卡中

你需要在xml文件裡面使能外部的sd卡:
<!-- 在SDCard中創建於刪除文件的許可權 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard中寫入數據的許可權 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

然後android的java code如下:
public void saveToSDCard(String filename,String content) throws Exception{
File file=new File(Environment.getExternalStorageDirectory(), filename);
OutputStream out=new FileOutputStream(file);
out.write(content.getBytes());
out.close();
}

⑶ android一個文本文件如何按行讀取

try{
InputStreammyInput=mcontext.getResources().openRawResource(R.raw.medicalspeciality);
InputStreamReaderreader=newInputStreamReader(myInput);
BufferedReaderbreader=newBufferedReader(reader);
Stringstr;
FileWritermyOutput=newFileWriter(outFileName,true);
while((str=breader.readLine())!=null){
System.out.println(i+++str);
}
//Closethestreams
myOutput.flush();
myOutput.close();
myInput.close();
}catch(Exceptione){
//TODO:handleexception
e.getStackTrace();
}

⑷ 【總】Android之IO流/文件導航

本篇文章內容來自於
1. Android基礎之IO流

一、File類
--1.1 File類的構造方法
--1.2 File類的創建方法
--1.3 File類的常用方法

二、IO流
1.IO流分類
2.InputStream/OutputStream(位元組流基類 拷貝用這個)
3.Reader/Writer(字元流基類 只讀或者只寫用這個)
4. FileInputStream/FileOutputStream (文件輸入輸出流,一般都用這個)
5. BufferedInputStream/BufferedOutputStream (位元組緩沖流,減少與硬碟的交流次數,加快速度,需要flush()才可寫入)
6.IO流如何處理異常
7. FileReader/FileWriter (文件字元流)
8. BufferedReader/BufferedWriter (緩沖字元流,需要flush()才可寫入)
9. InputStreamReader/OutputStreamWriter轉換流 (將位元組流轉換成字元流)
10. ByteArrayInputStream/ByteArrayOutputStream (數組字元流,可以將流寫入到內存中,然後獲取所有結果)
11. DataInputStream/DataOutputStream (基本數據流,可以以基本數據的形式寫入和讀取)
12. ObjectInputStream/ObjectOutputStream (對象操作流,可以序列化或者反序列化)

三、IO流應用
1.處理流(BufferedXXX)配合節點流(XXXInputStream和XXXWriter/Reader)
2.輸入流(FileInputStream等)配合使用ByteArrayOutputStream(內存數組流),將輸入流內容一次性輸出
3.使用ObjectOutputStream將得到的密鑰Key對象存儲

文件有無後綴都會創建

輸入流/輸出流 (按流向分)
輸入流是寫入到內存 InputStream、Reader
輸出流是寫出到存儲設備 OutputStream、Writer

位元組流/字元流 (按操作類型分)
位元組流可操作任何數據 InputStream、OutputStream
字元流只能操作純字元數據 Reader、Writer

節點流/處理流
節點流的的構造參數是物理IO節點,比如public FileInputStream(File file)
處理流的構造參數是已經存在的流(裝飾設計模式),比如public BufferedOutputStream(OutputStream out)

常用節點流

InputStream是位元組輸入流的抽象基類

OutputStream是位元組輸入流的抽象基類

Reader是字元輸入流的抽象基類

Writer是字元輸出流的抽象基類

Android-IO流之文件輸入輸出位元組流FileInputStream/FileOutputStream

Android-IO流之緩沖位元組流BufferedInputStream/BufferedOutputStream

處理方式一:

處理方式二:

Android-IO流之文件字元流FileReader/FileWriter

Android-IO流之緩沖字元流BufferedReader/BufferedWriter

Android-IO流之轉換流InputStreamReader/OutputStreamWriter

Android-IO流之數組內存位元組流ByteArrayInputStream/ByteArrayOutputStream

Android-IO流之數據流DataInputStream/DataOutputStream

Android-IO流之對象操作流ObjectInputStream/ObjectOutputStream

實例:當系統提供的方法返回的是FileOutputStream時,想寫入字元串,則配合使用緩存流BufferedWriter和轉換流OutputStreamWriter

實例:當系統提供的方法返回是FileInputStream時,想讀出字元串信息。則配合使用BufferedReader和InputStreamReader

實例:將文件中的內容讀出顯示

⑸ Android 怎樣在應用程序中向文件里寫入數據

  1. Android 怎樣在應用程序中向文件里寫入數據?在AndroidManifest.xml中添加, <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />,解決!

  2. 另外了解一下android的數據存儲方式:文件流的讀取,SQLite,Content Provider以及Preference.。 註:resource和assets中的文件用戶方面是只可以讀取不能夠進行寫的操作的。
    Content Provider作為程序之間唯一共享數據途徑,用在這里不是很合適。所以,
    第一種方式,使用FileInputStream,FileOutputStreami類實現文件I/O操作,直接往手機中存儲數據。
    第二種方式,使用SQLite,通過SQLiteDatabase類中方法操作數據。
    第三種方式,Preference用於存儲簡單數據類型的數據,一些設置信息的保存。個人認為用在這里最合適。 它利用鍵值對存儲的。例:
    存儲:SharedPreferences.Editor editor =sp.edit();
    editor.putString(KEY_TEXT,"yonghu");
    editor.commit();
    獲取 :sp =getPreferences(MODE_PRIVATE);

    String result =sp.getString(KEY_TEXT,null)

  3. Android保存數據幾種常用方法解析

    它應用於手機中能夠幫助我們實現許多需求。比如今天為大家介紹的Android保存數據,就是其中一個比較重要的操作技巧。Android組件相關概念總結Android傳值方法細講Android橫豎屏切換正確實現方式分享Android開發環境相關配置概覽Android NDK具體作用講解對於我們所熟悉的大部分軟體都有一個比較典型的特點,應用現有的數據根據不同的需求來得到相應的結果。例如,我們最常用的Officeword、Excel、PowerPoint等辦公軟體,它們都是幫助我們完成某種特定的需求,同時由其所產生的數據或者文檔又可以被其它軟體所讀取和做進一步的優化等等,在這個層面上可以看成是這些軟體通過相同的文件標准來共享數據。但是對於Android最大的不同點在於,其平台上的應用軟體所存儲的數據或者文件是私有,僅僅可以通過它自身才可以訪問其所包含的數據資源。那麼基於這樣的限制,該如何在Android平台上實現不同程序間的數據共享呢?答案非常簡單 – 應用ContentProviders,這是建立在Android平台上用於定義統一的數據標准。Android提供了針對不同數據類型的ContentProviders來滿足各種需要。例如:Image、Audio、Video和通訊錄信息類等。閱讀下邊的文檔之前,最好先熟悉Content Providers的概念。有了上邊所提到Content Providers,接下來就要處理在共享文件過程中的存儲環節了,這里有四種方法分別適用於不同情況的需求。它們都各自有相應的優缺點,所以當開發者決定選擇應用哪種方法之前,先要考慮當前所操作的情況是否適合於所選擇的方法。Preferences Files Databases Network 接下來將依次介紹上訴四個Android保存數據方法:Preferences從其保存數據的結構來分析,這是一個相對較輕量級的存儲數據的方法。類似於我們常用的ini文件保存軟體初始化設置,同樣在Android平台常用於存儲較簡單的參數設置。例如,可以通過它保存上一次用戶所作的修改或者自定義參數設定,當再次啟動程序後依然保持原有的設置。通過Context.getSharedPreferences()方法來讀寫數值,這個方法通過設置name來使得同一個程序內的其它模塊共享數據。如果不需要與其它模塊共享數據,可以使用Activity.getPreferences()方法保持數據私有。需要著重強調一點,無法直接在多個程序間共享Preferences數據(不包括使用Content Providers)。通過一個實例來了解實際使用方法:import android.app.Activity; import android.content.SharedPreferences; public class Calc extends Activity { public static final String PREFS_NAME = "MyPrefsFile"; . . . . Override protected void onCreate(Bundle state){ super.onCreate(state); . . . . // Restore preferences SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); boolean silent = settings.getBoolean("silentMode", false); setSilent(silent); } @Override protected void onStop(){ super.onStop(); // Save user preferences. We need an Editor object to // make changes. All objects are from android.context.Context SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("silentMode", mSilentMode); // Don't forget to commit your edits!!! editor.commit(); } } Files從這是第二種方法,可以在設備本身的存儲設備或者外接的存儲設備中創建用於保存數據的文件。同樣在默認的狀態下,文件是不能在不同的程序間共享。寫文件:調用Context.openFileOutput()方法根據指定的路徑和文件名來創建文件,這個方法會返回一個FileOutputStream對象。讀取文件:調用Context.openFileInput()方法通過制定的路徑和文件名來返回一個標準的Java FileInputStream對象。
    (注意:在其它程序中將無法應用相同的路徑和文件名來操作文件)另外編譯程序之前,在res/raw/tempFile中建立一個static文件,這樣可以在程序中通過Resources.openRawResource (R.raw.myDataFile)方法同樣返回一個InputStream對象,直接讀取文件內容。Databases在Android API中包括了應用SQLite databases的介面,每個程序所創建的資料庫都是私有的,換句話說,程序間無法相互訪問對方的資料庫。在程序中創建SQLiteDatabase對象,其中包含了大部分與database交互的方法,例如:讀取數據或者管理當前數據。可以應用SQLiteDatabase和其subClassSQLiteOpenHelper的create()方法來創建新的資料庫。對於SQLitedatabase而言,其強大和方便的功能為Android提供了強有力的存儲功能。特別是存儲一些復雜的數據結構,例如:Android特別為通訊錄創建了特有的數據類型,其中包含了非常多的子集而且涵蓋了大部分的數據類型 「First Name」 「Last Name」 「PhoneNumber」和「Photo」等。Android可以通過Sqlite3 database tool來查看指定資料庫中表的內容,直接運行SQL命令來快速便捷的直接操作SQLite database。

⑹ Android中Java 默認UTF-8,中文亂碼

OutputStreamWriter(OutputStream out)
Creates an OutputStreamWriter that uses the default character
encoding.

OutputStreamWriter(OutputStream out, Charset cs)
Creates an OutputStreamWriter that uses the given
charset.

OutputStreamWriter(OutputStream out, CharsetEncoder enc)
Creates an OutputStreamWriter that uses the given charset
encoder.

OutputStreamWriter(OutputStream out, String charsetName)
Creates an OutputStreamWriter that uses the named
charset.
參考一下吧

⑺ 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"));
    }
    }
閱讀全文

與androidfilewriter相關的資料

熱點內容
抖音生活圈小程序源碼 瀏覽:620
建行預約解壓需要多久時間 瀏覽:568
命令式介面 瀏覽:399
電腦伺服器域名地址怎麼查 瀏覽:340
什麼行業要用編程 瀏覽:297
三洋立風櫃壓縮機 瀏覽:296
微拍app為什麼下載不了了 瀏覽:257
非常好的期貨5分鍾公式源碼 瀏覽:4
linuxcentos7安裝 瀏覽:691
華為網盤文件夾加密 瀏覽:74
安卓手機什麼真人游戲好玩 瀏覽:772
崑山加密軟體需求 瀏覽:270
蘋果照片壓縮包怎麼打開 瀏覽:796
檢測溫濕度和二氧化碳的單片機 瀏覽:964
安卓手機雨滴怎麼隱藏 瀏覽:587
pdf文件轉換器word 瀏覽:987
vscodepython模塊方法 瀏覽:344
如何知道伺服器有什麼漏洞 瀏覽:902
java電商訂單支付源碼 瀏覽:102
android手機滑鼠 瀏覽:465