Android에서 중국어 문자를 읽는 파일 읽기 관련 설명

1. assets/license를 어떻게 표시합니까?txt (중국어) 내용?(1) 메서드 1: InputStream.available () 는 바이트 수를 얻어 한 번에 읽습니다
 
private String readUserAgreementFromAsset(String assetName) {
String content ="";
try {
InputStream is= getAssets().open(assetName);
if (is != null){
DataInputStream dIs = newDataInputStream(is);
intlength = dIs.available();
byte[] buffer = new byte[length];
dIs.read(buffer);
content= EncodingUtils.getString(buffer, "UTF-8");
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
(2) 방법 2: BufferedReader를 사용합니다.readLine() 행 읽기 및 줄 바꿈, 마지막으로 StringBuilder.append () 는 문자열로 연결됩니다.A. 다음은 UTF8:
 
private String readUserAgreementFromAsset(String assetName) {
StringBuilder sb = newStringBuilder("");
String content ="";
try {
InputStream is= getAssets().open(assetName);
if (is != null){
BufferedReader d = newBufferedReader(new InputStreamReader(is));
while (d.ready()) {
sb.append(d.readLine() +"
");
}
content =EncodingUtils.getString(sb.toString().getBytes(), "UTF-8");
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
B. 다음은 InputStreamReader에서 UTF8로 파일을 읽도록 지정한 다음 읽기 작업을 수행합니다.또한 UTF8 코드는 new String(buffer, "utf-8")을 사용할 수 있습니다.(3) 대체 방법 3: license를txt 내용은string입니다.xml의string, 예를 들어: 사용자 프로토콜 1, 서비스 조항의 확인과 수용... 에서 주의해야 할 것은string에 줄 바꾸기를 추가해야 한다는 것입니다. 원래 txt의 줄 바꾸기는string을 얻은 후에 무효입니다.사용할 수 없는 방법 4: 4096바이트를 읽을 때마다 UTF8로 코드를 바꾸고 문자열을 연결합니다.한자가 끊어질 수 있기 때문에 4096의 배수 부근의 중국어에 난자가 발생할 수 있다..
 
private String readUserAgreementFromAsset(String assetName) {
StringBuilder sb = newStringBuilder("");
String content ="";
try {
InputStream is= getAssets().open(assetName);
if (is != null){
BufferedReaderd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while(d.ready()) {
sb.append(d.readLine() +"
");
}
content= sb.toString();
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
https://www.jb51.net/kf/201207/140312.html http://blog.sina.com.cn/s/blog_933d50ba0100wq1h.html2. Android에서 읽기와 쓰기 파일 (1) 리소스의 raw 폴더에서 파일을 가져와 데이터를 읽습니다. (자원 파일은 읽기만 하고 쓸 수 없습니다.\res\raw\test.txt)
 
private String readUserAgreementFromAsset(String assetName) {
StringBuilder sb = newStringBuilder("");
String content ="";
try {
InputStream is= getAssets().open(assetName);
if (is != null){
DataInputStream dIs = new DataInputStream(is);
byte[] buffer = new byte[1024*4];
int length = 0;
while ((length = dIs.read(buffer)) >0) {
content =EncodingUtils.getString(buffer, 0, length, "UTF-8");
sb.append(content);
}
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
(2) asset에서 파일을 가져와 데이터를 읽습니다(자원 파일은 읽기만 하고 쓸 수 없습니다.\assets\test.txt)는 raw 폴더와 유사합니다. 단지: InputStream is = getAssets().open(“test.txt”); (3) 개인 폴더 아래의 파일 액세스 (/data/data/패키지 이름/files/test.txt) 는 openFileOutput을 사용하여 파일을 씁니다
 
String res = "";
try{
InputStream in = getResources().openRawResource(R.raw.test);
int length = in.available();
byte [] buffer = newbyte[length];
in.read(buffer);
res = EncodingUtils.getString(buffer,"UTF-8");// ,
in.close();
}catch(Exception e){
e.printStackTrace();
}
openFileInput을 사용하여 파일 읽기:
 
public void writeFileData(String fileName,String message){
try{
FileOutputStream fout =openFileOutput(fileName,MODE_PRIVATE);
byte [] bytes =message.getBytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){
e.printStackTrace();
}
}
(4) sdcard 디렉터리의 파일 액세스 (/mnt/sdcard/) FileOutputStream을 사용하여 파일을 작성합니다
 
public String readFileData(String fileName){
String str = “”;
try{
FileInputStream fin =openFileInput(fileName);
int length = in.available();
byte [] bytes = newbyte[length];
fin.read(bytes);
str = EncodingUtils.getString(bytes,"UTF-8");
fin.close();
}
catch(Exception e){
e.printStackTrace();
}
return str;
}
FileInputStream을 사용하여 파일 읽기:
 
public void writeFile2Sdcard(String fileName,String message){
try{
FileOutputStream fout = new FileOutputStream(fileName);
byte [] bytes =message.getBytes();
fout.write(bytes);
fout.close();
}
catch(Exception e){
e.printStackTrace();
}
}
http://dev.10086.cn/cmdn/wiki/index.php?doc-view-6017.html http://blog.sina.com.cn/s/blog_4d25c9870100qpax.html

좋은 웹페이지 즐겨찾기