这是我正在做的事情:
1-从我的资料中获取文件并将其放入字符串 - SUCCESSFUL
2-在getFilesDir()
制作文件 - 失败(尚未崩溃)
3-使用FileWriter
- FAILS(尚未崩溃)将我收到的字符串写入此文件
4-阅读我刚刚创建的新文件 - FAILS(抛出NoSuchElementException)
以下是你要看的内容:
我使用的一个小方法:
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>
堆栈追踪:
03-11 21:23:08.241: E/AndroidRuntime(3958): FATAL EXCEPTION: main
03-11 21:23:08.241: E/AndroidRuntime(3958): java.util.NoSuchElementException
03-11 21:23:08.241: E/AndroidRuntime(3958): at java.util.Scanner.next(Scanner.java:1007)
03-11 21:23:08.241: E/AndroidRuntime(3958): at java.util.Scanner.next(Scanner.java:980)
03-11 21:23:08.241: E/AndroidRuntime(3958): at seaskyways.testingproject.MainActivity.inputStreamToString(MainActivity.java:44)
03-11 21:23:08.241: E/AndroidRuntime(3958): at seaskyways.testingproject.MainActivity$7.onLongClick(MainActivity.java:171)
03-11 21:23:08.241: E/AndroidRuntime(3958): at android.view.View.performLongClick(View.java:4240)
03-11 21:23:08.241: E/AndroidRuntime(3958): at android.widget.TextView.performLongClick(TextView.java:8060)
03-11 21:23:08.241: E/AndroidRuntime(3958): at android.view.View$CheckForLongPress.run(View.java:17339)
03-11 21:23:08.241: E/AndroidRuntime(3958): at android.os.Handler.handleCallback(Handler.java:725)
03-11 21:23:08.241: E/AndroidRuntime(3958): at android.os.Handler.dispatchMessage(Handler.java:92)
03-11 21:23:08.241: E/AndroidRuntime(3958): at android.os.Looper.loop(Looper.java:137)
03-11 21:23:08.241: E/AndroidRuntime(3958): at android.app.ActivityThread.main(ActivityThread.java:5237)
03-11 21:23:08.241: E/AndroidRuntime(3958): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 21:23:08.241: E/AndroidRuntime(3958): at java.lang.reflect.Method.invoke(Method.java:511)
03-11 21:23:08.241: E/AndroidRuntime(3958): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
03-11 21:23:08.241: E/AndroidRuntime(3958): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
03-11 21:23:08.241: E/AndroidRuntime(3958): at dalvik.system.NativeStart.main(Native Method)
请不要解决,因为我有未来的计划......感谢您的帮助!如果您需要更多信息,请点击几厘米:P
答案 0 :(得分:1)
从this documentation我明白你应该用.hasNext()保护你的.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"/>
答案 1 :(得分:0)
我说:“boolean b = file.createNewFile(); //返回false,表示文件未被创建” 这不是真的,b = false没有任何异常意味着文件已经被制作了...但是崩溃是因为创建的文件没有任何要读取的文本,这将导致崩溃,因为我什么都没读......
所以要纠正它,我不需要任何权限,只需要Java ...所有我要做的就是在尝试阅读之前写一些文件或编写我的类,这给了我一个{{1} String
的{{1}}返回的东西而不是什么都没有崩溃:
所以第一个解决方案是这样的:
InputStream
第二个似乎更容易的解决方案就是:
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);