• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help fixing file directory  RSS feed

 
Nico Fish
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Im struggling with the above. I am trying to sort through some textfiles (files contain RNA nucleotides for those curious) and I am catching an error that results from the file directories having

"\" instead of "/" (When I try to use the directories stored in the tempStringBuff array).

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)

I read the above for replaceAll() and it says that I need to use Matcher.replaceAll() in order to suppress the special meaning of the "\" character


My question is how can I implement Matcher.replaceAll();

I have been looking around and I cant seem to find the answer.

ideally I would do the following


Thank you

note: I made an account a while ago, but I stopped coding for a few years (just got back to it today) so I am not sure if my question is properly formatted.


 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome (back) to the ranch!
Congratulations on using code tags to format your code.
Unfortunately the code posted isn't compiling/working - there are some fundamental flaws in it, so lets start with fixing those first shall we?

What is this variable/class FileDirectory?
Where do its values come from?
I'm presuming it is an array of file names that you want to read because thats how it is treated, but thats just a guess on my part.
Oh and variable names should start with a lower case letter according to standard java programming conventions. It makes it a LOT easier for other people to read your code if you follow them.

You refer to tempStringBuff as an array in your post. It isn't an array - it's declared as a BufferedReader. But then you try and use it as as input to a matcher which accepts neither arrays or Readers.

You have a string "\" in your code.
Backslash is a special escape character in a String. To indicate \ in a string, you need "\\"
Backslash also has a special meaning in regular expressions. To actually match a backslash in a pattern, your pattern needs to specify two backslashes.
To specify two backslashes in a string, you need to escape both of them.
So to get a pattern that looks for the '\' character you end up with Pattern.compile("\\\\")
Crazy enough for ya?

Now, I'm not convinced you actually NEED to do pattern matching on backslashes. It depends what this FileDirectory thing is, and how you get it. If it IS as I suspect a String[] you got from a call to the File.list() method, I would recommend you take a look at the File.listFiles() method and avoid the whole problem :-)
You need to stop and think about what you are trying to accomplish here.
Maybe start with something small that just prints out all the file names and check if they exist or not?

Oh one more thing. Check out the class java.nio.file.Files - particularly the readAllLines method.

 
Nico Fish
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome (back) to the ranch!
Congratulations on using code tags to format your code.
Unfortunately the code posted isn't compiling/working - there are some fundamental flaws in it, so lets start with fixing those first shall we?

What is this variable/class FileDirectory?
Where do its values come from?
I'm presuming it is an array of file names that you want to read because thats how it is treated, but thats just a guess on my part.
Oh and variable names should start with a lower case letter according to standard java programming conventions. It makes it a LOT easier for other people to read your code if you follow them.

You refer to tempStringBuff as an array in your post. It isn't an array - it's declared as a BufferedReader. But then you try and use it as as input to a matcher which accepts neither arrays or Readers.

You have a string "\" in your code.
Backslash is a special escape character in a String. To indicate \ in a string, you need "\\"
Backslash also has a special meaning in regular expressions. To actually match a backslash in a pattern, your pattern needs to specify two backslashes.
To specify two backslashes in a string, you need to escape both of them.
So to get a pattern that looks for the '\' character you end up with Pattern.compile("\\\\")
Crazy enough for ya?

Now, I'm not convinced you actually NEED to do pattern matching on backslashes. It depends what this FileDirectory thing is, and how you get it. If it IS as I suspect a String[] you got from a call to the File.list() method, I would recommend you take a look at the File.listFiles() method and avoid the whole problem :-)
You need to stop and think about what you are trying to accomplish here.
Maybe start with something small that just prints out all the file names and check if they exist or not?

Oh one more thing. Check out the class java.nio.file.Files - particularly the readAllLines method.



Thank you for your quick reply.

you are right tempStringBuff is not an array I got confused cause I had been changing things up prior to writing my message.

I had the code compile prior to writing the message, but I broke it (just this method) trying to fix the file directory name thing.

