首页 文章

从Windows执行Java程序时会产生不同的输出

提问于
浏览
0

我正在运行一个Java程序来计算树中的所有路径 . 当我在windows上运行eclipse时,我看到一个output1,但是当我从jar或Mac运行相同的程序时,我注意到不同的输出 . 有很多差异 . 甚至文件大小也不同 . 缓冲区编写器在依赖平台时表现不同吗?

所以,我从jar执行或在Mac Eclipse上执行时输出相同,但从windows eclipse执行时输出不同 .

这是写入文件的代码:成员变量:

public  HashMap&ltString, HashSet&ltString>> nodeListFromFile = new HashMap&ltString, HashSet&ltString>>();

Funciton:
    public void getAllPaths(String root, String path){
        //Since we are assuming there are approximately 12-16 levels
        //And we are expecting a LCA in less than 16 steps
        //All paths evaluated are of max size of 16 using this counter
        int stepCounter = 0;
        File file = new File(outPutfilePath);

        try{
            if(!file.exists()){
                file.createNewFile();
            }  


            FileWriter fw = new FileWriter(file,true);
            BufferedWriter bw = new BufferedWriter(fw);

            //Iterate over each child node
            for(String childNode: nodeListFromFile.get(root)){
                String tempPath = path;
                if((tempPath.indexOf(childNode)  grandChildSet = nodeListFromFile.get(childNode);
                    boolean goodGrandChild = true;
                    for(String gc: grandChildSet){
                        if(!path.contains(gc))
                            goodGrandChild = false;
                    }

                    if(grandChildSet.size()==0 || goodGrandChild){
                        bw.write(tempPath+System.getProperty("line.separator"));
                        bw.flush();
                    }
                }
            }
            //End iteration of children
        }
        catch (Exception e) {
            // TODO: handle exception
        }
        finally{
        }

    }//End of function

2 回答

  • 0

    您使用 line.separator ,即 system dependent .

    • 对于Windows来说是 \r\n .

    • 对于Mac来说是 \r .

    还(位相关),

    • 对于OSX和Linux,它是 \n

    所以,你得到不同的输出 .

  • 0

    定义“分隔符”的另一种解决方案是

    File.separator

    例:

    // Removing directories 
    int lastDirectory = fileName.lastIndexOf(File.separator);
    fileName = fileName.substring(lastDirectory+1);
    

相关问题