J'ai un fichier de ressources dans mon dossier/res/raw/(/res/raw/textfile.txt) que j'essaie de lire depuis mon application Android pour le traitement.Accéder aux fichiers de ressources sous Android
public static void main(String[] args) {
File file = new File("res/raw/textfile.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0) {
// Do something with file
Log.d("GAME", dis.readLine());
}
fis.close();
bis.close();
dis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
J'ai essayé une syntaxe différente de chemin, mais toujours une erreur java.io.FileNotFoundException. Comment puis-je accéder au fichier /res/raw/textfile.txt pour le traitement? Est Fichier fichier = nouveau fichier ("res/raw/textfile.txt"); la mauvaise méthode dans Android?
* Réponse: *
// Call the LoadText method and pass it the resourceId
LoadText(R.raw.textfile);
public void LoadText(int resourceId) {
// The InputStream opens the resourceId and sends it to the buffer
InputStream is = this.getResources().openRawResource(resourceId);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String readLine = null;
try {
// While the BufferedReader readLine is not null
while ((readLine = br.readLine()) != null) {
Log.d("TEXT", readLine);
}
// Close the InputStream and BufferedReader
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Considérez-nous ing [Commons d'Apache] (http://commons.apache.org/proper/commons-io/). Rechercher "IOUtils" là. – JJD