Perhaps I should explain my ordeal in more detail.

I have a bunch of files that have a mixture of RNA and COI DNA data mixed together (somebody screwed up). I have to sort out the strings that are RNA by finding ultra-conserved regions.

So I want to read the nucleotides out the fasta files (I'm not sure if that will be a problem).

I will post all of the code I have:




Here is the error message I am catching:
run:
"
2 // number of files
C:\Users\NicoFish\Documents\Kjer lab\Ribosomal coding project\taxa\Acanthocasuarina_muellerianae_Psyloidea\.DS_Store // here is the problem there are back slashes in the directory as opposed to forwards slashes****
read failure: java.io.FileNotFoundException: C:\Users\NicoFish\Documents\Kjer lab\Ribosomal coding project\taxa\Acanthocasuarina_muellerianae_Psyloidea\result (Access is denied)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds) "



All I want to do is replace the back slashes with forwards slashes so that I can save the strings to FileDArr



Thanks again for all the help. I was hacking at this for longer than I would like to admit. It is amazing how much you can forget after not having coded at all for a few years (its coming back to me though)





 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not begin variable names with an upper case character, e.g. FileDirectory should be fileDirectory.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are setting the fileDArr size to the length of the list of directories, then you are filling fileDArry with a line read in from the file. There's no reason to assume that fileDArr and fileDirectory should be the same length. If you really want to read in the whole file then replace fileDArr with an ArrayList<String>.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tempStringBuff is never closed.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not clear on the intended use of fileDArr. Are you filling it with the first line of every file?
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure how to read this. If you're just trying to work around "\" in a set of file pathnames, that's trivial:



Java will automatically handle pathnames with either "\" or "/" as directory separator characters. You can even get the simple file name:


Or build a file in a directory.


And, if you'll notice, never need to care about which way the slashes point. This is actually good practice also in that it makes code more OS-independent (write-once/run-anywhere).

Now on the other hand, if what you're attempting to do is simply read lines of text in from a file and handle backslashes in the text itself, that's a more challenging question since there's no single standard on what may or may not be backslash-escaped and how.

For example: "\r" - translates to carriage-return (ASCII code 13). "\010" translates to a backspace control character (control-h). "\u0010" translates to a single unicode character. And, of course "\\" translates to a single "\". But only in cases where those particular interpretations are accepted.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At this point we are trying to reverse engineer your needs. Could you explain whether it is a file naming problem or a file content problem or both? Could you include a couple of lines in the file that are examples of what you're trying to search for?

Note that File.listFiles() returns both files and directories so you may want to check that what you are iterating though is an actual file: File.isFile().
 
Nico Fish
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all the responses. As it turns out I had used the wrong file path to begin with which is why I was getting an error. Sorry for wasting your time.....
 
Michael D Sims
Ranch Hand
Posts: 113
1
IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nico Fish wrote:Thank you for all the responses. As it turns out I had used the wrong file path to begin with which is why I was getting an error. Sorry for wasting your time.....

I was about to say, the specific error you were receiving, "Access is denied" given that the path looked properly formatted to me, would indicate an ACL (permissions / security) issue with the file or folder itself. I'm not sure if Java throws the same error if you point it to something that doesn't exist, but based on what you just said, that is exactly what it does.

Incidentally, I just finished a rather complex program that takes what is essentially garbage text that was spit out to a text file from a database using the most disgusting SQL report techniques that I have ever seen. The code then formats it into nicely presented strings that can then be imported into a database. I literally completed the roughly 25 RegEx search and replace / massage function an hour ago. I have lived and breathed Regex patterns and matchers and everything in between and even outside the margins for weeks now ... if you could use any assistance in your project, ask me anything however trivial it might seem. Maybe I can help you hone your learning curve down to a linear graph more quickly than you could do on your own, since it seems like whatever you're doing has a sense of urgency to it. And I happen to have some extra time on my hands this weekend.

Mike

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!