你加上双引号没有必要,系统反而以为引号是路径的一部分。
直接用path构造输出流:FileOutputStream fos=new FileOutputStream(path)
使用保存文件对话框:\x0d\x0a\x0d\x0a /**\x0d\x0a * 保存\x0d\x0a */\x0d\x0aprivate void saveFile(){\x0d\x0aJFileChooser dialog = new JFileChooser()\x0d\x0adialog.setDialogTitle("另存为")\x0d\x0adialog.setFileSelectionMode(JFileChooser.FILES_ONLY)\x0d\x0adialog.setDialogType(JFileChooser.SAVE_DIALOG)\x0d\x0adialog.setFileFilter(new TextFileFilter("*.txt", "文本文档(*.txt)"))\x0d\x0aint result = dialog.showSaveDialog(this)\x0d\x0aif(result == JFileChooser.APPROVE_OPTION){\x0d\x0aFile file = dialog.getSelectedFile()\x0d\x0afileName = file.getAbsolutePath()//得到文件全名\x0d\x0a...\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a附文本类型过滤器:\x0d\x0aimport java.io.File\x0d\x0aimport java.util.ArrayList\x0d\x0aimport javax.swing.filechooser.FileFilter\x0d\x0a\x0d\x0a/**\x0d\x0a * 设置文件打开对话框的文件过滤器\x0d\x0a * @author developer\x0d\x0a */\x0d\x0apublic class TextFileFilter extends FileFilter {\x0d\x0aprivate ArrayList extensions = new ArrayList()\x0d\x0aprivate ArrayList descriptions = new ArrayList()\x0d\x0a\x0d\x0apublic TextFileFilter(){\x0d\x0asuper()\x0d\x0a}\x0d\x0a\x0d\x0apublic TextFileFilter(String extension, String description) {\x0d\x0asuper()\x0d\x0athis.extensions.add(extension)\x0d\x0athis.descriptions.add(description)\x0d\x0a}\x0d\x0a\x0d\x0a@Override\x0d\x0apublic boolean accept(File pathname) {\x0d\x0aif (pathname != null) {\x0d\x0aif (pathname.isDirectory()) {\x0d\x0areturn true\x0d\x0a}\x0d\x0aString extension = getExtension(pathname)\x0d\x0afor(int i=0i使用javaswing中的JFileChooser类就ok了\x0d\x0aimport javax.swing.JFileChooser\x0d\x0aimport javax.swing.JPanel\x0d\x0aimport javax.swing.filechooser.FileNameExtensionFilter\x0d\x0a\x0d\x0apublic class WebJianXuanZe {\x0d\x0a\x0d\x0apublic static void fileChooser() {\x0d\x0aJFileChooser chooser = new JFileChooser()\x0d\x0aFileNameExtensionFilter filter = new FileNameExtensionFilter(\x0d\x0a"JPG &GIF Images", "jpg", "gif")\x0d\x0a//设置文件类型\x0d\x0achooser.setFileFilter(filter)\x0d\x0a//打开选择器面板\x0d\x0aint returnVal = chooser.showOpenDialog(new JPanel()) \x0d\x0a //保存文件从这里入手,输出的是文件名\x0d\x0aif(returnVal == JFileChooser.APPROVE_OPTION) {\x0d\x0a System.out.println("你打开的文件是: " +\x0d\x0achooser.getSelectedFile().getName())\x0d\x0a}\x0d\x0a}\x0d\x0apublic static void main(String[] args) {\x0d\x0afileChooser()\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a建议你可以翻java API 。新童鞋的话一定要学会用API、善于用API,尽量独立解决问题,这样能够加深映像,并且对子什么也是一个激励!~欢迎分享,转载请注明来源:内存溢出
评论列表(0条)