aspose file tools*
The moose likes Java in General and the fly likes how to include file-separator in fileName? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to include file-separator in fileName?" Watch "how to include file-separator in fileName?" New topic
Author

how to include file-separator in fileName?

honey singh
Ranch Hand

Joined: Apr 26, 2002
Posts: 44
I'm retrieving the file name from a function and that function returns me the string and now I can't change this.
String fileName="C:\jdk1.2\hello.java"
Now next with this file Name I'm making a file object
File f=new File(fileName);
FileInputStream in=new FileInputStream(f);
but this gives me error..that it doesn't found the specified the file given...
so this means I must give the separator as C:\\jdk1.2\hello.java
but I can't change the value returned to me by the function...
So this means that I have to make a function replace which converts '\' to '\\'
String newfileName=replace(fileName);
so please can anybody tell me how to write this funcion? Actually I can't use replace fn. of String class, as it says that '\' is invalid escape character...u try it and compile the result and tell me
thanx in advance for the favourable response
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Try read this post:
http://www.coderanch.com/t/392052/java/java/String-token
/Rene


Regards, Rene Larsen
Dropbox Invite
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I'm retrieving the file name from a function and that function returns me the string
What does the funtion look like? And what happens if you execute
System.out.println(fileName);
You may well find that the problem with the file name is not what you think it is.
It seems likely that you're confusing the escape character used in Java code (\) with actual characters in a String. They're different. If I write Java code:
String str = "\\";
that string has a length of one character. And if I print it, I will see
\
In the code you showed,
String fileName="C:\jdk1.2\hello.java"
does not even compile on my machine. That's because it thinks \j should be a special character (but it isn't). Likewise for \h. If this is the actual code you're using, try
String fileName="C:\\jdk1.2\\hello.java"
instead. Otherwise, go into the function you claim you're getting the file name from, and fix it there.
[ July 18, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
honey singh
Ranch Hand

Joined: Apr 26, 2002
Posts: 44
thanx very much for the reply, actually the thing is that the method which u told will work fine with some other characters...but for my case I can't even write like this
String a="C:\jdk\hello.java"
String b=replace(a,"\","\\");
as this will give me compiler error...
actually my function is extracting value from the form when it is submitted having the file field
so data is as follows
-----------------------------7d01ecf406a6
Content-Disposition: form-data; name="FileItem";
****filename="C:\Inetpub\wwwroot\Upload\file1.txt"***********
Content-Type: text/plain
This file has some text in it.
It also has more than one line.
-----------------------------7d01ecf406a6
Content-Disposition: form-data; name="fileaaa";
filename="C:\Inetpub\wwwroot\Upload\pic.gif"
Content-Type: image/gif

so this function is extracting value from here.. and there it is just simply without '\\'
so it will return me in '\' format...
but I need to convert into '\\' format...
please help me!
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Urgh...
You need to distinguish between what String literals look like in a Java file, and what the actual Strings look like.
Inside a .java file, an expression like "\" is utterly meaningless to the compiler. Because \" is not a legal escape character. If you need code that will replace every \ in a String with \\ (and I don't believe you really do), the code inside your .java file would be something like
String b=replace(a,"\\","\\\\");
If it helps to understand, run the following:
System.out.println("One: \\");
System.out.println("Two: \\\\");
What you see printed to the screen is
One: \
Two: \\
This represents the strings as they really are in memory. The "\\" and "\\\\" are only used in a .java file as part of a String literal representation.
Now, again, you need to run
System.out.println(fileName);
to find out what the file name looks like in memory. If, in memory, it looks like
C:\jdk1.2\hello.java
then there is nothing wrong with the \ symbol. This is what file separators are supposed to look like (at least on Windows, which seems to be the case.) You're not inside a .java file anymore; you don't need the \\ escape sequences at this point. If your system isn't finding the file, something else is the problem. Are you sure the file really exists? And that the path is correct? Try this: replace

with

Run it and read the output carefully. Look to see if the file path is correct or not. If you still don't understand, report exactly what the output of the program is. (This section anyway.)
saager mhatre
Ranch Hand

Joined: Dec 20, 2000
Posts: 61
Praanji! Possibly the simplest means to solve the problem is to avoid using the backslash('\'). Simply use the frontslash('/') and let the File Class handle the file-seperator conversion! It's designed to do that!!
:roll:
 
Consider Paul's rocket mass heater.
 
subject: how to include file-separator in fileName?