从多个活动访问文件。
使我在一个活动(例如 Filewriter.java)中创建的文件可用于我的应用程序中的所有其他活动(用于数据操作)的最佳方法是什么?我已确保使用相同的文件名,但如何确保我在各处访问同一个文件?我需要在 2 个文件上实现这一点,一个包含字符串名称,另一个包含整数。我已经使用 writeUTF() 创建了字符串文件;
下面是从前一个活动(UI)接收数据并将内容写入文件的代码。我想从另一个 UI 活动访问同一个文件...
package com.shuaib669.bunkrecord;
import java.io.DataOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class filewriter extends Activity{
String[] newText = new String[7];
String[] newNum = new String[7];
int[] no_of_classes = new int[7];
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
newText = extras.getStringArray("com.shuaib669.bunkrecord.thetext");
newNum = extras.getStringArray("com.shuaib669.bunkrecord.thenum");
finish();
if (newText != null && newNum != null){
try {
// open subject.txt and classes.bin for writing
DataOutputStream out1 = new DataOutputStream(openFileOutput("subject.txt", Context.MODE_WORLD_READABLE));
DataOutputStream out2 = new DataOutputStream(openFileOutput("classses.bin", Context.MODE_WORLD_READABLE));
for(int x=0;x<7;x++){
try{
//converting string representation of no.s into integers
no_of_classes[x]=Integer.parseInt(newNum[x]);
}
catch(Exception e){
e.printStackTrace();
}
}
for(int x=0;x<7;x++){
if(newText[x]==null)
out1.writeUTF("\n");
else if(newText[x]==" " || newText[x]=="\n" || newText[x]=="\t")
out1.writeUTF("\n");
else
out1.writeUTF(newText[x]);
if(newNum[x]==null)
out2.writeInt((Integer) null);
else
out2.writeInt(no_of_classes[x]);
}
out1.close();
}
catch(IOException e){
Log.e("Data Input Sample", "I/O Error");
}
}
startNextMatchingActivity(new Intent("com.shuaib669.bunkrecord.SUBLIST"));
//Sublist being the activity that displays the stored contents of the file and lets user manipulate the file classes.bin.
}
}
我的主要困境是能够访问同一个文件(无论是 subject.txt 还是classes.bin)并能够读取和操作来自任何其他文件的数据同一应用程序中的活动。
What is the best way to make a file i've created in one activity (say Filewriter.java) to be available(for data manipulation) to all other activities in my app? I've made sure to use the same filename but how do i ensure that i'm accessing the one same file everywhere? I need to implement this on 2 files, one containing string names and the other integers. I've created the string file using writeUTF();
here is the code for receiving data from a previous activity(the UI) and writing the contents to the file. I want to access this same file from another UI activity...
package com.shuaib669.bunkrecord;
import java.io.DataOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class filewriter extends Activity{
String[] newText = new String[7];
String[] newNum = new String[7];
int[] no_of_classes = new int[7];
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
newText = extras.getStringArray("com.shuaib669.bunkrecord.thetext");
newNum = extras.getStringArray("com.shuaib669.bunkrecord.thenum");
finish();
if (newText != null && newNum != null){
try {
// open subject.txt and classes.bin for writing
DataOutputStream out1 = new DataOutputStream(openFileOutput("subject.txt", Context.MODE_WORLD_READABLE));
DataOutputStream out2 = new DataOutputStream(openFileOutput("classses.bin", Context.MODE_WORLD_READABLE));
for(int x=0;x<7;x++){
try{
//converting string representation of no.s into integers
no_of_classes[x]=Integer.parseInt(newNum[x]);
}
catch(Exception e){
e.printStackTrace();
}
}
for(int x=0;x<7;x++){
if(newText[x]==null)
out1.writeUTF("\n");
else if(newText[x]==" " || newText[x]=="\n" || newText[x]=="\t")
out1.writeUTF("\n");
else
out1.writeUTF(newText[x]);
if(newNum[x]==null)
out2.writeInt((Integer) null);
else
out2.writeInt(no_of_classes[x]);
}
out1.close();
}
catch(IOException e){
Log.e("Data Input Sample", "I/O Error");
}
}
startNextMatchingActivity(new Intent("com.shuaib669.bunkrecord.SUBLIST"));
//Sublist being the activity that displays the stored contents of the file and lets user manipulate the file classes.bin.
}
}
My main dilemma is to be able to access the SAME one file (whether subject.txt or classes.bin) and to be able to read and manipulate the data from any other activity in the same app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这里的困境是什么 - 如果您使用相同的文件名,并且可能使用相同的路径名,那么根据定义 - 它是相同的文件。
只要您的所有活动都在同一个应用程序/包中,那么我认为您不会遇到任何问题。
因此 - 对两个文件使用相同的完整路径名,您不必担心它们是否是同一个文件。
我建议的是,您可能想要使用
openFileOutput("subject.txt", Context.MODE_WORLD_READABLE)
(这是相对的,您可能不知道文件在哪里),而不是使用 < code>openFileOutput(getFilesDir ( ) + "/" + "subject.txt", Context.MODE_WORLD_READABLE) 这将始终为您提供应用程序私有目录中的完整路径名内部存储器上的数据。我通常在我的应用程序中提供一个辅助方法:
I'm not sure what the dilemma is here - if you're using the same filename, and presumably the same pathname, then by definition - it's the same file.
So long as all your activities are in the same app/package, then I don't see that you would have any problems.
So - use the same full pathname for each of the two files, and you needn't worry about whether they are the same file.
What I would recommend is, instead of using
openFileOutput("subject.txt", Context.MODE_WORLD_READABLE)
(which is relative, and you may not know where the file is), you probably want to useopenFileOutput(getFilesDir ( ) + "/" + "subject.txt", Context.MODE_WORLD_READABLE)
which will always provide you with a full pathname within your app's private data on internal storage.I usually provide a helper method in my apps:
我建议使用
Application
类使您可以从您的所有活动中访问该文件。您可以扩展Application
类,并在自定义Application
类中创建您的DataOutputStream
,并将其设为公共或通过 getter 进行访问,getOutputStream()
。然后,在您的活动中,您可以执行以下操作来访问
OutputStream
:您还可以在
Application
中声明用于打开或关闭OutputStream
的实现方法完成写入文件后。I would recommend using the
Application
class for making the file accessible from all your activities within your. You can extend theApplication
class, and in your customApplication
class you create yourDataOutputStream
, and make it either public or accessible through a getter,getOutputStream()
.Then in your activities you can do the following for accessing the
OutputStream
:You can also declare in your
Application
implementation methods for opening or closing theOutputStream
once you are done with writing to the file.