2015-04-13 71 views
-1

我一直在試圖創建一個名爲TextFileReaderWriter的類我想使用getters和setters來讀取和寫入文本文件,以便我可以調用類,並通過簡單地使用setfileContents(somestring)somestring = getfileContents()像這樣 例如在程序的任何地方的方法:在Android Studio中從我自己的類讀取和寫入文本文件

TextFileReaderWriter trw = new TextFileReaderWriter(); 
trw.setfileContents(somestring); //this would write 'somestring' to the text file. 
String somestring = trw.getfileContents(); //this would return 'somestring' from the text file. 

這裏就是我有這麼遠,但它沒有寫入文件:

public class TextFileReaderWriter extends Activity{ 

      String fileContents; 
      Context context; 
      String TAG = "MYTAG"; 

    public TextFileReaderWriter(String fileContents, Context context) { 
     this.fileContents = fileContents; 
     this.context = context; 
    } 

    public String getFileContents() { 
     return fileContents; 
    } 

    public void setFileContents(String fileContents) { 
     this.fileContents = fileContents; 

     FileOutputStream fos = null; 
     try { 
      fos = context.openFileOutput("UserInputStore", Context.MODE_PRIVATE); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     OutputStreamWriter osw = new OutputStreamWriter(fos); 

     try { 
      osw.write(fileContents); 
      Log.d(TAG, fileContents); 

     } catch (IOException e) { 
      e.printStackTrace(); 

     } 


    } 

} 
+0

你的logcat的說?無論如何,首先你的getfilecontent沒有讀你的文件,它讀取你的變量filecontent,其次你寫在你的應用程序的私人文件夾 –

+0

你是故意試圖存儲它的內部存儲,還是你的意思是去外部?另外,你的'getFileContents()'實際上不會返回文件內容;當您重新啓動應用程序(或活動,甚至)時,數據將被gc'd。 –

+0

我還沒有編寫getFileContents到目前爲止,我只使用setFileContents方法....現在的問題是,它不會寫入我將它傳遞給文件的字符串。它確實創建了文件,但它是空的。 – CJG

回答

0

你d on't需要OutputStreamWriter - FileOutputStream將做的伎倆就好了。

//what you had before 
    FileOutputStream fos = null; 
    try { 
     fos = context.openFileOutput(filename, Context.MODE_PRIVATE); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    //use just the file output stream to write the data 
    //data here is a String 
    if (fos != null) { 
     try { 
      fos.write(data.getBytes()); 
      fos.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
0

方法保存在磁盤上的數據:

protected static void saveDataOnDisk(String data) { 

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 

try { 

ObjectOutput objectOutput = new ObjectOutputStream(byteArrayOutputStream); 

objectOutput.writeObject(data); 

byte[] buffer = byteArrayOutputStream.toByteArray(); 

File loginDataFile = (new File(filePath)); // file path where you want to write your data 

loginDataFile.createNewFile(); 

FileOutputStream fileOutputStream = new FileOutputStream(loginDataFile); 

fileOutputStream.write(buffer); 

fileOutputStream.close(); 

objectOutput.flush(); 

objectOutput.close(); 

byteArrayOutputStream.flush(); 

byteArrayOutputStream.close(); 

Log.i(「SAVE」, 」———————-DONE SAVING」); 

} catch(IOException ioe) { 

Log.i(「SAVE」, 「———serializeObject|」+ioe); 

} 

} 

方法從磁盤讀取數據:

private static Object getDataFromDisk() { 

try { 

FileInputStream fileInputeStream = new FileInputStream(FilePath); 

ObjectInputStream objectInputStream = new ObjectInputStream(fileInputeStream); 

Object data = (Object) objectInputStream.readObject(); 

objectInputStream.close(); 

fileInputeStream.close(); 

return dataModel; 

} catch (Exception error) { 

Log.i(「FETCH」, 」—-getDataFromDisk———ERROR while reading|」 + error); 

} 

return null; 

}