Java FileNotFoundException打开失败:eNot(没有这样的文件或目录)
我有一个名为“and.doc”的文件,它有大量的记录,每个记录都有这种形状 表达:防御 所以我想Java FileNotFoundException打开失败:eNot(没有这样的文件或目录),java,android,Java,Android,我有一个名为“and.doc”的文件,它有大量的记录,每个记录都有这种形状 表达:防御 所以我想 用扫描仪读取文件 使用作为分隔符 找到从定义中拆分表达式的方法 并以某种方式将它们添加到我的Sqlite数据库中(如果有人知道如何做到这一点,那就太好了) 我正在使用这个代码 try { mf =new File("/home/agh/AndroidStudioProjects/Dicod/app/src/main/res/raw/and.doc"); input
- 用扫描仪读取文件
- 使用
代码>作为分隔符
- 找到从定义中拆分表达式的方法
- 并以某种方式将它们添加到我的Sqlite数据库中(如果有人知道如何做到这一点,那就太好了)
try {
mf =new File("/home/agh/AndroidStudioProjects/Dicod/app/src/main/res/raw/and.doc");
inputFile = new Scanner(mf);
inputFile.useDelimiter(";");
while (inputFile.hasNext())
{
String x = inputFile.next();
Toast.makeText(getApplicationContext(),x,Toast.LENGTH_LONG).show();
//Splitting and adding to the databse
}
}
catch(FileNotFoundException e) {
e.printStackTrace();
}`
但我总是犯这个错误
06-23 04:38:28.771 23620-23620/? W/System.err: java.io.FileNotFoundException: /home/agh/AndroidStudioProjects/Dicod/app/src/main/res/raw/and.txt: open failed: ENOENT (No such file or directory)
06-23 04:38:28.771 23620-23620/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:465)
06-23 04:38:28.771 23620-23620/? W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:76)
06-23 04:38:28.771 23620-23620/? W/System.err: at java.util.Scanner.<init>(Scanner.java:158)
06-23 04:38:28.772 23620-23620/? W/System.err: at java.util.Scanner.<init>(Scanner.java:138)
06-23 04:38:28.772 23620-23620/? W/System.err: at com.example.agh.dicod.MainActivity.onCreate(MainActivity.java:28)
06-23 04:38:28.772 23620-23620/? W/System.err: at android.app.Activity.performCreate(Activity.java:5990)
06-23 04:38:28.772 23620-23620/? W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
06-23 04:38:28.772 23620-23620/? W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
06-23 04:38:28.772 23620-23620/? W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
06-23 04:38:28.772 23620-23620/? W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:156)
06-23 04:38:28.772 23620-23620/? W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
06-23 04:38:28.772 23620-23620/? W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
06-23 04:38:28.772 23620-23620/? W/System.err: at android.os.Looper.loop(Looper.java:211)
06-23 04:38:28.772 23620-23620/? W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5389)
06-23 04:38:28.772 23620-23620/? W/System.err: at java.lang.reflect.Method.invoke(Native Method)
06-23 04:38:28.772 23620-23620/? W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
06-23 04:38:28.772 23620-23620/? W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
06-23 04:38:28.772 23620-23620/? W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
06-23 04:38:28.772 23620-23620/? W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
06-23 04:38:28.773 23620-23620/? W/System.err: at libcore.io.Posix.open(Native Method)
06-23 04:38:28.773 23620-23620/? W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
06-23 04:38:28.773 23620-23620/? W/System.err: at libcore.io.IoBridge.open(IoBridge.java:451)
06-23 04:38:28.773 23620-23620/? W/System.err: ... 17 more
06-2304:38:28.77123620-23620/?W/System.err:java.io.FileNotFoundException:/home/agh/AndroidStudioProjects/Dicod/app/src/main/res/raw/and.txt:open failed:enoint(没有这样的文件或目录)
06-23 04:38:28.771 23620-23620/? W/System.err:at libcore.io.IoBridge.open(IoBridge.java:465)
06-23 04:38:28.771 23620-23620/? W/System.err:at java.io.FileInputStream。(FileInputStream.java:76)
06-23 04:38:28.771 23620-23620/? W/System.err:at java.util.Scanner.(Scanner.java:158)
06-23 04:38:28.772 23620-23620/? W/System.err:at java.util.Scanner.(Scanner.java:138)
06-23 04:38:28.772 23620-23620/? W/System.err:at com.example.agh.dicod.MainActivity.onCreate(MainActivity.java:28)
06-23 04:38:28.772 23620-23620/? W/System.err:at-android.app.Activity.performCreate(Activity.java:5990)
06-23 04:38:28.772 23620-23620/? W/System.err:at-android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
06-23 04:38:28.772 23620-23620/? W/System.err:at-android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
06-23 04:38:28.772 23620-23620/? W/System.err:at-android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
06-23 04:38:28.772 23620-23620/? W/System.err:android.app.ActivityThread.access$800(ActivityThread.java:156)
06-23 04:38:28.772 23620-23620/? W/System.err:at-android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
06-23 04:38:28.772 23620-23620/? W/System.err:at-android.os.Handler.dispatchMessage(Handler.java:102)
06-23 04:38:28.772 23620-23620/? W/System.err:at-android.os.Looper.loop(Looper.java:211)
06-23 04:38:28.772 23620-23620/? W/System.err:at-android.app.ActivityThread.main(ActivityThread.java:5389)
06-23 04:38:28.772 23620-23620/? W/System.err:at java.lang.reflect.Method.invoke(本机方法)
06-23 04:38:28.772 23620-23620/? W/System.err:at java.lang.reflect.Method.invoke(Method.java:372)
06-23 04:38:28.772 23620-23620/? W/System.err:com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
06-23 04:38:28.772 23620-23620/? W/System.err:com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
06-23 04:38:28.772 23620-23620/? W/System.err:原因:android.System.ErrnoException:打开失败:enoint(没有这样的文件或目录)
06-23 04:38:28.773 23620-23620/? W/System.err:at libcore.io.Posix.open(本机方法)
06-23 04:38:28.773 23620-23620/? W/System.err:at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
06-23 04:38:28.773 23620-23620/? W/System.err:at libcore.io.IoBridge.open(IoBridge.java:451)
06-23 04:38:28.773 23620-23620/? W/System.err:。。。还有17个
可能文件不在所选目录中,或者未授予读取外部存储权限
请注意,如果以API 23为目标,则必须在运行时请求权限,而不仅仅是在清单中
该文件不存在。使用
file()
类创建文件并将流写入其中。然后从中读取数据。尝试将权限添加到Manifest.xml,然后重试
<manifest ...>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
/>
...
</manifest>
...
@Abdul2511我不确定我是否理解写流部分
我认为他的意思是,尽管下面的代码将读取目录中的文本文件内容
作为参数传递的文件myfile应该是提取的目录
// getdata() is the method which reads the data
// the data that is saved in byte format in the file
private String getdata(File myfile) {
FileInputStream fileInputStream = null;
try {
Log.i("file_read_send_to_getData_function",myfile.toString());
fileInputStream = new FileInputStream(myfile);
int i = -1;
StringBuffer buffer = new StringBuffer();
while ((i = fileInputStream.read()) != -1) {
buffer.append((char) i);
}
return buffer.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
我想你要通过的目录
"/home/agh/AndroidStudioProjects/Dicod/app/src/main/res/raw/and.doc"
存在于您的计算机上,但该文件需要来自您的android设备或android emulator的目录,这就是它引发异常的原因
如果您的“and.doc”存在于android设备的共享外部存储“/storage/emulated/0/Documents/”中,那么只要您正确提取目录,我认为该文件将能够访问它
以下是如何获取sd卡中的文档文件夹目录的示例
File externalStorageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS); //getting external storage directory
//it's another method to get the directory of a file inside the external storage of a device
File folder = new File(externalStorageDirectory + "/Password");
替换为“/Password”,将其替换为您的文件夹名@Abdul2511
,并以某种方式将其添加到我的Sqlite数据库中
下面是一个示例代码,您可以使用它作为参考,了解如何在android设备中保存和检索sqlLITE数据库中的数据
我们的SQLite数据库的根类是SQLiteOpenHelper
扩展SQLiteOpenHelper将把SQLite数据库的根类导入到我们的数据库类中
//root class of our SQLite database is SQLiteOpenHelper
//extends SQLiteOpenHelper will import that root class of SQLite databse into our NoteDatabase class
public class NoteDatabase extends SQLiteOpenHelper {
private static final int DATABASEVERSION= 2;
private static final String DATABASE_NAME = "your database name";
private static final String DATABASE_TABLE = "your table name";
//column names for database
private static final String KEY_ID="id";
private static final String KEY_TITLE="expression";
private static final String KEY_CONTENT="defenition";
private static final String KEY_DATE="date";
private static final String KEY_TIME="time";
public static List<Note> allNotes;
//creating a constructor for our noteDatabase
NoteDatabase(Context context){
super(context,DATABASE_NAME,null,DATABASEVERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//onCreate is called everytime the noteDatabase instance is created inside any class of this application
//creating the database table
String query = "CREATE TABLE " + DATABASE_TABLE +" ("+ KEY_ID +" INTEGER PRIMARY KEY AUTOINCREMENT , "+
KEY_TITLE+" TEXT, "+
KEY_CONTENT+" TEXT, "+
KEY_DATE+" TEXT, "+
KEY_TIME+" TEXT "+")";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//checking for updates for our database
if(oldVersion>=newVersion){
return;
}
else{
//update the table if new version is available
db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
onCreate(db);
}
}
//it will take Note as a parameter this note will contain all the information like title, content , date and time
//and it will send that note from addnote activity to this node database
//Note note is the class that we just created
public long addnote(Note note){
//here we will insert the data
SQLiteDatabase db = this.getWritableDatabase();
//this contentValues will create a dictionary like structure
ContentValues contentValues = new ContentValues();
//now we can save our value to the keys that we have created in this class
// contentValues.put(KEY_ID,note.getID()); ***Do not insert Id inside SQLite instead auto increament the id to be unique
contentValues.put(KEY_TITLE,note.getTitle());
contentValues.put(KEY_CONTENT,note.getContent());
contentValues.put(KEY_TIME,note.getTime());
contentValues.put(KEY_DATE,note.getDate());
//now we will insert the data
//if the data is inserted successfully it will return the long value of the primary key
long ID = db.insert(DATABASE_TABLE,null,contentValues);
db.close();
Log.i("ID", Long.toString(ID));
return ID;
}
//this will get all the notes present in the database
//so that we can desplay it in our ListView
public List<Note> getNotes(){
//we will pull the data from the database using element's unique id
//select * from databse where id = whatever the id we have passed on here
//* means acessing all the data in that particular id elements
//creating an instance of our database
SQLiteDatabase db = this.getReadableDatabase();
//creating a list of generic type called Note
allNotes = new ArrayList<>();
String query = "SELECT * FROM "+DATABASE_TABLE;
Cursor cursor = db.rawQuery(query,null);
if(cursor.moveToFirst()){
//i am going to pull all the data from the database and pass that data onto our listView
do{
//now creating a new note and save the data from the database by using cursor
Note note = new Note();
note.setID(cursor.getLong(0));
note.setTitle(cursor.getString(1));
note.setContent(cursor.getString(2));
note.setDate(cursor.getString(3));
note.setTime(cursor.getString(4));
//adding this to lisView
allNotes.add(note);
}while(cursor.moveToNext());
}
return allNotes;
}
//updating the database
//replacing the old data with the updated data set inside the database
public int editNote(Note note){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_TITLE,note.getTitle());
contentValues.put(KEY_CONTENT,note.getContent());
contentValues.put(KEY_DATE,note.getDate());
contentValues.put(KEY_TIME,note.getTime());
return db.update(DATABASE_TABLE, contentValues,KEY_ID+"=?",new String[]{String.valueOf(note.getID())});
}
//the method will handel the deletion of the notes
//this method will be called from the noteDetails.class
void deleteNote(long id){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(DATABASE_TABLE,KEY_ID+"=?",new String[]{String.valueOf(id)});
db.close();
}
}
//我们的SQLite数据库的根类是SQLiteOpenHelper
//扩展SQLiteOpenHelper将把SQLite数据库的根类导入我们的NoteDatabase类
公共类NoteDatabase扩展了SQLiteOpenHelper{
私有静态最终int数据库版本=2;
私有静态最终字符串数据库\u NAME=“您的数据库名称”;
私有静态最终字符串数据库\u TABLE=“您的表名”;
//数据库的列名
私有静态最终字符串密钥\u ID=“ID”;
私有静态最终字符串键\u TITLE=“expression”;
私有静态最终字符串键\u CONTENT=“defentition”;
私有静态最终字符串键\u DATE=“DATE”;
私有静态最终字符串键\u TIME=“TIME”;
公共静态列表所有注释;
//为我们的Notes数据库创建构造函数
NoteDatabase(上下文){
super(上下文、数据库名称、null、数据库版本);
}
@凌驾
public void onCreate(SQLiteDatabase db){
//每次在该应用程序的任何类中创建noteDatabase实例时,都会调用onCreate
//创建dat