PrintWriter似乎没有写入文件,即使我关闭它

时间:2015-08-10 07:31:13

标签: ioexception printwriter

我尝试使用PrintWriter打印到名为.txt的预定output.txt文件中。 我没有尝试将FileWriter放在PrintWriter内,也没有运气。我也关闭它了。最奇怪的是,在我的其他代码中,PrintWriter的另一个类似用法似乎工作得很好(第二个)。 有人可以给我一些指示/提示吗?

public static void main(String[] args) throws IOException{

    if(args.length <4){
        return;
    }else{
        String patternFileN = args[0];
        String sequenceFileN = args[1];
        int numOfSeqs = Integer.parseInt(args[2]);
        int pattInd = Integer.parseInt(args[3]);
        if(numOfSeqs <= 0){
            return;
        }
        String pattern;
        String[] txtSequence = new String[numOfSeqs];
        PrintWriter outFile = null;
        try{

            String outFileN = "output.txt";
            outFile = new PrintWriter(new File(outFileN));

            for(int seqInd = 0; seqInd < numOfSeqs; seqInd++){
                int offset = RKscanner.seqSearch(txtSequence[seqInd]);
                if(offset < txtSequence[seqInd].length()){
                    //outFile.println(pattInd + " " + seqInd);
                    outFile.println("nigaaaa");

                }
            }
        }catch(IOException e){
            e.printStackTrace();
            System.err.println("files couldn't be opened");
        }finally{
            outFile.close();
        }

    }
}

此代码工作正常:

public static void main(String[]args) throws IOException{
    int letters = 4;
    int pattLength, possNum; //number of possible sequences

    try{
        if(args.length <=1){
            System.err.println("please specify output filename & lengths of patterns to be generated");
            System.err.println("---try: java patternGen pattern.txt 5");
        }else{  
            String fileName = args[0];
            PrintWriter outFile = new PrintWriter(new FileWriter(fileName));
            pattLength = Integer.parseInt(args[1]);
            possNum = (int)Math.pow(letters, pattLength); //possible number of combinations without wildcards.
            //Nucleotide[][] patternBuild = new Nucleotide[possNum][pattLength];//the built pattern to be used for consructor.
            StringBuilder sb = new StringBuilder(pattLength);
            for(int i = 0; i < possNum; i++){
                sb.setLength(0);
                StringBuilder alphToNuc = new StringBuilder(0);
                String sbStore = sb.toString();
                //...then string builders populated
                //System.out.println("alphabet: "+sb);
                outFile.println(alphToNuc);
                System.out.println(alphToNuc);
            }
            outFile.close();    
        }

    }catch(IOException e){
        e.printStackTrace();
        }
}

1 个答案:

答案 0 :(得分:0)

固定!

问题是我在循环中使用bash脚本运行这些java文件,因此当我打开并在java文件中写入这些文件时,文件将覆盖。

抱歉无关紧要的问题