2

The following code is from a method in a class that I am making to modify a list of file directories in a program folder. However I am trying to use "\" as a delimiter for a scanner as I only need the start of the directory "S:\" and the last part which is just name of a sub folder. So for example it looks like this:

F:\Data\Subfolder\Another

The code complies but when I run the method i get this following run time error:

java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 \

And was just wondering if anyone knows what it means and how I can stop it from happening. Is it because of using the \ for a delimeter?

Note: newFolder class is a nested class

public void scanFiles() throws IOException{

       try
       {
        System.out.println("Sage 2015 is Installed on this machine");
        File companyFile = new File(sageFolders[8] + "\\COMPANY");
        Scanner input = new Scanner(new BufferedReader(new FileReader(companyFile)));
        input.useDelimiter("\\");


        while(input.hasNextLine())
        {
           if(line.contains("F"))
           {
               String drive = input.next();
               String dataFolder = input.next();
               String sageFolder = input.next();
               String clientFolder = input.next();

               newFolders.add(new newFolder(drive, clientFolder));


           }

        }
        //Close the Readers
        fileReader.close();
        bufferedReader.close();

        //fileWriter = new FileWriter(companyFile);
        //bufferedWriter = new BufferedWriter(fileWriter);

           //Write back to file

        //fileWriter.flush();
        //bufferedWriter.close();

       }
       catch(FileNotFoundException e)
       {
          System.out.println("File not Found: Moving onto next Version"); 
       }

}

class newFolder
{
    private String driveLetter;
    private String clientFolder;

    public newFolder(String driveLetter, String clientFolder)
    {
        this.driveLetter = driveLetter;
        this.clientFolder = clientFolder;
    }
}
Baker3915
  • 37
  • 1
  • 2
  • 8

1 Answers1

4

Since \ is regex special character and also special in Java, you have to escape your java backslash with \\ and also your regex backslash with \\, hence... you have four backslashes \\\\

You have to use:

input.useDelimiter("\\\\");
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123