Les choses sont simples mais ne fonctionnent pas comme prévu.Android lire le fichier de ressources brutes de texte
J'ai un fichier texte ajouté en tant que ressource brute. Le fichier texte contient du texte comme:
b) SI LA LOI EXIGE TOUTE GARANTIE EN CE QUI CONCERNE LE LOGICIEL , TOUTES CES GARANTIES SONT LIMITÉE À UNE DURÉE DE VINGT-DIX (90) JOURS DE LA DATE DE LIVRAISON .
(c) NO informations orales ou écrites OU CONSEIL DONNE PAR ORIENTATION VIRTUELLE, ses distributeurs, DISTRIBUTEURS, AGENTS OU COLLABORATEURS NE CONSTITUE UNE GARANTIE OU AUGMENTER LA PORTEE DE LA GARANTIE CONTENUE DANS .
(d) (États-Unis uniquement) CERTAINS ÉTATS NE PERMETTENT PAS L'EXCLUSION DE GARANTIE IMPLICITE , L'EXCLUSION CI-DESSUS PEUT NE PAS VOUS. CETTE GARANTIE DONNE VOUS DROITS LÉGAUX SPÉCIFIQUES ET VOUS POUVEZ AUSSI AVOIR D'AUTRES DROITS LÉGAUX QUE VARIENT D'UN ÉTAT À L'AUTRE.
Sur mon écran, j'ai une mise en page comme ceci:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1.0"
android:layout_below="@+id/logoLayout"
android:background="@drawable/list_background">
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/txtRawResource"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dip"/>
</ScrollView>
</LinearLayout>
Le code pour lire la ressource brute est:
TextView txtRawResource= (TextView)findViewById(R.id.txtRawResource);
txtDisclaimer.setText(Utils.readRawTextFile(ctx, R.raw.rawtextsample);
public static String readRawTextFile(Context ctx, int resId)
{
InputStream inputStream = ctx.getResources().openRawResource(resId);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
return null;
}
return byteArrayOutputStream.toString();
}
Le texte get a montré, mais après chaque ligne je reçois un personnage étrange [] Comment puis-je supprimer ce personnage? Je pense que c'est New Line.
SOLUTION DE TRAVAIL
public static String readRawTextFile(Context ctx, int resId)
{
InputStream inputStream = ctx.getResources().openRawResource(resId);
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
StringBuilder text = new StringBuilder();
try {
while ((line = buffreader.readLine()) != null) {
text.append(line);
text.append('\n');
}
} catch (IOException e) {
return null;
}
return text.toString();
}
Je ne suis pas sûr que le Inputstream.available() est le bon choix ici, lisez plutôt à un n ByteArrayOutputStream jusqu'au n == -1. – ThomasRS
Cela peut ne pas fonctionner pour les ressources volumineuses. Cela dépend de la taille du tampon de lecture inputtream et ne peut renvoyer qu'une partie de la ressource. – d4n3
@ d4n3 a raison, la documentation de la méthode de flux d'entrée disponible indique: "Renvoie un nombre estimé d'octets pouvant être lues ou ignorés sans bloquer pour plus d'entrées Notez que cette méthode fournit une garantie si faible qu'elle n'est pas très utile dans la pratique " – ozba