[JAVA] String schrijven in array

Pagina: 1
Acties:

  • xos
  • Registratie: Januari 2002
  • Laatst online: 28-08 15:35
Ik heb een tekstbestand (invoer.txt) en deze wil ik openen en vervolgens sorteren en weer wegschrijven naar uitvoer.txt.
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    public void inPut() throws IOException{
        File inputFile = new File("invoer.txt");        //invoer file
        FileReader openstream = new FileReader(inputFile);
        BufferedReader in = new BufferedReader(openstream);
                
        File outputFile = new File("uitvoer.txt");      //uitvoer file
        FileWriter out = new FileWriter(outputFile);
        
        StringWriter arrayWriter = new StringWriter((int)inputFile.length());   
        String[] stringArray = new String[0];       
        
        String input;
      while ((input = in.readLine()) != null) {
       //Hier zouden dus de strings in de array geschreven moeten worden                                               
      }
      in.close();                                           
    
    }

Voor een chararray is er zoiets als een chararraywriter. Met onderstaande constructie is het al gelukt om data in een array te plaatsen.
code:
1
2
3
4
5
6
7
    CharArrayWriter caw = new CharArrayWriter( (int)inputFile.length() ); 
    char[] charArray = new char[255];
    int c;
    while ((c = in.read(charArray)) != -1){                             

    caw.write(charArray,0,c);                                            
    }

Alleen hoe doe ik dit met een string??

  • SWfreak
  • Registratie: Juni 2001
  • Niet online
Erhm, alle strings in een Vector rammen en die vector vervolgens sorteren?

  • nxt
  • Registratie: November 2001
  • Laatst online: 26-08 13:51

nxt

iets als
code:
1
2
3
4
java.util.ArrayList al=new java.util.ArrayList();
while ((input = in.readLine()) != null) {
     al.add(input);
}

?

  • Glimi
  • Registratie: Augustus 2000
  • Niet online

Glimi

Designer Drugs

(overleden)
Op zondag 26 mei 2002 14:35 schreef SWfreak het volgende:
Erhm, alle strings in een Vector rammen en die vector vervolgens sorteren?
ArrayList. Vector is Syncronized en we willen die overhead daarvan helemaal niet hebben als we maar met 1 thread werken toch?

  • xos
  • Registratie: Januari 2002
  • Laatst online: 28-08 15:35
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    public void inPut() throws IOException{
        File inputFile = new File("invoer.txt");    
        FileReader openstream = new FileReader(inputFile);
        BufferedReader in = new BufferedReader(openstream);
                
        File outputFile = new File("uitvoer.txt");
        FileWriter out = new FileWriter(outputFile);
        
        //StringWriter arrayWriter = new StringWriter((int)inputFile.length()); 
        //String[] stringArray = new String[0];     
        
        String input;
        ArrayList arrayList = new ArrayList();
        while ((input = in.readLine()) != null) {
         arrayList.add(input);
        }
        in.close();                                     
    }

Oke dit werkt nu alleen nog die list na een array schrijven maar dat zou vrij simpel moeten kunnen volgens mij.
Bedankt.

  • wasigh
  • Registratie: Januari 2001
  • Niet online

wasigh

wasigh.blogspot.com

heeft het geen methode toArray() ??

  • Glimi
  • Registratie: Augustus 2000
  • Niet online

Glimi

Designer Drugs

(overleden)
http://java.sun.com/j2se/1.4/docs/api/java/util/ArrayList.html#toArray()
Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.
If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.
[Note]Waarom zoek ik dit eigenlijk voor iemand op met m'n 56k ;(
Pagina: 1