在網上找了些 FileInputStream 的實作方法
方法 1
try {
FileInputStream fileInputStream=openFileInput(FILE_NAME);
byte[] buffer=new byte[1024];
fileInputStream.read(buffer);
String filecontentString=EncodingUtils.getString(buffer, "UTF-8");
textView.setText(filecontentString);
}
catch (Exception e) {
// TODO: handle exception
}
方法 2
try{
FileInputStream fileInputStream;
fileInputStream = openFileInput("myfile.txt");
byte[] readBytes =newbyte[fileInputStream.available()];
fileInputStream.read(readBytes);
String readString =newString(readBytes);
}
catch(FileNotFoundException e){
}
catch(IOException e){
}
方法 3
try {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(
openFileInput("DayTwentyTwoFile")));
String inputString;
StringBuffer stringBuffer = new StringBuffer();
while ((inputString = inputReader.readLine()) != null) {
stringBuffer.append(inputString + "\n");
}
textView.setText(stringBuffer.toString());
} catch (IOException e) {
e.printStackTrace();
}
方法 4
try {
FileInputStream fis;
fis = openFileInput("file_out.txt");
byte[] buffer = new byte[1024];
fis.read(buffer);
// 对读取的数据进行编码以防止乱码
String fileContent = EncodingUtils.getString(buffer, "UTF-8");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
方法 5
// ファイルを読み出し
public String readFile(String file) {
FileInputStream fileInputStream;
String text = null;
try {
fileInputStream = openFileInput(file);
String lineBuffer = null;
BufferedReader reader= new BufferedReader(new InputStreamReader(fileInputStream,"UTF-8"));
while( (lineBuffer = reader.readLine()) != null ) {
text = lineBuffer ;
}
} catch (IOException e) {
e.printStackTrace();
}
return text;
}
方法 6
String myData = "";
try {
FileInputStream fis = new FileInputStream(myInternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
myData = myData + strLine + "\n";
}
in.close();
}catch(Exception e){}
就是用FileInputStream讀取,然後每行每行讀出來,讀到沒有為止,最後記得一樣要關stream!
方法 7
private void reader() {
// TODO Auto-generated method stub
// 檔案輸入到buffered
FileInputStream login = null;
BufferedInputStream buffered = null;
try {
login = openFileInput("file.txt");
buffered = new BufferedInputStream(login);
// 這邊應該是設定一次取出的範圍大小20byte
byte[] buffbyte = new byte[20];
content.setText("");
// 讀取資料直到檔案結束
do {
int flag = buffered.read(buffbyte);
//if 已經取不到資料了
if (flag == -1) {
break;
} else {
//把所有輸入的buffered丟到content
content.append(new String(buffbyte), 0, flag);
}
} while (true);
buffered.close();
} catch (Exception e) {
e.printStackTrace();
}
}
方法 8
try {
FileInputStream fis=openFileInput(filename);
InputStreamReader isr=new InputStreamReader(fis);
//使用BufferedReader可以使用readLine()方法非常好用,並可以提升效率
BufferedReader br=new BufferedReader(isr);
String str1="",str2="";
//Java常見用法
while((str1=br.readLine())!=null){
str2+=str1;
show.setText("儲存之值"+str2);
br.close( );
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
方法 9
try {
FileInputStream fis = openFileInput(file.getPath());
int temp;
while((temp = fis.read()) !=-1){
FP.p(String.valueOf(((char)temp)));
}
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
方法 10
//取得內部儲存體擺放檔案的目錄
//預設擺放目錄為 /data/data/[package.name]/
File dir = context.getFilesDir();
//開啟或建立該目錄底下檔名為 "test.txt" 的檔案
File inFile = new File(dir, "test.txt");
//讀取 /data/data/com.myapp/test.txt 檔案內容
String data = readFromFile(inFile);
//readFromFile 方法如下
private String readFromFile(File fin) {
StringBuilder data = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(fin), "utf-8"));
String line;
while ((line = reader.readLine()) != null) {
data.append(line);
}
} catch (Exception e) {
} finally {
try {
reader.close();
} catch (Exception e) {
}
}
return data.toString();
}
方法 11
FileInputStream in = null;
StringBuffer data = new StringBuffer();
try {
//開啟 getFilesDir() 目錄底下名稱為 test.txt 檔案
in = openFileInput("test.txt");
//讀取該檔案的內容
BufferedReader reader = new BufferedReader(
new InputStreamReader(in, "utf-8"));
String line;
while ((line = reader.readLine()) != null) {
data.append(line);
}
} catch (Exception e) {
} finally {
try {
in.close();
} catch (Exception e) {
}
}
方法 12
//Read file in Internal Storage
FileInputStream fis;
String content = "";
try {
fis = openFileInput(file);
byte[] input = new byte[fis.available()];
while (fis.read(input) != -1) {}
content += new String(input);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
方法 13
private String readDataFromFile(String filename){
String result = null;
try {
StringBuilder sb = new StringBuilder();
FileInputStream fin = this.openFileInput(filename);
byte[] data = new byte[fin.available()];
while (fin.read(data) != -1) {
sb.append(new String(data));
}
fin.close();
result = sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
以上程式碼全部於網上複製,未能一一貼出原址,還望見諒。