首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PrintWriter似乎并不是在写文件,即使我要关闭它

PrintWriter似乎并不是在写文件,即使我要关闭它
EN

Stack Overflow用户
提问于 2015-08-10 07:31:13
回答 1查看 235关注 0票数 1

我正在尝试使用PrintWriter打印到一个名为output.txt的预定.txt文件中。我还没有尝试将File和FileWriter都放入PrintWriter中,而且没有任何运气。我也要关闭它。其中最奇怪的一点是,在我的其他代码中,PrintWriter的另一种类似用法似乎非常好(第二种)。有人能给我一些提示吗?

代码语言:javascript
运行
复制
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();
        }

    }
}

此代码运行良好:

代码语言:javascript
运行
复制
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();
        }
}
EN

Stack Overflow用户

回答已采纳

发布于 2015-08-13 06:13:09

修好了!

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

很抱歉提出了一些无关紧要的问题

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31913905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档