简体   繁体   中英

How to write a file and read that file again into temporary file

edit "second version"

I have building an Java swing using ANTLR to get the tree of source code. For get the tree image that build from ANTLR, I am using GRAPHVIZ, http://www.graphviz.org/ .

So, Graphviz will be write to a file that have extension .dot and then I will load it into my Swing application. My code is like this

String OS = System.getProperty("os.name");
        Runtime rt = Runtime.getRuntime();
        BufferedWriter bw = null;
        File tempDir = new File(System.getProperty("java.io.tmpdir"));

        File tempInput;
        try {
            tempInput = File.createTempFile("output", ".dot", tempDir);
            File tempOutput = File.createTempFile("tree", ".png", tempDir);

            if (OS.equals("Linux")) {

                try {

                    bw = new BufferedWriter(new FileWriter(tempInput.getAbsoluteFile()));
                    bw.write(st2);
                    bw.close();

                    Process pr = rt.exec("dot -Tpng  /tmp/output.dot -o "
                            + "/tmp/tree.png");

                } catch (IOException ex) {
                    System.out.println("failed to write the image file");
                }

            } else {

                try {

                    bw = new BufferedWriter(new FileWriter(tempInput.getAbsoluteFile()));
                    bw.write(st2);
                    bw.close();

                    String dotPath = "C:\\Program Files (x86)\\Graphviz2.38\\bin\\dot.exe";
                    String fileInputPath = tempInput.toString();
                    String fileOutputPath = tempOutput.toString();
                    String tParam = "-Tpng";
                    String tOParam = "-o";

                    String[] cmd = new String[5];
                    cmd[0] = dotPath;
                    cmd[1] = tParam;
                    cmd[2] = fileInputPath;
                    cmd[3] = tOParam;
                    cmd[4] = fileOutputPath;

                    rt.exec(cmd);

                } catch (IOException ex) {
                    System.out.println("Failed to write to file");
                } finally {

                }
            }
        } catch (IOException ex) {
            Logger.getLogger(MainAlgoritma.class.getName()).log(Level.SEVERE, null, ex);
        }

you know, the output in windows should be named tree.png. But windows not gives me that name. The name is dynamically change like tree2593490478729479216.png and sometimes like tree9133268802668231475.png and etc .

My question is :

  1. how to get the name just tree.png ?
  2. how to delete the image after process after the image is read and loaded to app ?

    edit

Now, how to read that image and then load it into app... ? I make a class again to load the image, but you know, I am still confused.

private BufferedImage image;
String tmpfolder = System.getProperty("java.io.tmpdir");

public FileImage() {

    if (OS.equals("Linux")) {
        try {
            image = ImageIO.read(new File(tmpfolder+"/tree.png"));
        } catch (IOException ex) {
            System.out.println("Image failed to load..!!! ");
        }
    } else {

        try {
            image = ImageIO.read(new File(tmpfolder+"\\tree.png"));
        } catch (IOException ex) {
            System.out.println("Image failed to load...!!! ");
        }
    }

    JLabel jLabel = new JLabel(new ImageIcon(image));
    jPanel3.add(jLabel);

}

您可以使用相对路径或在Swing中使用JFileChooser来打开文件选择器,而不是对绝对路径进行硬编码,从而使用户可以选择文件进行输入和/或输出。

how to get the name just tree.png

You should read the javadoc of the createTempFile method you are using. It clearly states that the strings you pass in as parameters are a prefix and suffix.

If you replace

File.createTempFile("output", ".dot", tempDir);

by

tempInput = new File( tempDir, "output.dot" );
tempInput.createNewFile();

it should work.

Note how I used the File constructor taking two arguments. This avoids the ugly switch statement on the OS you are using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM