rohan
rohan

Reputation: 557

Different output is produced as a result of Java program when executed from windows

I am running a Java program to compute all paths in a tree. When I run it form eclipse on windows as I see an output1, but when I run the same program from jar or Mac, I notice different output. There are lots of diffs. Even file sizes are different. Does buffer writer behave differently when depending on platform?

So, I have a same output when executed from the jar or executed on Mac Eclipse, but different output when executed from windows eclipse.

Here is the code thats writing to the file: Member Variable:

    public  HashMap<String, HashSet<String>> nodeListFromFile = new HashMap<String, HashSet<String>>();

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

Upvotes: 0

Views: 722

Answers (2)

AlejandroHernandez.Inc
AlejandroHernandez.Inc

Reputation: 153

Another solution to define the "separator" is by

File.separator

Example:

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

Upvotes: 0

dryairship
dryairship

Reputation: 6077

You use line.separator, which is system dependent.

  • It is \r\n for Windows.
  • It is \r for Mac.

Also (bit related),

  • It is \n for OSX and Linux

So, you get different outputs.

Upvotes: 0

Related Questions