write

Status
Nicht offen für weitere Antworten.
Hallo,
Ich habe in meine Anwendung zur Fehlersuche Daten in eine Datei geschrieben. Als der Fehler behoben war löschte ich den zugehörigen Code. Dann mußte ich die Fehlersuche wieder beginnen. Dabei trat ein merkwürdiges Phenomen auf. Zum Beipiel wurde mit
f2.write(d), f2.write(g[x][y]+" g[x] ") nichts in die Datei geschrieben, mit f2.write(g[x][y]+" g[x] ")//{hier ist ein Leerzeichen mehr}, f2.write(g[x][y]+" g[x][ ") gelang es Problemlos. Ursprünglich wollte ich f2.write(d) verwenden. d ist vom Typ char, g[x][y] ist int und f2 ein FileWriter. Ich habe null Ahnung woran das liegen könnte. Danke

PS
Das Programm (mein erstes) ist lang und noch unübersichtlich:
Z96 wird deklariert
Z146 zugehöriges try
Z149 initialiesierung von f2
Z162 Es beginnenz zwei Schleifen in der jedesmal das wirte aufgerufen wird
Z245 eine Zeilenschaltung
Z247 das besagt write
Z255 die Schleifen werden beendet
Z270 f2 wird geschlossen

Code:
import java.awt.*;
import java.util.*;
import java.text.*;
import com.sun.image.codec.jpeg.JPEGCodec;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.*;
import java.awt.event.*;

public class Listing1000 extends Thread {
    int i;

    C1000 counter;

    // Hilfsmatrix(Vergangenheit)
    int[][] v = new int[35][35];

    public Listing1000( int i, C1000 counter ) {
        this.counter = counter;
        this.i = i;
        this.v = v;
    }

    public static void main( String args[] ) throws Exception {
        // Hilfsmatrix(Vergangenheit)
        int[][] v = new int[35][35];
        Thread[] t = new Thread[3000];
        for (int i = 1000; i < 4000; i++) {
            System.out.println(i + " a ");
            C1000 c1000 = new C1000();
            t[(i - 1000)] = new Listing1000(i, c1000);
            t[(i - 1000)].start();
            System.out.println(i + " b ");
            t[(i - 1000)].join();
            System.out.println(i + " c ");
        }
        System.exit(0);
    }

    public void run() {
        synchronized (getClass()) {
            try {
                {
                    for (int y = 0; y < 35; y++) {
                        for (int x = 0; x < 35; x++) {
                            System.out.println("vorher" + " i " + i + " x " + x
                                    + " y " + y + " " + v[x][y]);
                            Thread.sleep(5);
                        }
                    }
                    v = counter.g(i, v);
                    for (int y = 0; y < 35; y++) {
                        for (int x = 0; x < 35; x++) {
                            System.out.println("nach " + " i " + i + " x " + x
                                    + " y " + y + " " + v[x][y]);
                            Thread.sleep(5);
                        }
                    }
                }
            } catch (InterruptedException e) {
                System.out.println("IE");
            }
        }
    }
}

class C1000 {
    // Hilfsdateien
    final boolean File1 = true;

    final boolean File2 = true;

    final boolean File3 = true;

    int U = 2;

    int X = 11;

    int Y = 67;

    int Z = (266);

    int W = (266);

    int P = 40;

    int P1 = 45;

    int P2 = 333;

    public C1000() {
    }

