[Java] - String[][] zu groß, wie in Datei speichern?

SunboX

Mitglied
Hi,

also ich habe eine String Matrix mit 15539 Zeilen und 11 Spalten
(String[15539][11]). Erstmal hab ich mir nichts dabei gedacht
(programmiere erst ein Jahr mit Java) und das Array mit viel Aufwandt
in eine eigene Klasse geschrieben (es sind statische Geodaten von
Deutschland). Jetzt wollte ich das äußere Array einmal durchlaufen und prüfen, ob alle inneren Arrays die Länge 11 haben. Das endete auch sogleich in der Fehlermeldung das die Methode größer 64kB sei. Ich habe schon überall gesucht, das problem ist mir jetzt einigermassen klar.
Jetzt wollte ich das Array in einer externen datei speichern, nur
wie??? (per Hand ist eher schlecht :eek:) ) Und wie kann ich es dann
später z.B. einmal komplett wieder durchlaufen? (das Programm ist
später für ein mobiles Endgerät gedacht (J2ME))

Habt ihr ein paar kleine (große?) Tips für mich? Wäre echt spitze

Ciao SunboX
 
Servus!

...oder so:

Code:
import java.io.*;

/*
 * ReadArray.java
 *
 * Created on 8. Juli 2003, 21:09
 */

/**
 *
 * @author  Administrator
 */
public class ReadArray {
    
    String[][] matrix = {{"a","b","c"},{"d","e","f"},{"g","h","i"}};
    String[][] mtx2 = new String[matrix.length][matrix[0].length];
    
    /** Creates a new instance of ReadArray */
    public ReadArray() {
    }
    
    public void doIt(){
        
        try{  
        
        FileWriter fw = new FileWriter("c:\\myfile.dat");
        BufferedWriter bw = new BufferedWriter(fw);
        StringBuffer sb = new StringBuffer();
        
                   
        for(int i = 0; i < matrix.length; i++){
            for(int j = 0; j < matrix[i].length; j++){
                sb.append(matrix[i][j]+"@");
            }
            bw.write(sb.toString()+"\r\n");
            sb.delete(0, matrix[i].length*2);
            
        }
        
        bw.flush();
        bw.close();
        fw.close();
        
        
        FileReader fr = new FileReader("c:\\myfile.dat");
        BufferedReader br= new BufferedReader(fr);
        String s = null;
        int i = 0;
        
        while( (s = br.readLine() ) != null){
            mtx2[i++] = s.split("@");
        }
        
        for(int n = 0; n < mtx2.length; n++){
            for (int m = 0; m < mtx2[n].length; m++){
                System.out.print(mtx2[n][m] + ",");
            }
            System.out.println();
        }
            br.close();
            fr.close();
            
        
        System.out.println("Fertig");
        
        }catch(IOException ioe){
            System.out.println("IO Fehler: " +ioe);
        }
        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        new ReadArray().doIt();
    }
    
}

Gruss Tom
 
Servus!

Version 2... mit ObjectOutputStream ... ;-)

Code:
import java.io.*;

/*
 * ReadArray.java
 *
 * Created on 8. Juli 2003, 21:09
 */

/**
 *
 * @author  Administrator
 */
public class ReadArray {
    
    String[][] matrix = {{"a","b","c"},{"d","e","f"},{"g","h","i"}};
    String[][] mtx2 = new String[matrix.length][matrix[0].length];
    
    /** Creates a new instance of ReadArray */
    public ReadArray() {
    }
    
    public void doIt(){
        
        try{  
        
        FileWriter fw = new FileWriter("c:\\myfile.dat");
        BufferedWriter bw = new BufferedWriter(fw);
        StringBuffer sb = new StringBuffer();
        
                   
        for(int i = 0; i < matrix.length; i++){
            for(int j = 0; j < matrix[i].length; j++){
                sb.append(matrix[i][j]+"@");
            }
            bw.write(sb.toString()+"\r\n");
            sb.delete(0, matrix[i].length*2);
            
        }
        
        bw.flush();
        bw.close();
        fw.close();
        
        
        FileReader fr = new FileReader("c:\\myfile.dat");
        BufferedReader br= new BufferedReader(fr);
        String s = null;
        int i = 0;
        
        while( (s = br.readLine() ) != null){
            mtx2[i++] = s.split("@");
        }
        
        for(int n = 0; n < mtx2.length; n++){
            for (int m = 0; m < mtx2[n].length; m++){
                System.out.print(mtx2[n][m] + ",");
            }
            System.out.println();
        }
            br.close();
            fr.close();
            
        
        System.out.println("Fertig");
        
        }catch(IOException ioe){
            System.out.println("IO Fehler: " +ioe);
        }
        
    }
    
