2011-12-19 1 views
1

Comment lire un fichier dans la vue texte avec l'affichage approprié comme dans un fichier texte? Je peux lire le fichier en textview. Mais afficher comme dans le fichier texte. Mon dossier Texte original:Comment lire un fichier dans une vue texte avec un affichage correct comme dans un fichier texte?

Testing this 1 

Satheeshisdf sdf asdfsdfasd fasdfsdfsd 
i can able to do it. 

Trying it       thats all.` 

Mais l'affichage comme ceci:

i can able to do it. 

Trying it       thats all.` 

Mise en page:

<ScrollView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:fillViewport="true" 
android:layout_weight="1.0"> 
<TextView 
    android:id="@+id/subtitletv" 
    android:textSize="18dp" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
/> 

Programme:

subtitletv = (TextView)findViewById("R.id.t"); 
try { 
     FileReader fr=new FileReader(selectedfile); 
     BufferedReader br=new BufferedReader(fr); 
     String line = null; 
     try { 
      while(br.readLine()!=null) 
      { 
       line =br.readLine(); 
       subtitletv.append(line); 
       subtitletv.append("\n"); 
      } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

Update1: Mais maintenant je ne peux pas lire le gros fichier (50KB). Petit fichier de chargement bien.

Répondre

4

Vous n'annexant toutes les autres lignes parce que vous lisez deux lignes à la fois ...

while(br.readLine()!=null) // THIS READS FIRST LINE THEN THIRD LINE 
{ 
    line = br.readLine(); // THIS READS SECOND LINE THEN FOURTH AND SO ON 
    subtitletv.append(line); 
    subtitletv.append("\n"); 
} 

utilisation ...

while((line = br.readLine()) != null) 
{ 
    subtitletv.append(line); 
    subtitletv.append("\n"); 
} 
2

Vous lisez une ligne de br deux fois par itération de boucle. Seulement lire une fois. En outre, assurez-vous de fermer le FileReader dans un bloc finally. Par exemple:

subtitletv = (TextView)findViewById("R.id.t"); 
FileReader fr = null; 
try { 
    fr = new FileReader(selectedfile); 
    BufferedReader br = new BufferedReader(fr); 
    String line = br.readLine(); 
    while (null != line) { 
     subtitletv.append(line); 
     subtitletv.append("\n"); 
     line = br.readLine(); 
    } 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} finally { 
    if (null != fr) { 
     try { 
      fr.close(); 
     } catch (IOException e) { 
      // ignore 
     } 
    } 
} 
+0

homme Merci. Il travaille.Mais maintenant je ne peux pas lire grand fichier (30 Ko) .Comment faire. –

+0

Quelle est l'erreur? –

+0

L'activité ne répond pas. avec deux boutons attendez et ok –

Questions connexes