    public synchronized int[][] g( int name, int[][] v ) {
        int i = name;
        // File
        FileWriter f4;
        FileWriter f2;
        // Hilfsmatrix(Gegenwart)
        int[][] g = new int[35][35];
        for (int y = 0; y < 33; y++) {
            for (int x = 0; x < 33; x++) {
                g[x][y] = 0;
            }
        }
        // Hilfsvariabeln(orange Schlange)
        int xKopf = 17;
        int yKopf = 32;
        int xKopfAlt = v[33][33];
        int yKopfAlt = v[34][34];
        // Hilfsmatrix(Differenz)
        int[][] diff = new int[33][33];
        int ctrl = KeyEvent.VK_LEFT;
        // Hilfsvaribalen(gruene Schlange)
        int xKopfGr;
        int yKopfGr;
        int xKopfNeu = 17;
        int yKopfNeu = 31;
        // Hilfsvariablen Richtung
        int Richtung = 4;
        boolean wechsel, negativ2, negativ4, negativ6, negativ8;
        // Hilfsvariablen Sehen
        char d = '0';
        int ha = 0;
        int hb = 0;
        int hc = 0;
        // hier begann die Schleife
        GregorianCalendar cal = new GregorianCalendar();
        int style = DateFormat.MEDIUM;
        String pt;
        DateFormat df;
        df = DateFormat.getDateInstance(style);
        pt = (df.format(cal.getTime()) + "0");
        df = DateFormat.getTimeInstance(style);
        pt += (df.format(cal.getTime()));
        // pt+="-"+i;
        // File file=new File("screenshot/screenshot"+i+".jpg");
        Rectangle rt = new Rectangle(X, Y, Z, W);
        try {
            BufferedImage bi = new Robot().createScreenCapture(rt);
            // FileOutputStream out = new FileOutputStream(
            // "screenshot/screenshot"+i+".jpg" );
            // JPEGCodec.createJPEGEncoder( out ).encode( bi );
            // Bild in Zahlenmatrix transformieren
            // breite, hoehe und farbe=vier Zahlen
            try {
                f4 = new FileWriter("ma/ma" + i + ".txt");
                f2 = new FileWriter("null/null" + i + ".txt");
                wechsel = false;
                negativ2 = false;
                negativ4 = false;
                negativ6 = false;
                negativ8 = false;
                // xKopfAlt=xKopf;
                // yKopfAlt=yKopf;
                // (0)
                // if(!(xKopf==100)){System.out.print(xKopfAlt+" "+yKopfAlt+"
                // ");}
                xKopf = 100;
                yKopf = 100;
                // Matritzen werden erzeugt, ebenso werden die Koepfe berechnet
                for (int y = 0; y < 33; y++) {
                    for (int x = 0; x < 33; x++) {
                        v[x][y] = g[x][y];
                        d = '0';
                        int z7y = (y) * 4 * U + 5;
                        int z8y = (y) * 4 * U + 6;
                        int z7x = (x) * 4 * U + 5;
                        int z8x = (x) * 4 * U + 6;
                        int hm1 = bi.getRGB(z7x, z7y);
                        int hm2 = bi.getRGB(z7x, z8y);
                        int hm3 = bi.getRGB(z8x, z7y);
                        int hm4 = bi.getRGB(z8x, z8y);
                        // System.out.println(hm1+" "+hm2+" "+hm3+" "+hm4);
                        // ein int=vier byte
                        // einzelne Byte bekommen
                        Color c1 = new Color(hm1);
                        int h1a = c1.getRed();
                        int h1b = c1.getGreen();
                        int h1c = c1.getBlue();
                        Color c2 = new Color(hm2);
                        int h2a = c2.getRed();
                        int h2b = c2.getGreen();
                        int h2c = c2.getBlue();
                        Color c3 = new Color(hm3);
                        int h3a = c3.getRed();
                        int h3b = c3.getGreen();
                        int h3c = c3.getBlue();
                        Color c4 = new Color(hm4);
                        int h4a = c4.getRed();
                        int h4b = c4.getGreen();
                        int h4c = c4.getBlue();
                        ha = Math.round((h1a + h2a + h3a + h4a) / 4);
                        hb = Math.round((h1b + h2b + h3b + h4b) / 4);
                        hc = Math.round((h1c + h2c + h3c + h4c) / 4);
                        // System.out.println(ha+" "+hb+" "+hc+" ");
                        // Grauer Rahmen
                        if (ha == hb & hb == hc) {
                            d = '2';
                        }
                        if (ha == 161 & hb == 163 & hc == 168) {
                            d = '2';
                        }
                        if (ha == 144 & hb == 142 & hc == 151) {
                            d = '2';
                        }
                        if (ha == 176 & hb == 174 & hc == 176) {
                            d = '2';
                        }
                        if (ha == 169 & hb == 167 & hc == 176) {
                            d = '2';
                        }
                        if (ha == 152 & hb == 154 & hc == 159) {
                            d = '2';
                        }
                        if (ha == 151 & hb == 146 & hc == 151) {
                            d = '2';
                        }
                        // Gruene Schlange
                        if (ha == 0 & hb == 255 & hc == 0) {
                            d = '3';
                        }
                        if (ha == 0 & hb > 128 & hc == 0) {
                            d = '3';
                        }
                        if (ha == 72 & hb == 128 & hc == 0) {
                            d = '3';
                        }
                        if (ha == 54 & hb == 111 & hc == 0) {
                            d = '3';
                        }
                        if (ha == 102 & hb == 181 & hc == 0) {
                            d = '3';
                        }
                        if (ha == 54 & hb == 92 & hc == 0) {
                            d = '3';
                        }
                        if (hc == 0) {
                            d = '3';
                        }
                        if (ha == 24 & hb == 99 & hc == 10) {
                            d = '3';
                        }
                        // blauer Hintergrund=Spielfeld
                        if (ha == 0 & hb == 0 & hc == 255) {
                            d = '5';
                        }
                        // Rote Aepfel
                        if (ha > 200 & hb == 0 & hc == 0) {
                            d = '6';
                        }
                        // orange Schlange
                        if (ha > 200 & hb > 100 & hc < 100) {
                            d = '1';
                        }
                        if (ha > 130 & hb > 100 & hc < 90) {
                            d = '1';
                        }
                        // Roter Ball
                        if ((h1a == 139 | h2a == 139 | h3a == 139 | h4a == 139)
                                & hb == 0 & hc == 0) {
                            d = '4';
                        }
                        // Gelbe Aepfel
                        if (ha > 200 & hb > 200 & hc < 10) {
                            d = '7';
                        }
                        // Matritzen einlesen
                        g[x][y] = ((int) (byte) d) - 48;
                        diff[x][y] = v[x][y] - g[x][y];
                        // Koepfe berechen
                        if (diff[x][y] == (4)) {
                            xKopf = x;
                            yKopf = y;
                            g[33][33] = x;
                            g[34][34] = y;
                        }
                        if (diff[x][y] == (2)) {
                            xKopfGr = x;
                            yKopfGr = y;
                        }
                        // Daten in Datei einschreiben
                        try {
                            Thread.sleep(5);
                        } catch (InterruptedException e) {
                            System.out.println("Fehler");
                        }
                        System.out.println("in der Matrix " + "i " + i + "x "
                                + x + "y " + y + " " + d);
                        try {
                            if (x == 0) {
                                f4.write("\n");
                            }
                            if (x == 0) {
                                f2.write("\n");
                            }
                            f2.write(g[x][y] + " g[x]  ");
                            f4.write(g[x][y] + " g[x][y] " + d + " d " + i
                                    + " i " + x + "x" + y + "y" + ha + "ha"
                                    + hb + "hb" + hc + "hc" + h1a + "h1a" + h2a
                                    + "h2a" + h3a + "h3a" + h4a + "h4a" + h1b
                                    + "h1b" + h2b + "h2b" + h3b + "h3b" + h4b
                                    + "h4b" + h1c + "h1c" + h2c + "h2c" + h3c
                                    + "h3c" + h4c + "h4a ");
                        } catch (IOException e) {
                            System.out
                                    .println("Fehler beim Erstellen der Datei (f2)");
                        }
                    }
                }
                System.out.println(xKopf);
                if (!(xKopf == 100)) {
                    try {
                        Thread.sleep(P);
                    } catch (InterruptedException e) {
                        System.out.print("InterruptedException ");
                    }
                    try {
                        f4.close();
                        f2.close();
                        // out.close();
                    } catch (IOException e) {
                        System.out.print("Fehler out");
                    }
                    // Berechnen des Weges
                    // Richtung der orangen Schlange(HandyTastatur)
                    System.out.print(Richtung + "    ");
                    if (yKopf == yKopfAlt) {
                        if (xKopf > xKopfAlt) {
                            Richtung = 6;
                        } else if (xKopf < xKopfAlt) {
                            Richtung = 4;
                        }
                    } else if (xKopf == xKopfAlt) {
                        if (yKopf > yKopfAlt) {
                            Richtung = 8;
                        } else if (yKopf < yKopfAlt) {
                            Richtung = 2;
                        }
                    }
                    // (2)
                    // System.out.print(xKopf+" "+yKopf+" ");
                    // ein Schritt tief
                    // berechnen des naechsten Schritts, wenn nicht eingegriffen
                    // wird
                    if (Richtung == 2) {
                        xKopfNeu = xKopf;
                        yKopfNeu = yKopf - 1;
                    }
                    if (Richtung == 8) {
                        xKopfNeu = xKopf;
                        yKopfNeu = yKopf + 1;
                    }
                    if (Richtung == 6) {
                        xKopfNeu = xKopf + 1;
                        yKopfNeu = yKopf;
                    }
                    if (Richtung == 4) {
                        xKopfNeu = xKopf - 1;
                        yKopfNeu = yKopf;
                    }
                    // vergleichen
                    negativ2 = false;
                    negativ4 = false;
                    negativ6 = false;
                    negativ8 = false;
                    wechsel = false;
                    // (3)
                    // muß mit (1)uebereinstimmen
                    // System.out.print(wechsel+" ");
                    // (4)
                    // Rand, Felder
                    if (yKopf - 2 < 0 || g[xKopf][yKopf - 2] < 5) {
                        negativ2 = true;
                        System.out.print("neg2 ");
                    }
                    if (yKopf + 2 > 32 || g[xKopf][yKopf + 2] < 5) {
                        negativ8 = true;
                        System.out.print("neg8 ");
                    }
                    if (xKopf + 2 > 32 || g[xKopf + 2][yKopf] < 5) {
                        negativ6 = true;
                        System.out.print("neg6 ");
                    }
                    if (xKopf - 2 < 0 || g[xKopf - 2][yKopf] < 5) {
                        negativ4 = true;
                        System.out.print("neg4 ");
                    }
                    if (negativ2 == false) {
                        System.out.print(" A " + g[xKopf][yKopf - 2] + " ");
                        if (g[xKopf][yKopf - 2] < 5) {
                            negativ2 = true;
                            System.out.print("neg2 ");
                        }
                    }
                    if (negativ8 == false) {
                        System.out.print(" v " + g[xKopf][yKopf + 2] + " ");
                        if (g[xKopf][yKopf + 2] < 5) {
                            negativ8 = true;
                            System.out.print("neg8 ");
                        }
                    }
                    if (negativ6 == false) {
                        System.out.print(" => " + g[xKopf + 2][yKopf] + " ");
                        if (g[xKopf + 2][yKopf] < 5) {
                            negativ6 = true;
                            System.out.print("neg6 ");
                        }
                    }
                    if (negativ4 == false) {
                        System.out.print(" <= " + g[xKopf - 2][yKopf] + " ");
                        if (g[xKopf - 2][yKopf] < 5) {
                            negativ4 = true;
                            System.out.print("neg4 ");
                        }
                    }
                    // negativ:zufaellig einen naechsten Schritt berechnen
                    if (Richtung == 2 & negativ2 == true) {
                        wechsel = true;
                    }
                    if (Richtung == 4 & negativ4 == true) {
                        wechsel = true;
                    }
                    if (Richtung == 6 & negativ6 == true) {
                        wechsel = true;
                    }
                    if (Richtung == 8 & negativ8 == true) {
                        wechsel = true;
                    }
                    Robot Control = new Robot();
                    // (4)
                    System.out.print(Richtung + "    ");
                    // (5)
                    System.out.print(wechsel + "    ");
                    // (6)
                    // Falls (6)erscheint //= true// wechselt die Richtung
                    if (wechsel == true) {
                        if (Richtung == 2) {
                            if (negativ4 == false) {
                                Richtung = 4;
                                try {
                                    Thread.sleep(P1);
                                } catch (InterruptedException e) {
                                    System.out.print("InterruptedException;");
                                }
                                ctrl = KeyEvent.VK_LEFT;
                                System.out.print("VK_LEFT  ");
                                Control.keyPress(ctrl);
                                try {
                                    Thread.sleep(P2);
                                } catch (InterruptedException e) {
                                    System.out.print("InterruptedException;");
                                }
                                Control.keyRelease(ctrl);
                            } else if (negativ6 == false) {
                                Richtung = 6;
                                try {
                                    Thread.sleep(P1);
                                } catch (InterruptedException e) {
                                    System.out.print("InterruptedException;");
                                }
                                ctrl = KeyEvent.VK_RIGHT;
                                System.out.print("VK_RIGHT ");
                                Control.keyPress(ctrl);
                                try {
                                    Thread.sleep(P2);
                                } catch (InterruptedException e) {
                                    System.out.print("InterruptedException;");
                                }
                                Control.keyRelease(ctrl);
                            } else {
                                System.out.println("Fehler");
                            }
                        } else if (Richtung == 4) {
                            if (negativ8 == false) {
                                Richtung = 8;
                                try {
                                    Thread.sleep(P1);
                                } catch (InterruptedException e) {
                                    System.out.print("InterruptedException;");
                                }
                                ctrl = KeyEvent.VK_DOWN;
                                Control.keyPress(ctrl);
                                System.out.print("VK_DOWN ");
                                try {
                                    Thread.sleep(P2);
                                } catch (InterruptedException e) {
                                    System.out.print("InterruptedException;");
                                }
                                Control.keyRelease(ctrl);
                            } else if (negativ2 == false) {
                                Richtung = 2;
                                try {
                                    Thread.sleep(P1);
                                } catch (InterruptedException e) {
                                    System.out.print("InterruptedException;");
                                }
                                ctrl = KeyEvent.VK_UP;
                                Control.keyPress(ctrl);
                                System.out.print("VK_UP   ");
                                try {
                                    Thread.sleep(P2);
                                } catch (InterruptedException e) {
                                    System.out.print("InterruptedException;");
                                }
                                Control.keyRelease(ctrl);
                            } else {
                                System.out.println("Fehler");
                            }
                        } else if (Richtung == 6) {
                            if (negativ8 == false) {
                                Richtung = 8;
                                try {
                                    Thread.sleep(P1);
                                } catch (InterruptedException e) {
                                    System.out.print("InterruptedException;");
                                }
                                ctrl = KeyEvent.VK_DOWN;
                                Control.keyPress(ctrl);
                                System.out.print("VK_DOWN ");
                                try {
                                    Thread.sleep(P2);
                                } catch (InterruptedException e) {
                                    System.out.print("InterruptedException;");
                                }
                                Control.keyRelease(ctrl);
                            } else if (negativ2 == false) {
                                Richtung = 2;
                                try {
                                    Thread.sleep(P1);
                                } catch (InterruptedException e) {
                                    System.out.print("InterruptedException;");
                                }
                                ctrl = KeyEvent.VK_UP;
                                Control.keyPress(ctrl);
                                System.out.print("VK_UP ");
                                try {
                                    Thread.sleep(P2);
                                } catch (InterruptedException e) {
                                    System.out.print("InterruptedException;");
                                }
                                Control.keyRelease(ctrl);
                            } else {
                                System.out.println("Fehler");
                            }
                        } else if (Richtung == 8) {
                            if (negativ4 == false) {
                                Richtung = 4;
                                try {
                                    Thread.sleep(P1);
                                } catch (InterruptedException e) {
                                    System.out.print("InterruptedException;");
                                }
                                ctrl = KeyEvent.VK_LEFT;
                                Control.keyPress(ctrl);
                                System.out.print("VK_LEFT  ");
                                try {
                                    Thread.sleep(P2);
                                } catch (InterruptedException e) {
                                    System.out.print("InterruptedException;");
                                }
                                Control.keyRelease(ctrl);
                            } else if (negativ6 == false) {
                                Richtung = 6;
                                try {
                                    Thread.sleep(P1);
                                } catch (InterruptedException e) {
                                    System.out.print("InterruptedException;");
                                }
                                ctrl = KeyEvent.VK_RIGHT;
                                Control.keyPress(ctrl);
                                System.out.print("VK_RIGHT  ");
                                try {
                                    Thread.sleep(P2);
                                } catch (InterruptedException e) {
                                    System.out.print("InterruptedException;");
                                }
                                Control.keyRelease(ctrl);
                            } else {
                                System.out.println("Fehler");
                            }
                        }
                    }
                    if (File3 == true) {
                        try {
                            // History
                            FileWriter f3 = new FileWriter("his/his.txt");
                            f3.write("   i " + i + " ctrl " + ctrl + " neg2 "
                                    + negativ2 + " neg4 " + negativ4 + " neg6 "
                                    + negativ6 + " neg8 " + negativ8 + " Rtg "
                                    + Richtung + " wechsel " + wechsel
                                    + " yKpf " + yKopf + " xKpf " + xKopf
                                    + " yKpfN " + yKopfNeu + " xKpfN "
                                    + xKopfNeu + " yKpflt " + yKopfAlt
                                    + " xKpflt " + xKopfAlt + "\n");
                            f3.close();
                        } catch (IOException e) {
                            System.out
                                    .println("Fehler beim Erstellen der Datei (f1)");
                        }
                    }
                    // Tasten steuern
                    // (6)
                    System.out.println(Richtung + "    ");
                }
            } catch (IOException e) {
                System.out.println("IOException");
            }
        } catch (AWTException e) {
            System.out.println("java.awt.AWTException ");
        }
        return g;
    }
}

[Beni hat sich erlaubt, ein Formater über den Code zu jagen. Die Zeilennummer stimmen jetzt aber nicht mehr exakt! (es hat jetzt etwa 10 weniger als zuvor)]
 

mic_checker

Top Contributor
1) Wenn sich jemand ernsthaft den Code angucken soll -> bitte richtig formatieren
2) Werden Exceptions ausgelöst ?
3) Dein Problem ist das der Code so wie er oben steht nichts in eine Datei schreibt - wie gewünscht ?
 
A) ein witz ist das LEIDER nicht
B)
1)Beni hat den Code neu Formatiert, die neuen Zeilennummern sind:
Z98 f2 wird deklariert
Z146 zugehöriges try zur initialiesieung
Z148 initialisierung von f2
Z162 Es beginnen zwei Schleifen in der jedesmal das wirte aufgerufen wird
Z290 das try für die nächsten beiden Anweisungen
Z294 eine Zeilenschaltung
Z297 das besagt write
Z310 die Schleifen werden beendet
Z318 das try das zu f2.close gehört
Z320 f2 wird geschlossen
2) Jede Exception würde zu einem Ausgabe führen, diese treten nicht auf
3)Ja, das Problem ist wenn innerhalb der geschweiften Klammern(Zeile 297) zum Beispiel nach g[x] kein Leerzeichen steht wird in null/null1000.txt nichts geschrieben, wenn aber drei Leerzeichen folgen, dann funktioniert es reibungslos. Zur Probe muß das Unterverzeichnis null und ma angelegt werden. Danke
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
A Instance methods should not write to "static" fields Java Basics - Anfänger-Themen 4
N Threads Read-Modify-Write Problem bei Multithreading (philosopher dining problem) Java Basics - Anfänger-Themen 5
S write(), weshalb verschiedene Ausgaben? Java Basics - Anfänger-Themen 4
K exec.StdIn.Write Java Basics - Anfänger-Themen 7
B Input/Output Socket I/O - outputStream.write(-1) Java Basics - Anfänger-Themen 2
X write() und Zufallszahlen Java Basics - Anfänger-Themen 2
K Input/Output read/write Java Basics - Anfänger-Themen 15
J FileOutputStream write Java Basics - Anfänger-Themen 19
Corben Input/Output FileOutputStream - neue Zeile mit write(10) Java Basics - Anfänger-Themen 6
F FileOutputStream.write Java Basics - Anfänger-Themen 2
M Unterschied append / write aus der Klasse Writer Java Basics - Anfänger-Themen 2
W file read write crash Java Basics - Anfänger-Themen 2
M Audio Stream läuft auf :connection abort: socket write error Java Basics - Anfänger-Themen 2
M Pfad angabe bei getRescource und ImageIO.write Java Basics - Anfänger-Themen 4
hdi Datentypen FileOutputStream#write(int) mit ints? Java Basics - Anfänger-Themen 5
G ImageIO.write() Java Basics - Anfänger-Themen 2
M Streams, read and write Java Basics - Anfänger-Themen 2
I ImageIO.write(bild, "was gibts da alles", pfad); Java Basics - Anfänger-Themen 6
D FileWriter write? Zeilenumbruch ? Java Basics - Anfänger-Themen 4
S Technik hinter der write Methode Java Basics - Anfänger-Themen 5
S Inhalt aus Array auf JSP anzeigen mit bean:write Java Basics - Anfänger-Themen 2
F javax.imageio.ImageIO write methode Java Basics - Anfänger-Themen 11

Ähnliche Java Themen

Neue Themen


Oben