欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android讀寫中文如何避免亂碼詳解

 更新時間:2017年04月04日 10:27:28   作者:YoYong  
這篇文章主要介紹了android讀寫中文如何避免亂碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。

前言

android讀取文件中文出現(xiàn)亂碼的原因無非就是,讀取文件的字符格式與寫如文件的格式不一致。因此,避免中文亂碼,要在寫入文件的時候按照一定的格式寫入,讀取的時候按照一定的格式讀取。這樣對應(yīng)就不會出現(xiàn)亂碼。對于其它的文本讀取,在不知道何種格式的時候,可以先讀取相應(yīng)的文件信息,再進(jìn)行相應(yīng)的轉(zhuǎn)碼。

下面是一個避免中文讀寫出現(xiàn)亂碼的類。

RWFile.java

package com.rwfile.main; 
 
import java.io.BufferedReader; 
 
import java.io.BufferedWriter; 
 
import java.io.File; 
 
import java.io.FileInputStream; 
 
import java.io.FileOutputStream; 
 
import java.io.InputStreamReader; 
 
import java.io.OutputStreamWriter; 
 
import android.os.Environment; 
 
public class RWFile { 
 
/** 
 
* 判斷sdcard是否存在 
 
* 
 
* @return 
 
*/ 
 
public static boolean isSdcard() { 
 
String status = Environment.getExternalStorageState(); 
 
if (status.equals(Environment.MEDIA_MOUNTED)) { 
 
return true; 
 
} else { 
 
return false; 
 
} 
 
} 
 
/** 
 
* 讀取文件內(nèi)容 
 
* 
 
* @param filePathAndName 
 
* @return 
 
*/ 
 
public static String readFile(String filePathAndName) { 
 
String fileContent = null; 
 
try { 
 
File f = new File(filePathAndName); 
 
if (f.isFile() && f.exists()) { 
 
fileContent = ""; 
 
InputStreamReader read = new InputStreamReader( 
 
new FileInputStream(f), "UTF-8"); 
 
BufferedReader reader = new BufferedReader(read); 
 
String line; 
 
while ((line = reader.readLine()) != null) { 
 
fileContent += line; 
 
} 
 
read.close(); 
 
} 
 
} catch (Exception e) { 
 
e.printStackTrace(); 
 
return null; 
 
} 
 
return fileContent; 
 
} 
 
/** 
 
* 寫入文件內(nèi)容 
 
* 
 
* @param filePathAndName 
 
* @param fileContent 
 
*/ 
 
public static boolean writeFile(String filePathAndName, String fileContent) { 
 
try { 
 
File f = new File(filePathAndName); 
 
if (!f.exists()) { 
 
f.createNewFile(); 
 
} 
 
// 覆蓋文件 
 
OutputStreamWriter write = new OutputStreamWriter( 
 
new FileOutputStream(f), "UTF-8");// 覆蓋文件 
 
// 追加文件 
 
// OutputStreamWriter write = new OutputStreamWriter( 
 
// new FileOutputStream(f, true), "UTF-8"); // 追加文件 
 
BufferedWriter writer = new BufferedWriter(write); 
 
writer.write(fileContent); 
 
writer.close(); 
 
} catch (Exception e) { 
 
e.printStackTrace(); 
 
return false; 
 
} 
 
return true; 
 
} 
 
} 

根據(jù)這個類寫的一個測試的Demo項目。

MainActivity.java

package com.rwfile.main; 
 
import java.io.File; 
 
import android.os.Bundle; 
import android.os.Environment; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 
 
public class MainActivity extends Activity { 
 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
 
final EditText input = (EditText) findViewById(R.id.input); 
final TextView content = (TextView) findViewById(R.id.content); 
Button write = (Button) findViewById(R.id.write); 
 
write.setOnClickListener(new OnClickListener() { 
 
@Override 
public void onClick(View arg0) { 
// TODO Auto-generated method stub 
if (!RWFile.isSdcard()) { 
Toast.makeText(MainActivity.this, "無法找到sdcard卡", 
Toast.LENGTH_LONG).show(); 
} else { 
String sdcard = Environment.getExternalStorageDirectory() 
.toString() + File.separator; 
System.out.println("write path:" + sdcard + "test.txt"); 
RWFile.writeFile(sdcard + "test.txt", input.getText() 
.toString()); 
 
} 
} 
}); 
Button read = (Button) findViewById(R.id.read); 
 
read.setOnClickListener(new OnClickListener() { 
 
@Override 
public void onClick(View arg0) { 
// TODO Auto-generated method stub 
if (!RWFile.isSdcard()) { 
Toast.makeText(MainActivity.this, "無法找到sdcard卡", 
Toast.LENGTH_LONG).show(); 
} else { 
String sdcard = Environment.getExternalStorageDirectory() 
.toString() + File.separator; 
System.out.println("read path:" + sdcard + "test.txt"); 
String str = RWFile.readFile(sdcard + "test.txt"); 
if (str == null) 
Toast.makeText(MainActivity.this, "無法找到test.txt文件", 
Toast.LENGTH_LONG).show(); 
else { 
content.setText(str); 
} 
} 
} 
}); 
 
} 
 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
