如何在java中使用forloop将这样的输出转换为文本文件?

如何在java中使用forloop将这样的输出转换为文本文件?,java,string,for-loop,filewriter,Java,String,For Loop,Filewriter,我必须在java中获得这样的输出 digraph G { main -> parse -> execute; main -> init; main -> cleanup; execute -> make_string; execute -> printf init -> make_string; main -> printf; execute ->

我必须在java中获得这样的输出

     digraph G 
   {
     main -> parse -> execute;
     main -> init;
     main -> cleanup;
     execute -> make_string;
     execute -> printf
     init -> make_string;
     main -> printf;
     execute -> compare;
     }
我试过这个

PrintStream out = new PrintStream(new FileOutputStream("graph.gv"));

         for(int i=0;i<1;i++)

    {
        System.out.println("digraph G {");

    for (String node : visited)

    {

        System.out.print(node);
        System.out.print("->");
        out.write(node);
        out.flush();

    }
   out.close();

    System.out.println();
    }
      System.out.println("}");
}

但是输出未正确打印

将此:
System.out.println(“有向图G{”);
从for循环中删除。也就是说,更改此:

for(int i=0;i<1;i++)
{
   System.out.println("digraph G {");
   //Your remaining code
}
对于(int i=0;i使用以下方法:

System.out.println("digraph G {");   //  To print it once
for(int i=0;i<1;i++)
{
    for(int j=0; j<visited.length()-1; j++)  // Accessing one less node to avoid extra "->" at the end
    for (String node : visited)
    {
        System.out.print(node);
        System.out.print("->");
        out.write(node);
        out.flush();
    }
    System.out.print(node);  //Printing the last node left
    out.flush();
    out.close();

    System.out.println();
}
System.out.println("}");
System.out.println(“有向图G{”);//打印一次

对于(int i=0;i使用System.out.format()方法,而不是System.out.println()

如果要以某种方式格式化输出,可以使用format()


.

您现在得到了什么输出?@PradeepSimha我编辑了问题并粘贴了输出。您可以将相关的增强forloop发布到传统forloop吗?我尝试了您的代码,但仍然存在问题persists@MuralidharanT,您能否编辑更新的代码和输出?
System.out.println("digraph G {"); //Notice change here
for(int i=0;i<1;i++)
{
   //Your remaining code
}
System.out.println("digraph G {");   //  To print it once
for(int i=0;i<1;i++)
{
    for(int j=0; j<visited.length()-1; j++)  // Accessing one less node to avoid extra "->" at the end
    for (String node : visited)
    {
        System.out.print(node);
        System.out.print("->");
        out.write(node);
        out.flush();
    }
    System.out.print(node);  //Printing the last node left
    out.flush();
    out.close();

    System.out.println();
}
System.out.println("}");