Results 1 to 5 of 5
  1. #1
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default Backslash, File directories, and streams

    I dont really know where i should post this, so im just posting it here.

    I have a program, and part of it is to read a file, find a string, modify the string, and write it back to the file. The string in question is a file directory on windows, so it has backslashes (\).

    The originial string from the file:

    "string filename" ["C:\\My\\Fike\\Path\\toChange\\ta-00042"]

    I read the string using

    Java Code:
    FileInputStream fis = new FileInputStream((test));
                            BufferedReader is = new BufferedReader(new InputStreamReader(fis));
    And read all the lines of the code, split it using
    Java Code:
    String[] lines = file.split("\n");
    I need to change the String with a new filepath, and written out with 2 backslashes, like the originial. I make a new string, and use a string containing the directory. The directory was taken from a JTextField, so only 1 slash.

    How can i replace all these '\' with '\\'? Do i need to add more to have the FileWriter print the correct amount of literals?

    When the FileInputStream reads the file, does it do anything to the backslashes?

    I have tried
    Java Code:
    String.replaceAll("\\","\\\\");
    but i am getting the same output as input with System.out.println();

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    just use "/" instead of backslash.
    My Hobby Project: LegacyClone

  3. #3
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    that wouldnt work in windows, would it? Unix uses / and windows uses \

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rp181 View Post
    that wouldnt work in windows, would it? Unix uses / and windows uses \
    Have you tried it?

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Windows accepts both / and \
    I die a little on the inside...
    Every time I get shot.

Similar Threads

  1. How to handle \ (backslash) in regular expressions
    By federal102 in forum New To Java
    Replies: 1
    Last Post: 11-02-2008, 02:33 AM
  2. Replies: 0
    Last Post: 08-30-2008, 12:52 PM
  3. Directories
    By KevMeistr in forum New To Java
    Replies: 6
    Last Post: 06-11-2008, 05:03 PM
  4. getting paths and directories
    By marco in forum Java Applets
    Replies: 3
    Last Post: 11-25-2007, 08:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •