• Post Reply Bookmark Topic Watch Topic
  • New Topic

File Path handling on Button event  RSS feed

 
Manu Verma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have use of file path in my application, I have used jFileChooser for getting a folder path, I need folder path not file.
but it is giving path like D:\Eclipse Work\Framework\src, whether "\" is invalid character in Java.
Wndows system use "\\" and Unix uses "/".
I have also used replace('char1', 'char2') method to change the path denotion but it gives error when I use "\".
Please Guide me with suitable solution.

I am using this event code: -

public void OnmouseClickBrowse(java.awt.event.MouseEvent evt) {
String filename = File.separator+"tmp";

JFileChooser fileChooser = new JFileChooser(filename);
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

// Show open dialog; this method does not return until the dialog is closed
fileChooser.showOpenDialog(fileChooser);
File selFile = fileChooser.getSelectedFile();

// Show save dialog; this method does not return until the dialog is closed
fileChooser.showSaveDialog(fileChooser);
selFile = fileChooser.getSelectedFile();
String srcpath = selFile.getPath();
jTextField1.setText(srcpath);

//below line gives error that "\" character in invalid.
//srcpath = srcpath.replace('\','.');
}

Also I want solution to make a UI table/Grid dynamic, means it change the number of rows as per the requirement.
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manu Verma:
... whether "\" is invalid character in Java.

//below line gives error that "\" character in invalid.
//srcpath = srcpath.replace('\','.');


The backslash is not an invalid character in Java, but it does have a special role in character literals and string literals.

'a' is a character literal for: a
'/' is a character literal for: /
'\'' is a character literal for: ' (note: It ends with two single-quotes, not a double-quote.)
'\\' is a character literal for: \

Your code contains '\' which is a syntax error, not a character literal. Try srcpath.replace('\\', '.') instead.

[edit: Actually, I'd probably go with srcpath.replace(File.separatorChar, '.').]
[ October 25, 2007: Message edited by: Brian Cole ]
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wndows system use "\\" and Unix uses "/"


You can use slash '/' in windows. Microsoft allows it, and Java definitely knows how to handle it.

If you don't believe me, go to your computer properties, change the Path environment variable value to all use '/'. Everything will be OK.

If you have old-old windows, or in the cmd(dos) window, then '/' will not work.
 
Manu Verma
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Brian for concept and explanation, I have used srcpath.replace(File.separatorChar, '.'); and it is working fine.

Thanks to Roseanne Zhang also, you are very true.

I have another problem in same application

I have passed file path(srcpath) in classInfo() method and want to call method listFiles() if given path is a valid folder: -

public static String[] classInfo(String src) {
String[] methName = null;
File file = new File(src);
if (file.isDirectory()) {
listFiles(file);
} else {
array.add(file.getName());
}

But it gives error while creating file Object of File. Any help regarding this? I just want to check whether given path is a folder path or not.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!