在内部存储器中创建目录并保存文件
Create a directory in the internal memory and save files
这个问题可能是重复的。我很抱歉。我已经浏览过许多类似的帖子,但无法在我的工作中完成。
在我的应用程序中,我想将 Json 数据存储在 .txt 文件中,并将它们存储在内存中的特定文件夹中。或者直接放在Android/data文件夹里也可以。
我使用了以下代码来执行此操作,但我无法找到我想要存储的文件。谁能告诉我我的代码有什么问题,我该如何更正它??
String fname, fcontent;
Context context;
ProgressDialog pDialog;
public SaveIntoFile(String fileName, String jsonStr, Context context, ProgressDialog pDialog) {
this.fname = fileName;
this.context = context;
this.fcontent = jsonStr;
this.pDialog = pDialog;
}
public void write() {
try {
File mydir = context.getDir("NepalJapan", Context.MODE_PRIVATE);
File file = new File(mydir, fname+".txt");
if (file.exists())
file.delete();
// If file does not exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(fcontent);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String read() throws IOException {
BufferedReader br = null;
String response = null;
StringBuffer output = new StringBuffer();
File mydir = context.getDir("NepalJapan", Context.MODE_PRIVATE);
File file = new File(mydir, fname+".txt");
if (!file.exists()) {
if (pDialog.isShowing())
pDialog.dismiss();
return null;
} else {
br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
output.append(line + "\n");
}
response = output.toString();
br.close();
return response;
}
}
下面是将 txt 文件放入内存的代码片段。要看文件。您必须 运行 在模拟器中测试您的应用程序,然后转到 DDMS -> 文件资源管理器 -> app_NepalJapan -> hello.txt
public static String write(Context context, String strfilename) {
String internalMemoryPath = "";
try {
File InternalMemoryRoot = context.getDir("NepalJapan", Context.MODE_PRIVATE);
final File folderPath = new File(InternalMemoryRoot+"");
Log.v("filePath","filePath: "+folderPath);
if(!folderPath.exists() && !folderPath.isDirectory()){
folderPath.mkdirs();
}
String filePath = folderPath +"/"+ strfilename;
Log.v("filePath","filePath: "+filePath);
File file = new File(filePath);
FileOutputStream fileOutput = new FileOutputStream(file);
// create a buffer...
byte[] buffer = new byte[16384];
int bufferLength = 1;
fileOutput.write(buffer, 0, bufferLength);
fileOutput.close();
internalMemoryPath = filePath;
} catch (Exception e) {
e.printStackTrace();
}
return internalMemoryPath;
}
在您的 activity 中调用方法:YourStaticUtilClass.write(context, "hello.txt");
如果您仍然发现任何困难,请告诉我。
这个问题可能是重复的。我很抱歉。我已经浏览过许多类似的帖子,但无法在我的工作中完成。
在我的应用程序中,我想将 Json 数据存储在 .txt 文件中,并将它们存储在内存中的特定文件夹中。或者直接放在Android/data文件夹里也可以。
我使用了以下代码来执行此操作,但我无法找到我想要存储的文件。谁能告诉我我的代码有什么问题,我该如何更正它??
String fname, fcontent;
Context context;
ProgressDialog pDialog;
public SaveIntoFile(String fileName, String jsonStr, Context context, ProgressDialog pDialog) {
this.fname = fileName;
this.context = context;
this.fcontent = jsonStr;
this.pDialog = pDialog;
}
public void write() {
try {
File mydir = context.getDir("NepalJapan", Context.MODE_PRIVATE);
File file = new File(mydir, fname+".txt");
if (file.exists())
file.delete();
// If file does not exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(fcontent);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String read() throws IOException {
BufferedReader br = null;
String response = null;
StringBuffer output = new StringBuffer();
File mydir = context.getDir("NepalJapan", Context.MODE_PRIVATE);
File file = new File(mydir, fname+".txt");
if (!file.exists()) {
if (pDialog.isShowing())
pDialog.dismiss();
return null;
} else {
br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
output.append(line + "\n");
}
response = output.toString();
br.close();
return response;
}
}
下面是将 txt 文件放入内存的代码片段。要看文件。您必须 运行 在模拟器中测试您的应用程序,然后转到 DDMS -> 文件资源管理器 -> app_NepalJapan -> hello.txt
public static String write(Context context, String strfilename) {
String internalMemoryPath = "";
try {
File InternalMemoryRoot = context.getDir("NepalJapan", Context.MODE_PRIVATE);
final File folderPath = new File(InternalMemoryRoot+"");
Log.v("filePath","filePath: "+folderPath);
if(!folderPath.exists() && !folderPath.isDirectory()){
folderPath.mkdirs();
}
String filePath = folderPath +"/"+ strfilename;
Log.v("filePath","filePath: "+filePath);
File file = new File(filePath);
FileOutputStream fileOutput = new FileOutputStream(file);
// create a buffer...
byte[] buffer = new byte[16384];
int bufferLength = 1;
fileOutput.write(buffer, 0, bufferLength);
fileOutput.close();
internalMemoryPath = filePath;
} catch (Exception e) {
e.printStackTrace();
}
return internalMemoryPath;
}
在您的 activity 中调用方法:YourStaticUtilClass.write(context, "hello.txt");
如果您仍然发现任何困难,请告诉我。