gpt4 book ai didi

Java如何提示用户保存word文档

转载 作者:行者123 更新时间:2023-12-01 13:08:41 25 4
gpt4 key购买 nike

我找到了此 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。有人有什么想法吗?谢谢!

最佳答案

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

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);

谢谢卢克 D!如果可以的话+1(我认为我还没有足够的积分)(我是原发帖者,忘记密码了,抱歉)

关于Java如何提示用户保存word文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23065877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com