I am trying to check file position so that it would not be overwritten. For this purpose I have to use FileInputStream
because it has a method position()
that can be use with FileChannel
. BufferedReader
does not maintain position.
My code is:
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);
}
I am getting java.io.FileNotFoundException
:
/doneQuestionDetail.txt: open failed: ENOENT (No such file or directory)
This error means it is not getting file from location because file doesn't exsist there and if I use BufferedReader
:
BufferedReader inputReader = new BufferedReader(
new InputStreamReader(openFileInput("File.txt")));
It does not give any error in this line meaning file exists and FileInputStream
is not getting file.
After searching I got to get location first and then give it to FileInputStream
, then I changed code like:
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);
Now I am getting an error:
java.io.FileNotFoundException: /mnt/sdcard/imotax/File.txt: open failed: ENOENT (No such file or directory)
Hope for your suggestions. Thanks.
Try following code.
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);