Benim/res/raw/klasöründe (/res/raw/textfile.txt) bir kaynak dosyam var, bu işlem için android uygulamamdan okumaya çalışıyorum.Android'de kaynak dosyalarına eriş
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();
}
}
Ben farklı bir yol sözdizimi çalıştı ama her zaman java.io.FileNotFoundException hatası alıyorum var. İşlem için /res/raw/textfile.txt dosyasına nasıl erişebilirim? Dosya dosyası = new File ("res/raw/textfile.txt"); Android'de yanlış yöntem mi?
* Cevap:
// 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();
}
}
bizi düşünün
new BufferedInputStream(getResources().openRawResource(...));
ardından akışı olarak dosyanın içeriği (o uzantısı olmadan dosyanın adı genellikle var) [Apache Commons] (http://commons.apache.org/proper/commons-io/). Orada 'IOUtils' ara. – JJD