1

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.

user207421
312k45 gold badges324 silver badges493 bronze badges
asked Jan 8, 2015 at 10:00
0

1 Answer 1

1

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);
answered Jan 8, 2015 at 11:01
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.