我收到errorjava.io.FileNotFoundException:removed / contact / contact.txt:打开失败:ENOENT(没有这样的文件或目录)每当我尝试创建文件时。有人可以告诉我,如果我给文件的路径正确。此外,我也在清单中提供了所需的许可
MainActivity
public class MainActivity extends Activity {
List<String> phone = new ArrayList<String>();
Context context =this;
List<String> firstname = new ArrayList<String>();
List<String> lastname= new ArrayList<String>();
int data_block =100;
List<contact> rowItems;
ListView mylistview;
public String path = Environment.getExternalStorageState()+"/contact";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File f = new File(path);
f.mkdirs();
mylistview=(ListView) findViewById(R.id.contactList);
ContactAdpater adapter =new ContactAdpater (this,getContact());
mylistview.setAdapter(adapter);
}
private ArrayList<contact> getContact(){
try {
File fi= new File(path+"/contact.txt");
Scanner s = null;
s = new Scanner(fi);
while(s. hasNextLine()){
System.out.println(s.nextLine());
String firstName = s.next();
String lastName = s.next();
String phone = s.next();
Toast.makeText(getBaseContext(),"Message2:"+firstName,Toast.LENGTH_SHORT).show();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ArrayList<contact> alarms= new ArrayList<contact>();
for (int i =0; i<firstname.size();i++){
alarms.add(new contact (firstname.get(i),lastname.get(i),phone.get(i)));
}
return alarms;
}
@Override
protected void onResume (){
//run this method on the finish of second activity and update the database
super.onResume();
mylistview=(ListView) findViewById(R.id.contactList);
ContactAdpater adapter =new ContactAdpater (this,getContact());
mylistview.setAdapter(adapter);
}
public void add(View v){
Intent getNameIntent = new Intent(MainActivity.this, AddContact.class);
startActivity(getNameIntent);
}
public void remove(View v){
Intent getNameIntent = new Intent(MainActivity.this, DeleteContact.class);
startActivity(getNameIntent);
}
}
的addContact
public class AddContact extends Activity {
EditText textFirstName,textLastName,textPhone;
public String path = Environment.getExternalStorageState()+"/contact";
String first,last,phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
textFirstName = (EditText) findViewById(R.id.editText);
textLastName = (EditText) findViewById(R.id.editText2);
textPhone = (EditText) findViewById(R.id.editText3);
}
public void save(View v){
first= textFirstName.getText().toString();
last= textLastName.getText().toString();
phone= textPhone.getText().toString();
try{
File fou= new File(path+"/contact.txt");
FileWriter fWriter = new FileWriter (fou);
PrintWriter pWriter = new PrintWriter (fWriter);
pWriter.println(first+" "+last+" "+phone);
pWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finish();
}
public void cancel (View v){
finish();
}
}
logcast
12-07 11:48:45.627 17718-17744/com.example.lab6 W/EGL_emulation: eglSurfaceAttrib not implemented
12-07 11:48:45.627 17718-17744/com.example.lab6 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabdbe840, error=EGL_SUCCESS
12-07 11:48:45.705 17718-17744/com.example.lab6 E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab7548b0
12-07 11:48:45.708 17718-17744/com.example.lab6 D/OpenGLRenderer: endAllStagingAnimators on 0xab66e180 (RippleDrawable) with handle 0xabdbf760
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: java.io.FileNotFoundException: removed/contact/contact.txt: open failed: ENOENT (No such file or directory)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at java.io.FileWriter.<init>(FileWriter.java:42)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at com.example.lab6.AddContact.save(AddContact.java:41)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at java.lang.reflect.Method.invoke(Native Method)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at android.view.View.performClick(View.java:5198)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at android.view.View$PerformClick.run(View.java:21147)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at android.os.Looper.loop(Looper.java:148)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at java.lang.reflect.Method.invoke(Native Method)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
12-07 11:48:59.957 17718-17718/com.example.lab6 W/System.err: at libcore.io.Posix.open(Native Method)
12-07 11:48:59.958 17718-17718/com.example.lab6 W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
12-07 11:48:59.958 17718-17718/com.example.lab6 W/System.err: at libcore.io.IoBridge.open(IoBridge.java:438)
12-07 11:48:59.958 17718-17718/com.example.lab6 W/System.err: ... 15 more
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err: java.io.FileNotFoundException: removed/contact/contact.txt: open failed: ENOENT (No such file or directory)
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:76)
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err: at java.util.Scanner.<init>(Scanner.java:158)
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err: at java.util.Scanner.<init>(Scanner.java:138)
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err: at com.example.lab6.MainActivity.getContact(MainActivity.java:52)
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err: at com.example.lab6.MainActivity.onResume(MainActivity.java:80)
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err: at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1258)
12-07 11:48:59.979 17718-17718/com.example.lab6 W/System.err: at android.app.Activity.performResume(Activity.java:6312)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3092)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err: at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3134)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1388)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err: at android.os.Looper.loop(Looper.java:148)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err: at java.lang.reflect.Method.invoke(Native Method)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err: at libcore.io.Posix.open(Native Method)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err: at libcore.io.IoBridge.open(IoBridge.java:438)
12-07 11:48:59.980 17718-17718/com.example.lab6 W/System.err: ... 16 more
12-07 11:49:00.009 17718-17744/com.example.lab6 W/EGL_emulation: eglSurfaceAttrib not implemented
12-07 11:49:00.009 17718-17744/com.example.lab6 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad720560, error=EGL_SUCCESS
12-07 11:49:00.098 17718-17744/com.example.lab6 E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab7582e0
12-07 11:49:00.101 17718-17744/com.example.lab6 D/OpenGLRenderer: endAllStagingAnimators on 0xaf7fb780 (RippleDrawable) with handle 0xad71a400
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lab6">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AddContact" />
<activity android:name=".DeleteContact"></activity>
</application>
</manifest>
答案 0 :(得分:0)
只需更改
public String path = Environment.getExternalStorageState()+"/contact";
到
public String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "/contact";
来自文档
android.os.Environment public static java.lang.String getExternalStorageState()返回主节点的当前状态 “外部”存储设备。返回:MEDIA_UNKNOWN之一, MEDIA_REMOVED,MEDIA_UNMOUNTED,MEDIA_CHECKING,MEDIA_NOFS, MEDIA_MOUNTED,MEDIA_MOUNTED_READ_ONLY,MEDIA_SHARED, MEDIA_BAD_REMOVAL或MEDIA_UNMOUNTABLE。
从
中可以看出java.io.FileNotFoundException: removed/contact/contact.txt
您的路径已被删除/ contact / contact.txt ,因为 Environment.getExternalStorageState()会被删除。
**编辑**
此外,您必须对getContact()和save()方法进行此更改
getContact:
private ArrayList<contact> getContact(){
try {
File fi= new File(path+"/contact.txt");
Scanner s = null;
s = new Scanner(fi);
while(s.hasNext()){
String firstName = s.next();
String lastName = s.next();
String phone = s.next();
Toast.makeText(getBaseContext(),"Message2:"+firstName,Toast.LENGTH_SHORT).show();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
ArrayList<contact> alarms= new ArrayList<contact>();
for (int i =0; i<firstname.size();i++){
alarms.add(new contact (firstname.get(i),lastname.get(i),phone.get(i)));
}
return alarms;
}
当你显示我删除了System.out.println(s.nextLine());
因为它正在吃掉所有的行和
String firstName = s.next();
String lastName = s.next();
String phone = s.next();
或抛出NoSuchElementException
。我也改变了
while(s.hasNextLine())
while(s.hasNext())
NoSuchElementException
因为 public void save(View v){
first= textFirstName.getText().toString();
last= textLastName.getText().toString();
phone= textPhone.getText().toString();
try{
File fou = new File(path + File.separator +"contact.txt");
if (!fou.exists()) {
fou.createNewFile();
}
FileWriter fWriter = new FileWriter (fou);
PrintWriter pWriter = new PrintWriter (fWriter);
pWriter.println(first+" "+last+" "+phone);
pWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finish();
}
投了 if (!fou.exists()) {
fou.createNewFile();
}
。
保存:
Break
这里我唯一的改变是
Option Explicit
Sub SumByMon()
Application.ScreenUpdating = False
Dim wk As Worksheet, wt As Worksheet
Dim Astr As String, Bstr As String
Dim i, j, FinalRow, FinalRowG As Long
Dim sm As Double, Jsum As Double, Fsum As Double, Msum As Double, Asum As Double, Masum As Double, Jusum As Double, Julsum As Double, Ausum As Double, Ssum As Double, Osum As Double, Nsum As Double, Dsum As Double
Dim Dt
Dim LMon As Integer
Set wk = Sheets("BR Mailing List_12-4-15 (3)")
Set wt = Sheets("Total By Month")
FinalRowG = wk.Range("N900000").End(xlUp).Row
FinalRow = wt.Range("A900000").End(xlUp).Row
For i = 2 To FinalRow
Jsum = 0
Fsum = 0
Msum = 0
Asum = 0
Masum = 0
Jusum = 0
Julsum = 0
Ausum = 0
Ssum = 0
Osum = 0
Nsum = 0
Dsum = 0
Astr = Trim(wt.Range("A" & i))
For j = 2 To FinalRowG
Bstr = Trim(wk.Range("N" & j))
If Astr = Bstr Then
Dt = wk.Range("T" & j).Value
LMon = Month(Dt)
Select Case LMon
Case 1
sm = wk.Range("S" & j).Value
Jsum = Jsum + sm
Case 2
sm = wk.Range("S" & j).Value
Fsum = Fsum + sm
Case 3
sm = wk.Range("S" & j).Value
Msum = Msum + sm
Case 4
sm = wk.Range("S" & j).Value
Asum = Asum + sm
Case 5
sm = wk.Range("S" & j).Value
Masum = Masum + sm
Case 6
sm = wk.Range("S" & j).Value
Jusum = Jusum + sm
Case 7
sm = wk.Range("S" & j).Value
Julsum = Julsum + sm
Case 8
sm = wk.Range("S" & j).Value
Ausum = Ausum + sm
Case 9
sm = wk.Range("S" & j).Value
Ssum = Ssum + sm
Case 10
sm = wk.Range("S" & j).Value
Osum = Osum + sm
Case 11
sm = wk.Range("S" & j).Value
Nsum = Nsum + sm
Case 12
sm = wk.Range("S" & j).Value
Dsum = Dsum + sm
Case Else
Debug.Print LMon
End Select
Else: End If
Next j
wt.Range("B" & i) = Jsum
wt.Range("C" & i) = Fsum
wt.Range("D" & i) = Msum
wt.Range("E" & i) = Asum
wt.Range("F" & i) = Masum
wt.Range("G" & i) = Jusum
wt.Range("H" & i) = Julsum
wt.Range("I" & i) = Ausum
wt.Range("J" & i) = Ssum
wt.Range("K" & i) = Osum
wt.Range("L" & i) = Nsum
wt.Range("M" & i) = Dsum
Next i
wt.Select
Range("A1").Select
Application.ScreenUpdating = True
End Sub
如果不存在则创建文件。
答案 1 :(得分:0)
我认为这是StackOverflow上一个非常类似的问题。请检查一下:How to create text file and insert data to that file on Android。
您错过了代码的重要部分,它会告诉Android系统保存文件的位置。您选择了错误的方法,因为removed
给出的状态Environment.getExternalStorageState()
仅表示Android无法找到您的文件,并且该方法永远不会为您提供文件路径