大家下午好, 我在同一目录中有10个文件,但有些文件正在被读取。 我检查了名字,写得很好。
活动从其他活动中获取int“位置”。它就像一个用于加载文件的标识符,其名称在文件“Categorias”中,我把文件的名称放在一个字符串中,然后用“position”读取它,每行的txt放入列表视图的位置。问题是只能读取其中一些文件。我看不出问题了。
for的第一个循环读取fichero =“categorias.txt”。第二个循环读取向量[位置]
谢谢
Java
protected String fichero="categorias.txt";
protected String Nombre;
protected int position;
protected int contarLineasFichero() {
int lineas = 0;
try {
InputStreamReader is = new InputStreamReader(openFileInput(fichero));
BufferedReader fich = new BufferedReader(is);
String s;
while ((s = fich.readLine()) != null)
lineas++;
is.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return lineas;
}
protected void leerFichero() {
int linea = 0;
String s;
try {
BufferedReader fin =
new BufferedReader(new InputStreamReader(openFileInput(fichero)));
while ((s = fin.readLine()) != null) {
vector[linea] = s;
linea++;
}
fin.close();
} catch (Exception ex) {
Log.e("Ficheros", "Error al leer fichero desde memoria interna");
}
}
protected void crearFichero(String auxiliar[]) {
String s;
int i;
try {
OutputStreamWriter fout = new OutputStreamWriter(
openFileOutput("fichero1.txt", Context.MODE_PRIVATE));
/*String [] auxi;
auxi= new String[3];
auxi[0]="Piña";
auxi[1]="Pera";
auxi[2]="Fresa";
*/
for (i = 0; i < auxiliar.length; i++) {
if (auxiliar[i] != null) {
s = auxiliar[i];
fout.write(s);
fout.write("\n");
}
}
// fout.write("Es problema del for");
fout.close();
} catch (Exception ex) {
Log.e("Ficheros", "Error al escribir fichero a memoria interna");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selecciona);
Bundle extras=getIntent().getExtras();
Nombre=extras.getString("nombre");
position= Integer.parseInt(extras.getString("categoria"));
lvAlimentos = (ListView) findViewById(R.id.lvAlimentos);
btCarrito = (Button) findViewById(R.id.btCarrito);
btContinua = (Button) findViewById(R.id.btContinua);
contador = 0;
for(int i=0;i<2;i++) {
int total = contarLineasFichero(); //it counts the lines of a file
aux = new String[total];
if (total > 0) {
//vector = new String[0];
vector = new String[total];
leerFichero(); //it reads the file
} else {
vector = new String[]{"no va"};
}
if(i==0)
fichero = vector[position];
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, vector);
lvAlimentos.setAdapter(adapter);
日志:
LOG: 03-13 14:27:37.990 9031-9031/com.example.joan.mypoma W/System.err﹕ java.io.FileNotFoundException: /data/data/com.example.joan.mypoma/files/alcohol.txt : open failed: ENOENT (No such file or directory)
03-13 14:27:38.000 9031-9031/com.example.joan.mypoma W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:409)
03-13 14:27:38.000 9031-9031/com.example.joan.mypoma W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:78)
03-13 14:27:38.030 9031-9031/com.example.joan.mypoma W/System.err﹕ at android.app.ContextImpl.openFileInput(ContextImpl.java:780)
03-13 14:27:38.030 9031-9031/com.example.joan.mypoma W/System.err﹕ at android.content.ContextWrapper.openFileInput(ContextWrapper.java:179)
03-13 14:27:38.040 9031-9031/com.example.joan.mypoma W/System.err﹕ at com.example.joan.mypoma.Selecciona.contarLineasFichero(Selecciona.java:38)
03-13 14:27:38.040 9031-9031/com.example.joan.mypoma W/System.err﹕ at com.example.joan.mypoma.Selecciona.onCreate(Selecciona.java:124)
03-13 14:27:38.040 9031-9031/com.example.joan.mypoma W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5231)
03-13 14:27:38.040 9031-9031/com.example.joan.mypoma W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-13 14:27:38.050 9031-9031/com.example.joan.mypoma W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-13 14:27:38.050 9031-9031/com.example.joan.mypoma W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-13 14:27:38.050 9031-9031/com.example.joan.mypoma W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-13 14:27:38.070 9031-9031/com.example.joan.mypoma W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-13 14:27:38.070 9031-9031/com.example.joan.mypoma W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
03-13 14:27:38.070 9031-9031/com.example.joan.mypoma W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
03-13 14:27:38.070 9031-9031/com.example.joan.mypoma W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017)
03-13 14:27:38.090 9031-9031/com.example.joan.mypoma W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
03-13 14:27:38.090 9031-9031/com.example.joan.mypoma W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
03-13 14:27:38.110 9031-9031/com.example.joan.mypoma W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-13 14:27:38.110 9031-9031/com.example.joan.mypoma W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-13 14:27:38.130 9031-9031/com.example.joan.mypoma W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
03-13 14:27:38.140 9031-9031/com.example.joan.mypoma W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
03-13 14:27:38.180 9031-9031/com.example.joan.mypoma W/System.err﹕ at libcore.io.Posix.open(Native Method)
03-13 14:27:38.180 9031-9031/com.example.joan.mypoma W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
03-13 14:27:38.190 9031-9031/com.example.joan.mypoma W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:393)
03-13 14:27:38.190 9031-9031/com.example.joan.mypoma W/System.err﹕ ... 19 more
03-13 14:27:38.240 9031-9031/com.example.joan.mypoma I/Choreographer﹕ Skipped 52 frames! The application may be doing too much work on its main thread.
03-13 13:51:46.746 2048-2048/com.example.joan.mypoma W/System.err﹕ java.io.FileNotFoundException: /data/data/com.example.joan.mypoma/files/alinos.txt : open failed: ENOENT (No such file or directory)