Android 将文件写入GetFileDir()失败-NoTouchElementException
以下是我正在做的: 1-从我的资产中获取文件并将其放入字符串-成功 2-在Android 将文件写入GetFileDir()失败-NoTouchElementException,android,file,fileinputstream,filewriter,Android,File,Fileinputstream,Filewriter,以下是我正在做的: 1-从我的资产中获取文件并将其放入字符串-成功 2-在getFilesDir()中生成文件-失败(尚未崩溃) 3-使用文件编写器将我获得的字符串写入此文件-失败(尚未崩溃) 4-读取我刚刚创建的新文件-失败(抛出NoTouchElementException) 以下是你必须看到的东西: 我使用的一个小而有用的方法: public String inputStreamToString(InputStream stream){ Scanner s = new S
getFilesDir()
中生成文件-失败(尚未崩溃)
3-使用文件编写器将我获得的字符串写入此文件
-失败(尚未崩溃)
4-读取我刚刚创建的新文件-失败(抛出NoTouchElementException)
以下是你必须看到的东西:
我使用的一个小而有用的方法:
public String inputStreamToString(InputStream stream){
Scanner s = new Scanner(stream).useDelimiter("\\A");
String aa = s.next();
return aa;
}
MainActivity.java的代码段:
//B2 and text are a well defined Button and TextView respectively, ensured through previous testings
B2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AssetManager as = getAssets();
InputStream stream = null;
try {
stream = as.open("updater-script");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Scanner s = new Scanner(stream).useDelimiter("\\A");
String aa = s.next();
text.setText(aa);
File file = new File(getFilesDir() + "/updater-script");
try {
boolean b = file.createNewFile(); //returns false, indicating the file wasn't made
text.setText(text.getText()+String.valueOf(b));
new File(getFilesDir().getAbsolutePath()).mkdirs();
FileWriter write = new FileWriter(getFilesDir().toString()+File.pathSeparator+"updater-script");
Show(getFilesDir().toString());
write.write(aa);
write.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
B2.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
File file = new File(getFilesDir(), "updater-script");
try {
FileInputStream in = new FileInputStream(file);
String aa = inputStreamToString(in);
text.setText(aa);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
});
我的舱单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="seaskyways.testingproject"
android:versionCode="1"
android:versionName="beta" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STROAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock" >
<activity
android:name="seaskyways.testingproject.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo"
android:uiOptions="splitActionBarWhenNarrow" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="seaskyways.testingproject.MenuActivity"
android:label="@string/title_activity_menu" >
</activity>
<activity
android:name="seaskyways.testingproject.Splash"
android:label="@string/title_activity_splash"
android:theme="@style/Theme.Sherlock.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="seaskyways.testingproject.HorizontalScrollView"
android:label="@string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.HSV" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="seaskyways.testingproject.ExpandableLists"
android:label="@string/title_activity_expandable_lists"
android:parentActivityName="seaskyways.testingproject.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="seaskyways.testingproject.MainActivity" />
</activity>
<activity
android:name="seaskyways.testingproject.SwipeStrips"
android:label="@string/title_activity_swipe_tabs"
android:parentActivityName="seaskyways.testingproject.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="seaskyways.testingproject.MainActivity" />
</activity>
<activity
android:name="seaskyways.testingproject.SwipeTabs"
android:label="@string/title_activity_swipe_tabs"
android:parentActivityName="seaskyways.testingproject.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="seaskyways.testingproject.MainActivity" />
</activity>
</application>
</manifest>
请不要采取变通办法,因为我有未来的计划。。。谢谢你的帮助!如果您需要更多信息,请点击此处,注释框在下方几厘米处:P,据我所知,您实际上应该使用保护.next()调用
例如,在这里:public String inputStreamToString(InputStream stream){
Scanner s = new Scanner(stream).useDelimiter("\\A");
if (s.hasNext()){
return s.next();
}else{
return null;
}
}
而且,如果您的权限写入正确,则
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STROAGE"/>
权限实际上并不存在。应该是
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
据我所知,您实际上应该使用保护.next()调用
例如,在这里:
public String inputStreamToString(InputStream stream){
Scanner s = new Scanner(stream).useDelimiter("\\A");
if (s.hasNext()){
return s.next();
}else{
return null;
}
}
而且,如果您的权限写入正确,则
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STROAGE"/>
权限实际上并不存在。应该是
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
我说:“boolean b=file.createNewFile();//返回false,表示文件未生成”
这不是真的,b=false没有任何异常意味着该文件已经生成。。。但是崩溃是因为创建的文件没有任何文本可读取,这将导致崩溃,因为我什么也没读
所以要更正它,我不需要任何权限,只需要Java。。。我所要做的就是在尝试读取文件之前向该文件写入一些内容,或者对我的类进行编程,该类为我提供一个InputStream
的String
,以返回一些内容,而不是什么都没有,然后崩溃:
第一个解决方案是这样的:
File file = new File(get_data_dir(getApplicationContext()), "updater-script");
try {
InputStream stream = null;
try {
stream = getAssets().open("updater-script");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileOutputStream ou = new FileOutputStream(file);
byte[] buffer = inputStreamToString(stream).getBytes();
ou.write(buffer);//Now the file has something to be read ...
FileInputStream in = new FileInputStream(file);
String aa = inputStreamToString(in);
第二个似乎更容易的解决方案是:
public String inputStreamToString(InputStream stream){
String aa;
Scanner s = new Scanner(stream).useDelimiter("\\A");
if(s.hasNext()){
aa = s.next();
}else{
aa = "";
}
return aa;//Now it returns something no matter what ...
}
我说:“boolean b=file.createNewFile();//返回false,表示文件没有生成”
这不是真的,b=false没有任何异常意味着该文件已经生成。。。但是崩溃是因为创建的文件没有任何文本可读取,这将导致崩溃,因为我什么也没读
所以要更正它,我不需要任何权限,只需要Java。。。我所要做的就是在尝试读取文件之前向该文件写入一些内容,或者对我的类进行编程,该类为我提供一个InputStream
的String
,以返回一些内容,而不是什么都没有,然后崩溃:
第一个解决方案是这样的:
File file = new File(get_data_dir(getApplicationContext()), "updater-script");
try {
InputStream stream = null;
try {
stream = getAssets().open("updater-script");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FileOutputStream ou = new FileOutputStream(file);
byte[] buffer = inputStreamToString(stream).getBytes();
ou.write(buffer);//Now the file has something to be read ...
FileInputStream in = new FileInputStream(file);
String aa = inputStreamToString(in);
第二个似乎更容易的解决方案是:
public String inputStreamToString(InputStream stream){
String aa;
Scanner s = new Scanner(stream).useDelimiter("\\A");
if(s.hasNext()){
aa = s.next();
}else{
aa = "";
}
return aa;//Now it returns something no matter what ...
}
您的logcat实际上表明您的错误在s.next();布尔b=file.createNewFile()//返回false,表示文件未创建-读取
createNewFile()
的文档如果无法创建文件,它将抛出IOException
。如果您在调用时得到false
作为返回,并且没有异常,那么这是因为文件已经存在。或者,正如我修改后的答案所指出的,是因为权限有点错误…@DigCamara:getFilesDir()
返回内部存储器上的路径,因此不需要向外部存储器写入的权限。但是你在清单中的输入错误是正确的。好吧,如果最后一个建议失败,我将把这个问题留给其他人的建议,他们也无法使用这些说明创建文件:你的日志实际上表明你的错误在s.next();布尔b=file.createNewFile()//返回false,表示文件未创建-读取createNewFile()
的文档如果无法创建文件,它将抛出IOException
。如果您在调用时得到false
作为返回,并且没有异常,那么这是因为文件已经存在。或者,正如我修改后的答案所指出的,是因为权限有点错误…@DigCamara:getFilesDir()
返回内部存储器上的路径,因此不需要向外部存储器写入的权限。但是你在清单中的输入错误是正确的。好吧,如果最后一个建议失败,我将把这个问题留给其他人的建议,他们也无法使用这些说明创建文件:如果你阅读了我写的内容,你应该知道我的堆栈跟踪指向我的s.next()
因为布尔b=file.createNewFile()代码>返回false,表示文件未创建,意味着我的扫描仪找不到任何“next()”,导致异常。。。这不是答案,这会导致我有一个NullPointerException…你是对的。我在我的答案中添加了一个新的部分,这实际上可能解释了为什么您没有创建文件的权限。如果这还不行,我很乐意收回我的答案。你可能需要清理项目并重新安装。Eclipse不太擅长检测对清单所做的更改(当然,如果您正在使用Eclipse),如果您阅读了我写的内容,您应该知道我的堆栈跟踪指向我的s.next()
,因为boolean b=file.createNewFile()代码>返回false,表示文件未创建,意味着我的扫描仪找不到任何“next()”,导致异常。。。这不是答案,这会导致我有一个NullPointerException…你是对的。我在我的答案中添加了一个新的部分,这实际上可能解释了为什么您没有创建文件的权限。如果这还不行,我很乐意收回我的答案。你可能需要清理项目并重新安装。Eclipse不太擅长检测对清单所做的更改(当然,如果您使用的是Eclipse)