// Inflate the menu; this adds items to the action bar if it is present. 
getMenuInflater().inflate(R.menu.activity_main, menu); 
return true; 
} 
 
} 

activy_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
xmlns:tools="http://schemas.android.com/tools" 
 
android:layout_width="match_parent" 
 
android:layout_height="match_parent" 
 
android:orientation="vertical" 
 
tools:context=".MainActivity" > 
 
<TextView 
 
android:layout_width="wrap_content" 
 
android:layout_height="wrap_content" 
 
android:text="@string/input" /> 
 
<EditText android:id="@+id/input" 
 
android:layout_width="match_parent" 
 
android:layout_height="wrap_content" 
 
android:hint="@string/chinese" 
 
android:text="@string/chinese"/> 
 
<Button android:id="@+id/write" 
 
android:layout_width="wrap_content" 
 
android:layout_height="wrap_content" 
 
android:text="@string/write"/> 
 
<Button android:id="@+id/read" 
 
android:layout_width="wrap_content" 
 
android:layout_height="wrap_content" 
 
android:text="@string/read"/> 
 
<TextView android:id="@+id/content" 
 
android:layout_width="wrap_content" 
 
android:layout_height="wrap_content" 
 
/> 
 
</LinearLayout> 

注意:需要加入文件讀寫權(quán)限

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


避免讀寫中文亂碼

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Android neon 優(yōu)化實(shí)踐示例

    Android neon 優(yōu)化實(shí)踐示例

    這篇文章主要為大家介紹了Android neon 優(yōu)化實(shí)踐示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Android實(shí)現(xiàn)秒表功能

    Android實(shí)現(xiàn)秒表功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡易秒表功能,具備啟停功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Android垂直滾動控件ScrollView使用方法詳解

    Android垂直滾動控件ScrollView使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Android垂直滾動控件ScrollView的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android無需權(quán)限調(diào)起系統(tǒng)相機(jī)

    Android無需權(quán)限調(diào)起系統(tǒng)相機(jī)

    在進(jìn)行一些小型APP的開發(fā),或者是對拍照界面沒有自定義要求時,我們可以用調(diào)起系統(tǒng)相機(jī)的方式快速完成拍照需求
    2023-03-03
  • Android View事件分發(fā)和消費(fèi)源碼簡單理解

    Android View事件分發(fā)和消費(fèi)源碼簡單理解

    這篇文章主要介紹了Android View事件分發(fā)和消費(fèi)源碼簡單理解的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • android監(jiān)聽軟鍵盤的彈出與隱藏的示例代碼

    android監(jiān)聽軟鍵盤的彈出與隱藏的示例代碼

    本篇文章主要介紹了android監(jiān)聽軟鍵盤的彈出與隱藏的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • android自定義控件實(shí)現(xiàn)簡易時間軸(2)

    android自定義控件實(shí)現(xiàn)簡易時間軸(2)

    這篇文章主要為大家詳細(xì)介紹了android自定義控件實(shí)現(xiàn)簡易時間軸的第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 總結(jié)Android App內(nèi)存優(yōu)化之圖片優(yōu)化

    總結(jié)Android App內(nèi)存優(yōu)化之圖片優(yōu)化

    網(wǎng)上有很多大拿分享的關(guān)于Android性能優(yōu)化的文章,主要是通過各種工具分析,使用合理的技巧優(yōu)化APP的體驗,提升APP的流暢度,但關(guān)于內(nèi)存優(yōu)化的文章很少有看到。下面是我在實(shí)踐過程中使用的一些方法,很多都是不太成熟的項目,只是將其作為一種處理方式分享給大家。
    2016-08-08
  • Android自定義環(huán)形LoadingView效果

    Android自定義環(huán)形LoadingView效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義環(huán)形LoadingView效果的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 一文詳解Android?FCM接入

    一文詳解Android?FCM接入

    這篇文章主要為大家介紹了Android?FCM接入詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03

最新評論