Search code examples
ioexceptionprintwriter

PrintWriter doesn't seem to be writing to file even though I'm closing it


I'm trying to use PrintWriter to print into a predetermined .txt file called output.txt. I've not tried putting both File and FileWriter inside PrintWriter, and having no luck. I'm closing it too. Weirdest part of it is that another similar use of PrintWriter in my other code seems to be working perfectly fine (the 2nd one). Could someone flick me some pointers/hints please?

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();
        }

    }
}

This code is working fine:

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();
        }
}

Solution

  • FIXED!

    The problem was that I was running these java files with a bash script in a loop, so the file would overwrite When I open and wrote into these files in the java files.

    Sorry for irrelevantly topiced question