问题描述:

This is my code:

File root = Environment.getExternalStorageDirectory();

File dir = new File (root.getAbsolutePath() + "/Bonbon info");

dir.mkdirs();

File f = new File(dir, "paket.txt");

StringBuilder text = new StringBuilder();

try {

BufferedReader br = new BufferedReader(new FileReader(f));

String line;

br.skip(60);

int charactersRead = 0;

while ((line = br.readLine()) != null && charactersRead < 12) {

text.append(line);

text.append('\n');

charactersRead++;

}

}

catch (IOException e) {

}

final String URL = text.toString();

TextView tv = (TextView)findViewById(R.id.textView2);

tv.setText(text);

Reading is working, but i can't read only 12 characters, it reads trough the end of file, don't know why.

网友答案:

I'm guessing your file is relatively short.

You're calling BufferedReader.readLine(), which in an attempt to be efficient is sucking up a big chunk of the file stream rather than going through it character-by-character.

If you want that finer control over what you read, it's probably worth using an InputStream implementation straight up.

相关阅读:
Top