-
file pathname
Hi,
I am using netbeans and have created a form in JavaSwing where I could browse a file and its pathname is displayed in a text field.
The code used for creating that event is as follows.
Code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
// What to do with the file, e.g. display path of the file in a Textfield
jTextField1.setText(fileChooser.getSelectedFile().getAbsolutePath());
}
}
Now, I need to use this pathname in the 'jTextField1' in another class and it should be displayed for eg:"C:\\chinese.txt" instead of C:\chinese.txt as it is given in the jTextField.
Could someone clarify it please?
Thanks and Regards
-
Plug in this line of code and execute it: Code:
System.out.println("C:\\chinese.txt");
The double backslash is only needed in String literals, because the backslash is the escape character.
Go through this: Lexical Structure
db
-
No my question is actually, How can i use this pathname in the 'jTextField1' which is in the form of C:\chinese.txt and convert it into C:\\chinese.txt so that I can use it in the program. and this file pathname changes everytime, as I choose a different file.
Thanks & Regards
-
Why do you need to display 2 backslashes instead of one? If you plan on actually using the path to create a File object then as Daryl mentioned the double backslash is only needed in String literals in your code to get around the compiler. If you are reading the path in from some external source then the double backslash is not needed.