gpt4 book ai didi

java - SerializeObject.WriteSettings 在哪里写入文件? (小路)

转载 作者:行者123 更新时间:2023-12-01 14:35:39 26 4
gpt4 key购买 nike

我在手机上找不到该文件。我想要一个桌面应用程序,可以读取我用手机保存的文件(加速度计数据),并且因为我对 Android 开发非常陌生,所以我决定最简单的方法就是在文件系统中找到它,然后打开通过以下方式将其放在我的桌面上:

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

我通过以下方式将文件保存在我的应用程序中:

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

我尝试使用 ES Explorer 应用程序搜索文件系统,但它没有找到我的文件。也尝试过 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();
}

}

最佳答案

openFileOutput 将文件保存在 /data/data/your.package.name/files/ 目录中。

使用Context.getExternalFilesDirEnvironment.getExternalStorageDirectory轻松访问文件以复制到计算机或邮寄给自己。

关于java - SerializeObject.WriteSettings 在哪里写入文件? (小路),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16510072/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com