Java如何提示用户保存单词doc

时间:2014-04-14 16:56:51

标签: java ms-word save command-prompt

我找到了这个here的VB和C#版本,但我还没能用Java找到它。 我有一个字符串,我想提示用户保存他们的word文档,让他们决定在哪里。我已经使用以下方式打开了文件:

    public static void sendCommand(String command){
    try {  
        Process p = Runtime.getRuntime().exec("cmd /C " + command);  
        BufferedReader input = new BufferedReader(  
            new InputStreamReader(p.getInputStream()));  
            String line = null;  
            while ((line = input.readLine()) != null) {  
                System.out.println(line);  
                }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }}
    public static void unzipOpenToSave(String zipName3){
        command = "cmd.exe /c move \"" + zipName3 + ".docx.zip\"  \"" + zipName3 + ".docx\"";
        sendCommand(command);

        //here we need to open the document and prompt to save as the name they just created
        String unzippedPath = wholePath.substring(0,wholePath.length() - 4);
        command = "rundll32 url.dll, FileProtocolHandler " + unzippedPath;
        sendCommand(command);
    }

我在本网站的两个不同页面中找到了代码的想法。所以,接下来我需要打开一个提示,用它们键入名称的字符串保存。我想要么使用命令提示符使用相同的方法,要么只使用Java。有没有人有任何想法?谢谢!

2 个答案:

答案 0 :(得分:1)

这不是太糟糕。我创建了一个框架,将filechooser添加到框架中,修改了“批准按钮”所说的内容,设置模式以便显示我想要的名称,并设置按下按钮时的操作(实际保存或重写文件) 。这是有效的:

JFrame saveFrame = new JFrame();
    saveFrame.setVisible(true);
    String unzippedPath = fullPath.substring(0,fullPath.length() - 4);
    JFileChooser jfc = new JFileChooser();
    jfc.setCurrentDirectory(new File(unzippedPath));
    jfc.setSelectedFile(new File(newSavedFile4));
    jfc.setApproveButtonText("Save");

    jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);

    int result = jfc.showSaveDialog(null);
    if(result == JFileChooser.APPROVE_OPTION){
        in = new File(unzippedPath);
        out = new File(jfc.getSelectedFile().toString() + ".docx");
        System.out.println(out.getAbsolutePath());
        int BUF_SIZE = (int) in.length();

        FileInputStream fiss = new FileInputStream(in);
        FileOutputStream foss = new FileOutputStream(out);
        try{
            byte[]buf = new byte[BUF_SIZE];
            int i = 0;
            while((i = fiss.read(buf)) != -1){
                foss.write(buf, 0, i);
            }
        }
        catch(Exception e){
            throw e;
        }
        finally{
            if(fiss != null) fiss.close();
            if(foss != null) foss.close();
        }
        saveFrame.setVisible(false);
    }
    else if(result == JFileChooser.CANCEL_OPTION){
        saveFrame.setVisible(false);
    }

    JPanel SPanel = new JPanel();
    SPanel.setLayout(new FlowLayout());
    SPanel.add(jfc);



    saveFrame.setLayout(new FlowLayout());
    saveFrame.add(SPanel);
    saveFrame.pack();
    saveFrame.setTitle("Save your Doc");
    saveFrame.setLocationRelativeTo(null);

谢谢Luke D!如果可以的话+1(我认为我还没有足够的分数) (我是原始海报,不记得我的密码,抱歉)

答案 1 :(得分:0)

假设您想以图形方式执行此操作,您可能会想要使用Swing。 JFileChooser是一个不错的选择。

http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html

此外,here就是一个很好的例子。

相关问题