简体   繁体   English

为什么我的程序不将数据写入文本文件?

[英]Why doesn't my programme write data to a text file?

The programme readS the file line by line. 程序逐行读取文件。
If the programme finds in a line [@: Object successfully summoned] , it won't write to the second file. 如果程序在[@: Object successfully summoned]行中找到,它将不会写入第二个文件。
Probably the problem is in the try/catch/finally part. 问题可能出在try / catch / finally部分。 I have tried many changes a few times, but i can't fix the problem. 我几次尝试了许多更改,但无法解决问题。

This is what I have programmed: 这是我编写的:

package stringsHerkennen;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class MC {

    private static PrintWriter outputFile;

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException{
        FileInputStream fstream = null;
        FileWriter fwriter = null;

        fstream = new FileInputStream("C:\\Users\\Tim\\Documents\\test.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String strLine;
        fwriter = new FileWriter("bewerkte log.txt", true);
        outputFile = new PrintWriter(fwriter);
        try{
             while ((strLine = br.readLine()) !=null) {
                 try{
                 System.out.println(strLine);
                 String b = strLine;
                 String tokens[] = strLine.split("]: ");
                 String c = tokens[1].toString();
                 //System.out.println(tokens[1]);
                 Hamming H = new Hamming("[@: Object successfully summoned]", c);
                int Pi = H.getHammingDistance();
                //System.out.println(Pi);
                 if(!(Pi==0)){
                     //System.out.println("geschreven naar file");
                    outputFile.println(b);


                     //System.out.println("schrijven naar andere file");
                 }
                 else{
                     //System.out.println("niet geschreven naar file");

                 }


             //try2
             }
                catch(Exception ArrayIndexOutOfBoundsException){
                     //System.out.println("Schrijven naar File");

                     outputFile.println(strLine);

             }
             finally {
                     if (fstream != null)
                         fstream.close();}


             }

             //try1
        }
        catch(Exception ArrayIndexOutOfBoundsException){
                 fstream.close();
                 br.close();
        }
        finally {
                 if (fstream != null)
                     fstream.close();}
            System.out.println("klaar");


    }
}

This is the text file, that you want to filter 这是您要过滤的文本文件

sun.nio.cs.StreamDecoder.readBytes(Unknown Source) ~[?:1.7.0_13]#-  at 
sun.nio.cs.StreamDecoder.implRead(Unknown Source) ~[?:1.7.0_13]
    at sun.nio.cs.StreamDecoder.read(Unknown Source) ~[?:1.7.0_13]
    at java.io.InputStreamReader.read(Unknown Source) ~[?:1.7.0_13]
    at java.io.BufferedReader.fill(Unknown Source) ~[?:1.7.0_13]
    at java.io.BufferedReader.readLine(Unknown Source) ~[?:1.7.0_13]
    at java.io.BufferedReader.readLine(Unknown Source) ~[?:1.7.0_13]
    at ll.run(SourceFile:78) [minecraft_server.1.7.2.exe:?]
[15:39:13] [Server thread/INFO]: Starting minecraft server version 1.7.2
[15:39:13] [Server thread/INFO]: Loading properties
[15:39:13] [Server thread/INFO]: Default game type: SURVIVAL
[15:39:13] [Server thread/INFO]: Generating keypair
[15:39:13] [Server thread/INFO]: Starting Minecraft server on *:25565
[15:39:13] [Server thread/INFO]: Preparing level "world 3"
[15:39:13] [Server thread/INFO]: Preparing start region for level 0
[15:39:14] [Server thread/INFO]: Done (0,820s)! For help, type "help" or "?"
[17:09:01] [Server thread/INFO]: [@: Object successfully summoned]
[17:09:01] [Server thread/INFO]: [@: Object successfully summoned]
[17:09:01] [Server thread/INFO]: [@: Object successfully summoned]
[17:09:01] [Server thread/INFO]: [@: Object successfully summoned]
[17:09:02] [Server thread/INFO]: [@: Object successfully summoned]
[17:09:02] [Server thread/INFO]: [@: Object successfully summoned]
[17:09:02] [Server thread/INFO]: [@: Object successfully summoned]
[17:09:02] [Server thread/INFO]: [@: Object successfully summoned]
[17:09:02] [Server thread/INFO]: [@: Object successfully summoned]
[17:09:02] [Server thread/INFO]: [@: Object successfully summoned]

Your code is a little messy and hard to follow. 您的代码有点混乱并且很难遵循。 I'd write it more like this: 我会这样写:

package stringsHerkennen;

import java.io.*;


public class MC {
    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        FileInputStream fstream = null;
        FileWriter fwriter = null;
        PrintWriter outputFile = null;

        try {
            fstream = new FileInputStream("C:\\Users\\Tim\\Documents\\test.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;
            fwriter = new FileWriter("bewerkte log.txt", true);
            outputFile = new PrintWriter(fwriter);
            while ((strLine = br.readLine()) != null) {
                String tokens [] = strLine.split("]: ");
                if ((tokens != null) && (tokens.length > 0)) {
                    Hamming H = new Hamming("[@: Object successfully summoned]", tokens[1]);
                    int Pi = H.getHammingDistance();
                    if (Pi != 0) {
                        outputFile.println(b);
                    } else {
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace(); 
        } finally {
            closeQuietly(outputFile);
            closeQuietly(fwriter);
            closeQuietly(fstream);
        }
    }

    public static void closeQuietly(InputStream is) {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
            // Quietly....log the exception if you like
        }
    }

    public static void closeQuietly(Reader r) {
        try {
            if (r != null) {
                r.close();
            }
        } catch (IOException e) {
            // Quietly....log the exception if you like
        }
    }

    public static void closeQuietly(Writer w) {
        try {
            if (w != null) {
                w.close();
            }
        } catch (IOException e) {
            // Quietly....log the exception if you like
        }
    }
}

I believe it is bad practice to place code to be executed in the Catch block. 我认为将要执行的代码放在Catch块中是一种不好的做法。 However are you flushing the output from the PrintWriter? 但是,您是否要刷新PrintWriter的输出?

outputFile.flush(); 

might be what your looking for if the contents aren't being placed into the second file. 如果未将内容放入第二个文件,则可能是您要寻找的内容。

1) remove the finally block inside the while loop 2) add outputFile.flush() in the finally block after the loop 1)移除while循环内的finally块2)循环后在finally块中添加outputFile.flush()

                //try2
            } catch (Exception ArrayIndexOutOfBoundsException) {
                //System.out.println("Schrijven naar File");

                outputFile.println(strLine);

            } 
             //**COMMENT this fstream is being closed in the while loop !!!

             /*finally {
                if (fstream != null)
                    fstream.close();
            }*/


        }

        //try1
    } catch (Exception ArrayIndexOutOfBoundsException) {
        fstream.close();
        br.close();
    } finally {
        outputFile.flush(); // ADD THIS TO FLUSH TO DISK
        if (fstream != null)
            fstream.close();
    }
    System.out.println("klaar");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM