FileNotFoundException open failed: EISDIR (Is a directory)?

打印出来的log

04-18 14:59:09.566 14795-16215/com.yg.ggcar E/json_parts: Ford_Mustang_yuanche_hougai_fu.png
04-18 14:59:09.616 14795-16215/com.yg.ggcar I/System.out: Thread-1397 calls detatch()
04-18 14:59:09.616 14795-16215/com.yg.ggcar I/成功写入本地: /storage/emulated/0/ggcar/car/Mustang/Ford_Mustang_yuanche_hougai_fu.json
04-18 14:59:09.706 14795-16215/com.yg.ggcar I/System.out: Thread-1397 calls detatch()
04-18 14:59:09.716 14795-16215/com.yg.ggcar W/System.err: java.io.FileNotFoundException: /storage/emulated/0/ggcar/car/Mustang: open failed: EISDIR (Is a directory)
04-18 14:59:09.716 14795-16215/com.yg.ggcar W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:409)
04-18 14:59:09.716 14795-16215/com.yg.ggcar W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
04-18 14:59:09.716 14795-16215/com.yg.ggcar W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
04-18 14:59:09.716 14795-16215/com.yg.ggcar W/System.err:     at com.zhy.utils.Read_Write.write(Read_Write.java:33)
04-18 14:59:09.716 14795-16215/com.yg.ggcar W/System.err:     at com.yg.activity.Choose_Car$4.run(Choose_Car.java:512)
04-18 14:59:09.716 14795-16215/com.yg.ggcar W/System.err:     at java.lang.Thread.run(Thread.java:841)
04-18 14:59:09.716 14795-16215/com.yg.ggcar W/System.err: Caused by: libcore.io.ErrnoException: open failed: EISDIR (Is a directory)
04-18 14:59:09.716 14795-16215/com.yg.ggcar W/System.err:     at libcore.io.Posix.open(Native Method)
04-18 14:59:09.716 14795-16215/com.yg.ggcar W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
04-18 14:59:09.716 14795-16215/com.yg.ggcar W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:393)
04-18 14:59:09.716 14795-16215/com.yg.ggcar W/System.err:     ... 5 more
04-18 14:59:09.716 14795-16215/com.yg.ggcar I/Read_Write_write: 创建文件失败!

这个是主界面调用Read_Write方法的地方

 Read_Write.write(ServerAPI.Location_Path_Car + carname + "/", json_parts.get(i).getoptions()[opt].getJSONFile(), result1 + "");

Read_Write.class

public class Read_Write {

    public static void write(String dir, String filename, String content) {
        try {
            // 如果手机插入SD卡,而且应用程序具有访问SD的权限
            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                // 获取SD卡目录
                // File sdCardDir = Environment.getExternalStorageDirectory();
                // File targetFile = new File(sdCardDir.getCanonicalPath() +
                // "/GGCar/"+ filename);
                File targetdir = new File(dir);
                if (!targetdir.exists()) {
                    targetdir.mkdir();
                    Log.i("mkdir", "成功创建:" + targetdir);
                }
                File targetFile = new File(dir + filename);


                FileOutputStream fos = new FileOutputStream(targetFile);
                if (content!=null){
                    fos.write(content.getBytes());
                    fos.close();
                }

                // 以指定文件创建RandomAccessFile对象
//                RandomAccessFile raf = new RandomAccessFile(targetFile, "rw");
//                Log.e("targetFile", String.valueOf(targetFile));
//                // 将文件记录指针移动到最后
//                raf.seek(targetFile.length());
//                // 写入文件内容
//                raf.write(content.getBytes());
//                Log.e("content", content);
//                // 关闭
//                raf.close();
                // Toast.makeText(SecondMainActivity.this, "数据已保存到" +
                // targetFile,Toast.LENGTH_LONG).show();
                // System.out.println("targetFile = " + targetFile);
                Log.i("成功写入本地", targetFile + "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("Read_Write_write", "创建文件失败!");
        }
    }

    // 讀取文件
    public static String read(String FILE_NAME) {
        try {
            // 如果手機插入了SD卡,而且應用程序具有访问SD的权限
            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {

                // 获取SD卡对应的存储目录
                // File sdCardDir = Environment.getExternalStorageDirectory();
                // 获取指定文件对应的输入流
                // FileInputStream fis = new
                // FileInputStream(sdCardDir.getCanonicalPath() + "/GGCar/" +
                // FILE_NAME);
                FileInputStream fis = new FileInputStream(FILE_NAME);
                // 将指定输入流包装成BufferedReader
                BufferedReader br = new BufferedReader(new InputStreamReader(
                        fis));
                StringBuilder sb = new StringBuilder("");
                String line = null;
                // 循环读取文件内容
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                // 关闭资源
                br.close();
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static void deletefile(String filedir) {
        File file = new File(filedir);
        if (file.exists()) {
            file.delete();
            Log.i("delete", filedir + "删除成功");
        }
    }

    public static void deleteDirectory(File file) {
        if (file.isFile()) {
            file.delete();
            return;
        }
        if (file.isDirectory()) {
            File[] childFiles = file.listFiles();
            if (childFiles == null || childFiles.length == 0) {
                file.delete();
                return;
            }
            for (int i = 0; i < childFiles.length; i++) {
                deleteDirectory(childFiles[i]);
            }
            file.delete();
        }
    }

    public static LinkedList<String> getFileName(File[] files) {
        LinkedList<String> name = new LinkedList<String>();
        if (files != null) {// 先判断目录是否为空,否则会报空指针
            for (File file : files) {
                if (file.isDirectory()) {
                    getFileName(file.listFiles());
                    Log.i("zeng", "若是文件目录。继续读2" + file.getName().toString()
                            + file.getPath().toString());
                } else {
                    String fileName = file.getName();
                    if (fileName.endsWith(".do")) {
                        HashMap map = new HashMap();
                        String s = fileName.substring(0,
                                fileName.lastIndexOf(".")).toString();
                        Log.i("zeng", "文件名do:" + s);
                        name.add(s);
                    }
                }
            }
            return name;
        } else {
            // 创建文件夹
        }
        return null;
    }

    public static LinkedList<String> getDirectoryName(File[] files) {
        LinkedList<String> name = new LinkedList<String>();
        if (files != null) {// 先判断目录是否为空,否则会报空指针
            for (File file : files) {
                if (file.isDirectory()) {
                    name.add(file.getName().toString());
                    Log.i("zeng", "若是文件目录。继续读2" + file.getName().toString()
                            + file.getPath().toString());
                }
            }
            return name;
        }
        return null;
    }

    public static boolean fileIsExists(String FILE_NAME) {
        try {
            // 获取SD卡对应的存储目录
            File sdCardDir = Environment.getExternalStorageDirectory();
            File f = new File(sdCardDir.getCanonicalPath() + "/GGCar/"
                    + FILE_NAME);
            if (!f.exists()) {
                return false;
            }

        } catch (Exception e) {
            // TODO: handle exception
            return false;
        }
        return true;
    }
}
阅读 10.2k
1 个回答

文件加扩展名了吗

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