2013-05-12 86 views
0

我無法在手機上找到該文件。我想要一個桌面應用程序,可以讀取我用手機保存的文件(加速度計數據),並且因爲我對android開發非常陌生,所以我決定最簡單的方法就是在文件系統中找到它,然後打開它通過我的桌面上:SerializeObject.WriteSettings在哪裏寫入文件? (路徑)

SerializeObject.ReadSettings(this, "activityLibrary.dat"); 

我通過將文件保存在我的應用程序:

SerializeObject.WriteSettings(this, ser, "activityLibrary.dat"); 

我試圖尋找與ES瀏覽器的應用程序文件系統,但它並沒有找到我的文件。嘗試與Windows搜索以及。任何人都可以幫忙嗎?或者建議在我的手機和桌面之間移動此文件的另一種簡單易懂的方式?

編輯:

我使用這個類對象序列化:

import java.io.BufferedReader; 
import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.OutputStreamWriter; 
import java.io.Serializable; 

import android.content.Context; 
import android.util.Base64InputStream; 
import android.util.Base64OutputStream; 
import android.util.Log; 

/** 
* Take an object and serialize and then save it to preferences 
* @author John Matthews 
* 
*/ 
public class SerializeObject { 
private final static String TAG = "SerializeObject"; 

/** 
* Create a String from the Object using Base64 encoding 
* @param object - any Object that is Serializable 
* @return - Base64 encoded string. 
*/ 
public static String objectToString(Serializable object) { 
    ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    try { 
     new ObjectOutputStream(out).writeObject(object); 
     byte[] data = out.toByteArray(); 
     out.close(); 

     out = new ByteArrayOutputStream(); 
     Base64OutputStream b64 = new Base64OutputStream(out,0); 
     b64.write(data); 
     b64.close(); 
     out.close(); 

     return new String(out.toByteArray()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

/** 
* Creates a generic object that needs to be cast to its proper object 
* from a Base64 ecoded string. 
* 
* @param encodedObject 
* @return 
*/ 
public static Object stringToObject(String encodedObject) { 
    try { 
     return new ObjectInputStream(new Base64InputStream(
       new ByteArrayInputStream(encodedObject.getBytes()), 0)).readObject(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

/** 
* Save serialized settings to a file 
* @param context 
* @param data 
*/ 
public static void WriteSettings(Context context, String data, String filename){ 
    FileOutputStream fOut = null; 
    OutputStreamWriter osw = null; 

    try{ 
     fOut = context.openFileOutput(filename, Context.MODE_PRIVATE);  
     osw = new OutputStreamWriter(fOut); 
     osw.write(data); 
     osw.flush(); 
     //Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show(); 
    } catch (Exception e) {  
     e.printStackTrace(); 
     // Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show(); 
    } 
    finally { 
     try { 
      if(osw!=null) 
       osw.close(); 
      if (fOut != null) 
       fOut.close(); 
     } catch (IOException e) { 
       e.printStackTrace(); 
     } 
    } 
} 

/** 
* Read data from file and put it into a string 
* @param context 
* @param filename - fully qualified string name 
* @return 
*/ 
public static String ReadSettings(Context context, String filename){ 
    StringBuffer dataBuffer = new StringBuffer(); 
    try{ 
     // open the file for reading 
     InputStream instream = context.openFileInput(filename); 
     // if file the available for reading 
     if (instream != null) { 
      // prepare the file for reading 
      InputStreamReader inputreader = new InputStreamReader(instream); 
      BufferedReader buffreader = new BufferedReader(inputreader); 

      String newLine; 
      // read every line of the file into the line-variable, on line at the time 
      while ((newLine = buffreader.readLine()) != null) { 
       // do something with the settings from the file 
       dataBuffer.append(newLine); 
      } 
      // close the file again 
      instream.close(); 
     } 

    } catch (java.io.FileNotFoundException f) { 
     // do something if the myfilename.txt does not exits 
     Log.e(TAG, "FileNot Found in ReadSettings filename = " + filename); 
     try { 
      context.openFileOutput(filename, Context.MODE_PRIVATE); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 
    } catch (IOException e) { 
     Log.e(TAG, "IO Error in ReadSettings filename = " + filename); 
    } 

    return dataBuffer.toString(); 
} 

} 
+0

有Android中沒有的Java方法以大寫字母開始,並沒有'WriteSettings()'方法在Android中的任何資本。也許你可能會考慮爲你自己的'WriteSettings()'方法發佈代碼,或者改變你的標籤以刪除'java'並添加你正在使用的實際編程環境。 – CommonsWare 2013-05-12 17:44:24

+0

我添加了我用於序列化的類。 – LucasSeveryn 2013-05-12 17:49:34

回答

1

openFileOutput保存文件/data/data/your.package.name/files/目錄。

使用Context.getExternalFilesDirEnvironment.getExternalStorageDirectory輕鬆訪問文件複製到電腦或郵寄自己。

+0

@LucasSeveryn:請注意,您不能訪問設備上的此目錄,除非通過設備生成或使用'adb shell'。如果您希望能夠將文件傳輸到桌面,請使用外部存儲。 – CommonsWare 2013-05-12 17:54:08

+0

你有沒有關於如何將這個文件保存在外部存儲上的提示? – LucasSeveryn 2013-05-12 17:55:27

相關問題