Lire un fichier caractère par caractère
Fermé
master
-
baladur13 Messages postés 47321 Date d'inscription Statut Modérateur Dernière intervention -
baladur13 Messages postés 47321 Date d'inscription Statut Modérateur Dernière intervention -
Bonjour,
je essai de lire un fichier ligne par ligne puis je veux lire chaque line caractere par caractere
je fait ce code mais il m affiche une erreur
il m affiche l exeption suivante
5 1 2 6 1 3 1 1 1 2 2 2 6 4 6 3 6 5 2 6 1 1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 121, Size: 46
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at javaapplication2.heuristique1.affiche(heuristique1.java:90)
at javaapplication2.heuristique1.main(heuristique1.java:119)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
je essai de lire un fichier ligne par ligne puis je veux lire chaque line caractere par caractere
je fait ce code mais il m affiche une erreur
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author marwa
*/
public class heuristique1 {
/*public static ArrayList<String> lire_doc(String lex){
ArrayList<String> wordList =new ArrayList<String>();
try{
InputStream ips = new FileInputStream (lex);
InputStreamReader ipsr = new InputStreamReader (ips);
BufferedReader br = new BufferedReader(ipsr);
StreamTokenizer st = new StreamTokenizer(br);
int token;
while((token=st.nextToken())!=StreamTokenizer.TT_EOF){
if(token==StreamTokenizer.TT_WORD){
wordList.add(st.sval);
}
}
}catch(Exception e){
System.out.println(e.getMessage());
}
return wordList;
}*/
public static void affiche(String fichier) throws IOException {
String filePath = "C:\\"+fichier+".txt";
// ArrayList <Char> m1 = new ArrayList <Char>();
ArrayList<Integer> m2= new ArrayList<Integer>();
ArrayList<Integer>indice = new ArrayList<Integer>();
try{
// Création du flux bufférisé sur un FileReader, immédiatement suivi par un
// try/finally, ce qui permet de ne fermer le flux QUE s'il le reader
// est correctement instancié (évite les NullPointerException)
BufferedReader buff = new BufferedReader(new FileReader(filePath));
try {
// svp je veux à chaque fois qu 'il lire la ligne et le couper nombre par nombre et le met dans m1 et le deusieme dans m2
String line;
// Lecture du fichier ligne par ligne. Cette boucle se termine
// quand la méthode retourne la valeur null.
while ((line = buff.readLine()) != null) {
System.out.println(line);
// m1=lire_doc(line);
for(int i=0;i<=line.length();i++){
m2.add(Integer.parseInt(line.valueOf(i)));
}
// <editor-fold defaultstate="collapsed" desc="comment">
/*indice [0]=1;
for (int i=1;i<=m1.size;i++){
int j =indice[i-1];
indice[i] =indice[i-1]+(2*m1[j])+1;
}*/
//for (int i = 1; i <= m1.size(); i++){
//m2.add(Integer.parseInt(m1.get(i)));
for (int i = 0; i <= indice.size(); i++) {
if(i==0)
indice.add(1);
else
{
int j = indice.get(i - 1);
int l = indice.get(i - 1) + (2 * m2.get(j))+ 1;// </editor-fold>
indice.add(i, l );
}
//indice[i] =indice[i-1]+(2*tab[j])+1;
}
for (int h = 0;h<m2.size();h++){
System.out.println(m2);}
for (int h = 0;h<indice.size();h++){
System.out.println(indice);}
}
} finally {
// dans tous les cas, on ferme nos flux
buff.close();
}
} catch (IOException ioe) {
// erreur de fermeture des flux
System.out.println("Erreur --" + ioe.toString());
}}
public static void main(String[] args)throws IOException {
affiche("marwa");
}}
il m affiche l exeption suivante
5 1 2 6 1 3 1 1 1 2 2 2 6 4 6 3 6 5 2 6 1 1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 121, Size: 46
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at javaapplication2.heuristique1.affiche(heuristique1.java:90)
at javaapplication2.heuristique1.main(heuristique1.java:119)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
2 réponses
-
Quel est l'intéret de lire chaque caractère d'un fichier ligne par ligne ? dans quel but ?
Process trop lourd qui n'aboutira pas avant 600 siècle -
Bonjour
________ ___. .__ \______ \ ____ __ _\_ |__ | | ____ ____ | | \ / _ \| | \ __ \| | / _ \ / \ | ' ( <_> ) | / \_\ \ |_( <_> ) | \ /_______ /\____/|____/|___ /____/\____/|___| / \/ \/ \/
La suite ici :https://forums.commentcamarche.net/forum/affich-24172016-exeption-en-java