【问题标题】:Not getting file through FileInputStream?没有通过 FileInputStream 获取文件?
【发布时间】:2015-01-08 10:00:20
【问题描述】:

我正在尝试检查文件位置,以免被覆盖。为此,我必须使用FileInputStream,因为它有一个可以与FileChannel 一起使用的方法position()。 BufferedReader 不保持位置。

我的代码是:

FileChannel fc = null;
FileInputStream fis = null;      
int i=0;
long pos;
char c;
fis = new FileInputStream("File.txt");
   while((i=fis.read())!=-1)
                         {
                            fc = fis.getChannel();
                            pos = fc.position();
                            c = (char)i;
                            System.out.print("No of bytes read: "+pos);
                            System.out.println("; Char read: "+c);
                         }

我收到java.io.FileNotFoundException:

/doneQuestionDetail.txt:打开失败:ENOENT(没有这样的文件或 目录)

这个错误意味着它没有从位置获取文件,因为文件不存在并且如果我使用BufferedReader:

BufferedReader inputReader = new BufferedReader(
                                    new InputStreamReader(openFileInput("File.txt")));

这行没有给出任何错误意味着文件存在并且FileInputStream 没有得到文件。

搜索后我先得到位置,然后把它给FileInputStream,然后我改变了代码:

String extr = Environment.getExternalStorageDirectory().toString();
                            File mFolder = new File(extr + "/imotax");
                            String s = "doneQuestionDetail.txt";
                            File f = new File(mFolder.getAbsolutePath(), s);
                             fis = new FileInputStream(f);

现在我收到一个错误:

java.io.FileNotFoundException: /mnt/sdcard/imotax/File.txt: 打开 失败:ENOENT(没有这样的文件或目录)

希望您的建议。谢谢。

【问题讨论】:

    标签: java android bufferedreader fileinputstream filechannel


    【解决方案1】:

    试试下面的代码。

    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + File.separator
                            + "/imotax");
    dir.mkdirs();
    File f= new File(dir, fileToCreate);
    if(!f.exists()){
    f.createNewFile();
    }
    
    fis = new FileInputStream(f);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      • 2011-02-19
      • 1970-01-01
      • 1970-01-01
      • 2015-09-25
      • 1970-01-01
      相关资源
      最近更新 更多