从我的应用程序创建的文本文件中读取时遇到问题。我的应用程序生成一个文本文件,其中包含每行中一个字段的数据,但即使有BufferedReader和StringBuilder的答案,我也无法在许多组合上处理好。加载按钮应该加载文件的名称(并执行它)并在指定的字段(TextView)中写入数据。为什么它不起作用?在开始时我认为这是StringBuilder的一个问题,因为他告诉我有点错误,但没有从他那里读取数据,所以我做了他不再产生这个错误但仍然没有。
onActivityResult:
@Override
public void onActivityResult(int reqCode, int resCode, Intent Data) {
super.onActivityResult(reqCode, resCode, Data);
if (reqCode == reqGaleria) {
if (resCode == RESULT_OK) {
try {
Uri imageUri = Data.getData();
InputStream imageStream = getContentResolver().openInputStream(imageUri);
Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
ramka.setImageBitmap(selectedImage);
sciezka = imageUri.getPath();
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Coś poszło nie tak", LENGTH_SHORT)
.show();
}
} else {
Toast.makeText(getApplicationContext(), "Nie wybrałeś obrazu", LENGTH_SHORT).show();
}
}
if (reqCode == reqLoad) {
if (resCode == RESULT_OK) {
try {
Uri plik = Data.getData();
@SuppressLint("Recycle") Cursor kursor = getContentResolver().query(plik, null, null, null, null);
int nazwa_i = kursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
kursor.moveToFirst();//Musi zostać - bez tego nie wyskoczy nazwa pliku
wczytaj.setText(kursor.getString(nazwa_i));
FileReader test = new FileReader(String.valueOf(wczytaj));
BufferedReader bufor_w = new BufferedReader(test);
String line;
line = bufor_w.readLine();
StringBuilder bob = new StringBuilder(line);
i_e_magazyn.setText(bob);
bob.append('\n');
i_e_strefa.setText(bob);
bufor_w.close();
kursor.close();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Problem z uruchamianiem pliku",
LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "Nie wybrałeś pliku", LENGTH_SHORT).show();
}
}
}
负责加载的按钮。
b2.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/*");
startActivityForResult(intent,reqLoad);
}
});
文件:
Magazyn
Strefa
Rząd
Rama
Słup
Uszkodzenie
Wielkość Uszkodzenia
Lokalizacja
Zielony
Dalsze Działania
Potrzebne Elementy
Typ Elementu
666
/external/images/media/68
最后一行是加载图像的路径
编辑: 问题不仅仅是读取文件而是读取我选择的文件。 当我指定必须加载的文件时,它会完美加载。当我决定向他展示必须加载数据的文件时,告诉我该文件不存在。
有效的代码:
testowy.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
File pliczek = Environment.getExternalStorageDirectory();
File plik = new File(pliczek,"/est.txt");
StringBuilder string;
string = new StringBuilder();
try{
BufferedReader reader = new BufferedReader(new FileReader(plik));
String linia;
int licz = 0;
while((linia = reader.readLine())!=null){
switch(licz) {
case 0:
string.append(linia);
i_e_magazyn.setText(linia);
string.append('\n');
break;
case 1:
string.append(linia);
i_e_strefa.setText(linia);
string.append('\n');
break;
case 2:
string.append(linia);
i_e_rzad.setText(linia);
string.append('\n');
break;
case 3:
string.append(linia);
i_e_rama.setText(linia);
string.append('\n');
break;
case 4:
string.append(linia);
i_e_slup.setText(linia);
string.append('\n');
break;
case 5:
string.append(linia);
i_e_uszk.setText(linia);
string.append('\n');
break;
case 6:
string.append(linia);
i_e_w_uszk.setText(linia);
string.append('\n');
break;
case 7:
string.append(linia);
i_e_lok.setText(linia);
string.append('\n');
break;
case 8:
string.append(linia);
i_e_klasa.setText(linia);
string.append('\n');
break;
case 9:
string.append(linia);
i_e_dal_dzial.setText(linia);
string.append('\n');
break;
case 10:
string.append(linia);
i_e_pot_elem.setText(linia);
string.append('\n');
break;
case 11:
string.append(linia);
i_e_typ.setText(linia);
string.append('\n');
break;
case 12:
string.append(linia);
i_e_ilosc.setText(linia);
string.append('\n');
break;
case 13:
string.append(linia);
wczytaj.setText(linia);
string.append('\n');
break;
}
licz++;
}
} catch(FileNotFoundException e){
Toast.makeText(getApplicationContext(),"Nie ma takiego pliku",LENGTH_SHORT)
.show();
} catch(IOException e){
Toast.makeText(getApplicationContext(),"Nie zczytano pliku",LENGTH_SHORT).show();
}
}
});
适合选择文件的代码:
if (reqCode == reqLoad) {
if (resCode == RESULT_OK) {
Uri wskaznik = data.getData();
assert wskaznik != null;
//FileReader test = new FileReader(Data.getData().toString());
edytuj.setText(String.valueOf(wskaznik));
File pliczek = Environment.getExternalStorageDirectory();
File plik = new File(String.valueOf(wskaznik));
StringBuilder string;
string = new StringBuilder();
try {
BufferedReader reader = new BufferedReader(new FileReader(plik));
String linia;
int licz = 0;
while ((linia = reader.readLine()) != null) {
switch (licz) {
case 0:
string.append(linia);
i_e_magazyn.setText(linia);
string.append('\n');
break;
case 1:
string.append(linia);
i_e_strefa.setText(linia);
string.append('\n');
break;
case 2:
string.append(linia);
i_e_rzad.setText(linia);
string.append('\n');
break;
case 3:
string.append(linia);
i_e_rama.setText(linia);
string.append('\n');
break;
case 4:
string.append(linia);
i_e_slup.setText(linia);
string.append('\n');
break;
case 5:
string.append(linia);
i_e_uszk.setText(linia);
string.append('\n');
break;
case 6:
string.append(linia);
i_e_w_uszk.setText(linia);
string.append('\n');
break;
case 7:
string.append(linia);
i_e_lok.setText(linia);
string.append('\n');
break;
case 8:
string.append(linia);
i_e_klasa.setText(linia);
string.append('\n');
break;
case 9:
string.append(linia);
i_e_dal_dzial.setText(linia);
string.append('\n');
break;
case 10:
string.append(linia);
i_e_pot_elem.setText(linia);
string.append('\n');
break;
case 11:
string.append(linia);
i_e_typ.setText(linia);
string.append('\n');
break;
case 12:
string.append(linia);
i_e_ilosc.setText(linia);
string.append('\n');
break;
case 13:
string.append(linia);
wczytaj.setText(linia);
string.append('\n');
break;
}
licz++;
}
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(), "Nie ma takiego pliku",
LENGTH_SHORT)
.show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Nie zczytano pliku", LENGTH_SHORT)
.show();
}
} else {
Toast.makeText(getApplicationContext(), "Nie wybrałeś pliku", LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
我认为你根本不想要StringBuilder
。我想你想读取文件的每一行,并根据你在文件中的哪一行将每一行分配到不同的字段。如果是这种情况,您可以在文件中读取的一种方式是:
FileReader test = new FileReader(Data.getData().toString());
BufferedReader bufor_w = new BufferedReader(test);
String line;
int counter = 0;
while((line = bufor_w.readLine()) != null){
switch(counter){
case 0:
i_e_magazyn.setText(line);
break;
case 1:
i_e_strefa.setText(line);
break;
case 2:
//etc.
break;
}
counter++;
}
bufor_w.close();
test.close();
这将读取文件的每一行,并根据您所在的行,您可以将值分配给您喜欢的任何字段。