
正文
java另存为路径代码 java编程程序保存路径
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java写一段程序 实现对本地路径下一word文档打开并且另存为指定的格式的文件,请问能实现吗? 求实例代码
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class SaveFile {
public static void main(String[] args) {
String olddir = "d:" + File.separator + "test.docx";
String newdir = "e:" + File.separator + "newtest.tt";
saveFileToAnotherDir(olddir, newdir);
}
public static void saveFileToAnotherDir(String olddir, String newdir) {
File fo = new File(olddir);
File fn = new File(newdir);
FileReader fr;
FileWriter fw;
try {
fr = new FileReader(fo);
fw = new FileWriter(fn);
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw);
String line = null;
while((line = br.readLine()) != null) {
bw.write(line);
bw.flush();
}
bw.close();
br.close();
fw.close();
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
}
}
类似这样java另存为路径代码的可以实现
相关问答
Q1: java中实现另存为的代码
保存代码功能,可以参考下面的代码。
代码如下:
function runCode(obj) {
var winname = window.open('', "_blank", '');
winname.document.open('text/html', 'replace');
winname.opener = null // 防止代码对父页面修改
winname.document.write(obj.value);
winname.document.close();
}
function saveCode(obj) {
var winname = window.open('', '_blank', 'top=10000');
winname.document.open('text/html', 'replace');
winname.document.write(obj.value);
winname.document.execCommand('saveas','','');
winname.close();
}
function oCopy(obj){
obj.select();
js=obj.createTextRange();
js.execCommand("Copy");
alert("提示:代码已经被成功复制!");
}
Q2: java 保存文件路径的问题
public class Test {
public static void main(String[] args) {
Test t = new Test();
t.SystemIn();
}
public void SystemIn() {
// 写入文件路径
String path = null;
// 读入输入内容
BufferedReader read =
new BufferedReader(new InputStreamReader(System.in));
try {
// 保存输入内容
StringBuffer value = new StringBuffer();
// 读入一行内容
String context = read.readLine();
while (context != null) {
if (context.equals("保存")) break;
value.append(context + "\n");
read = new BufferedReader(new InputStreamReader(System.in));
context = read.readLine();
}
System.out.println("请输入地址:");
while(true) {
// 读入路径
read = new BufferedReader(new InputStreamReader(System.in));
if (path == null) {
path = read.readLine();
File file = new File(path);
if (!file.exists()) {
System.out.println("文件不存在,请输入地址:");
path = null;
continue;
}
// 写入文件
PrintWriter print = new PrintWriter(new FileOutputStream(file));
print.write(value.toString());
print.close();
}
if (read.readLine().equals("quit")) {
System.exit(0);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Q3: Java:如何在前台显示"另存为"对话框,然后后台获取用户选择的路径呢?
java另存为路径代码你具体指java另存为路径代码的是JSP吧java另存为路径代码,其实关于另存对话框显示是浏览器自行决定的,即:一个url的跳转如果浏览器检测到这个url指向的是一个文件流那么就会显出另存为的对话框。具体的方式时前台一个 超链接a href="url",url指向后台的servlet(struts对应action,等其他的业务逻辑)。而后台的实现方式:把文件流输出到reponse的输出流中。具体代码为:
public static void downloadFile(HttpServletResponse response,InputStream in,String sFileName)
{
java.io.OutputStream toClient = null;
try
{
// 清空response
response.reset();
// 设置response的Header
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment;filename=" + new String(sFileName.getBytes("iso-8859-1")));
//response.addHeader("Content-Length", "" + file.length());
toClient = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[2 * 1024];
int iReadLen;
while(-1 != (iReadLen = in.read(buffer)))
{
toClient.write(buffer, 0, iReadLen);
}
toClient.flush();
} catch (Exception e)
{
logger.error(null, e);
}finally
{
IOUtils.closeQuietly(toClient);
}
}
Q4: java按下一个按钮弹出文件保存路径对话框
java代码如下:
response.addHeader( "Content-Disposition" , "attachment; filename=" + response.encodeURL(downloadfile));
备注:第一个值项是attachmentjava另存为路径代码,设定java另存为路径代码了这个值,浏览器就会显示另存为对话框,如果设成inline,则无论怎样浏览器都会自动打开文件。
java另存为路径代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java编程程序保存路径、java另存为路径代码的信息别忘了在本站进行查找喔。






