从JFileChooser中得到选择的目录,比如c:\temp\java,因为创建File目录在window环境下,需要变成c:\\temp\\java,怎么办?想了想没有太好 JFileChooser.getSelectedFile().toString(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 返回绝对路径用String path = JFileChooser.getSelectedFile().getAbsolutePath();String path = JFileChooser.getSelectedFile().getCanonicalPath();//建议使用 String filename=txt1.getText()String str=""JFileChooser filechooser=new JFileChooser()Object obj=filechooser.getCurrentDirectory()str=obj.toString()String downfilename=str+"\\"+filename String str=c:\temp\java我想是不是可以将"\"替换成"/",这样应该可以的.试试看. System.getProperty("file.separator");check the file.separator then use! 用StringTokenizer将\转换成为\\ import javax.swing.*;import java.awt.event.*;import javax.swing.filechooser.*;import java.io.*;import java.util.*;public class PathTest extends JFrameimplements ActionListener{ public PathTest(){ setSize(300,300); JPanel jp = new JPanel(); JButton jb = new JButton("Click"); jp.add(jb); jb.addActionListener(this); getContentPane().add(jp); } public void actionPerformed(ActionEvent e){ JFileChooser jfc = new JFileChooser(); int i = jfc.showSaveDialog(this); if(i==JFileChooser.APPROVE_OPTION) try{ String path = jfc.getSelectedFile().getCanonicalPath(); String perpath = makePathUnderWindows(path); System.out.println(path); System.out.println(perpath); File file = new File(perpath); file.createNewFile(); } catch(Exception evt){} } public static void main(String[] args){ new PathTest().show(); } //转化路径 public String makePathUnderWindows(String path){ StringTokenizer sk = new StringTokenizer(path,"\\"); StringBuffer sb = new StringBuffer(""); if(sk.hasMoreTokens())sb.append(sk.nextToken()); while(sk.hasMoreTokens()){ sb.append("\\\\"); sb.append(sk.nextToken()); } return sb.toString(); }} 迅雷-面试题(20100927) 相当重要的问题 高手在哪儿呢? 请进 Java------程序人生----路漫漫其修远兮 我是刚刚开始学JAVA的,很菜,但是遇到几个题目,能否有人帮我做一下?谢谢了。 关于窗体主界面的设计思路求教 简单的JAVA分析提!请大家帮忙! java如何连接ORACLE数据库 JCreator里的函数提示功能如何打开? 关于package的两个问题? 用smartupload将文件上传到数据库时候碰到问题,请帮助我 jdbc的小问题?大力散分了。 IE5.0不支持applet吗?必须用java plug-in插件才可看到统计图吗?
String path = JFileChooser.getSelectedFile().getAbsolutePath();
String path = JFileChooser.getSelectedFile().getCanonicalPath();//建议使用
String str=""
JFileChooser filechooser=new JFileChooser()
Object obj=filechooser.getCurrentDirectory()
str=obj.toString()
String downfilename=str+"\\"+filename
我想是不是可以将"\"替换成"/",这样应该可以的.试试看.
check the file.separator then use!
import java.awt.event.*;
import javax.swing.filechooser.*;
import java.io.*;
import java.util.*;public class PathTest extends JFrame
implements ActionListener
{
public PathTest(){
setSize(300,300);
JPanel jp = new JPanel();
JButton jb = new JButton("Click");
jp.add(jb);
jb.addActionListener(this);
getContentPane().add(jp);
}
public void actionPerformed(ActionEvent e){
JFileChooser jfc = new JFileChooser();
int i = jfc.showSaveDialog(this);
if(i==JFileChooser.APPROVE_OPTION)
try{
String path = jfc.getSelectedFile().getCanonicalPath();
String perpath = makePathUnderWindows(path);
System.out.println(path);
System.out.println(perpath);
File file = new File(perpath);
file.createNewFile();
}
catch(Exception evt){}
}
public static void main(String[] args){
new PathTest().show();
}
//转化路径
public String makePathUnderWindows(String path){
StringTokenizer sk = new StringTokenizer(path,"\\");
StringBuffer sb = new StringBuffer("");
if(sk.hasMoreTokens())sb.append(sk.nextToken());
while(sk.hasMoreTokens()){
sb.append("\\\\");
sb.append(sk.nextToken());
}
return sb.toString();
}
}