    public void doIt2(){
       
        try{
            
        FileOutputStream fos = new FileOutputStream("c:\\myfile2.dat");
        
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        
        oos.writeObject(matrix);
        
        oos.flush();
        oos.close();
        
        FileInputStream fis = new FileInputStream("c:\\myfile2.dat");
        
        ObjectInputStream ois = new ObjectInputStream(fis);
        
        mtx2 = (String[][])ois.readObject();
        
        ois.close();
        fis.close();
        
        for(int n = 0; n < mtx2.length; n++){
            for (int m = 0; m < mtx2[n].length; m++){
                System.out.print(mtx2[n][m] + ",");
            }
            System.out.println();
        }
        
        System.out.println("Fertig!");
        
        }catch(IOException ioe){
            System.out.println("IO Fehler: " + ioe);
        }catch(ClassNotFoundException cnfe){
            System.out.println(cnfe);
        }
        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        new ReadArray().doIt2();
    }
    
}

Gruss Tom
 
He Danke! :)

Hab morgen Info Prüfung, und deshalb jetzt leider keine Zeit mehr *gleichInsBettMuss* :) Werds mir morgen nachmittag aber gleich mal anschauen. Tausend dank nochmal

Ciao SunboX
 
Konnte mich noch nicht überwinden, zubett zu gehen. :) Ich bekomm immernoch den Fehler:

Fehler:
Code:
java.lang.ClassFormatError: test/Resource (Code of a method longer than 65535 bytes)
	at java.lang.ClassLoader.defineClass0(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:250)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)
	at test.ReadArray.<init>(ReadArray.java:21)
	at test.ReadArray.main(ReadArray.java:122)
Exception in thread "main"

Dein Code von mir angepasst:
Code:
package test;

import java.io.*;

/*
 * ReadArray.java
 *
 * Created on 8. Juli 2003, 21:09
 */

/**
 *
 * @author  Administrator
 */
public class ReadArray {

    static final String FILE_NAME1 = "c:\\geo_daten1.dat";
    static final String FILE_NAME2 = "c:\\geo_daten2.dat";

    //String[][] matrix = {{"a","b","c"},{"d","e","f"},{"g","h","i"}};
    String[][] matrix = Resource.GEO_DATEN;
    String[][] mtx2 = new String[matrix.length][matrix[0].length];

    /** Creates a new instance of ReadArray */
    public ReadArray() {
    }

    public void doIt(){

        try{

        FileWriter fw = new FileWriter(FILE_NAME1);
        BufferedWriter bw = new BufferedWriter(fw);
        StringBuffer sb = new StringBuffer();


        for(int i = 0; i < matrix.length; i++){
            for(int j = 0; j < matrix[i].length; j++){
                sb.append(matrix[i][j]+"@");
            }
            bw.write(sb.toString()+"\r\n");
            sb.delete(0, matrix[i].length*2);

        }

        bw.flush();
        bw.close();
        fw.close();


        FileReader fr = new FileReader(FILE_NAME1);
        BufferedReader br= new BufferedReader(fr);
        String s = null;
        int i = 0;

        while( (s = br.readLine() ) != null){
            mtx2[i++] = s.split("@");
        }

        for(int n = 0; n < mtx2.length; n++){
            for (int m = 0; m < mtx2[n].length; m++){
                System.out.print(mtx2[n][m] + ",");
            }
            System.out.println();
        }
            br.close();
            fr.close();


        System.out.println("Fertig");

        }catch(IOException ioe){
            System.out.println("IO Fehler: " +ioe);
        }

    }

    public void doIt2(){

        try{

        FileOutputStream fos = new FileOutputStream(FILE_NAME2);

        ObjectOutputStream oos = new ObjectOutputStream(fos);

        oos.writeObject(matrix);

        oos.flush();
        oos.close();

        FileInputStream fis = new FileInputStream(FILE_NAME2);

        ObjectInputStream ois = new ObjectInputStream(fis);

        mtx2 = (String[][])ois.readObject();

        ois.close();
        fis.close();

        for(int n = 0; n < mtx2.length; n++){
            for (int m = 0; m < mtx2[n].length; m++){
                System.out.print(mtx2[n][m] + ",");
            }
            System.out.println();
        }

        System.out.println("Fertig!");

        }catch(IOException ioe){
            System.out.println("IO Fehler: " + ioe);
        }catch(ClassNotFoundException cnfe){
            System.out.println(cnfe);
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        new ReadArray().doIt2();
    }

}

der selbe Fehler wie am Anfang. Java kann das Array nicht in einer Methode verarbeiten, da zu groß. Kann ich das irgendwie in teilen machen? Ich habe schon probiert es in Stücke zu verarbeiten (for i=0 bis 1000 ... u.s.w.) aber das ging auch nicht. Bin völlig Planlos... :(
 
genau tu das auf 2 methoden aufteilen.

Wenn der code in eine Methode verzweigt haut er dir alles auf den Stack. Dieser scheint bei dir "vollzulaufen" <- habe ich noch nie erlebt :D

Sprich über diese 65535 zu kommen.
Dies ist bei der Grösse deiner Matrize schnell möglich.

Du solltest nicht gebrauchte Strings direkt an den garbage collector übergeben und alle 1000 Strings den gc zum aufräumen zu bewegen.

Dies sollte dafür sorgen das nicht über die byte begrenzung kommst.

Durch den aufruf von System.gc(); kannst du den GarbageCollector selber zum aufräumen bewegen.
 
Zurück