First of all you can not have a string as you posted in question

String fname="C:\textfiles\db\query\query.txt";

this should be replaced by

String fname="C:\\textfiles\\db\\query\\query.txt";

as backslash("\") needs an escape as well.

Finally you need to do something like this to split them:

 String fname="C:\\textfiles\\db\\query\\query.txt";
 String[] items= fname.split("\\\\");
 System.out.println(Arrays.toString(items));

Hope this helps.

Answer from Sanjeev on Stack Overflow
🌐
Justknowitdoit
justknowitdoit.com › home › java › how to split a string by backslash in java
How to split a string by backslash in Java - Just Know IT Just Do IT
February 19, 2019 - So, use the split function of String and escape the backslash twice. The result of the split function will be an array of String which you can reference further in for example a loop. Have fun with Java coding!
🌐
Videlin
videlin.eu › 2015 › 10 › 14 › how-to-split-by-forward-orand-back-slash-in-java-using-regex
How to split by forward or/and back slash in Java using RegEx – VDonC#ev
October 14, 2015 - public class MatchBackslashRegex { public static void main(String[] args) { String string = "Element1\\Element2"; String[] strArr = string.split("/\\\\"); System.out.println(strArr[0]); // returns Element1 } }
🌐
Mkyong
mkyong.com › home › java › how to split a string in java
How to split a string in Java - Mkyong.com
February 9, 2022 - 4.3 Split a string by a backslash "" 4.4 Split a string by a period or dot "." 5. Split a string by multiple delimiters · 6. Split a string and Java 8 splitAsStream · 7. Split a string by a space or multiple spaces · 8. Split a string by new lines · 9. Split a string by StringTokenizer (legacy) 10.
🌐
Stack Abuse
stackabuse.com › how-to-split-a-string-in-java
How to Split a String in Java
September 20, 2023 - If we want to split a String at ... parameters. One way we can use this is to use a backslash \. For example: ... Splits the string variable at the | character. We use two backlashes here since we need to first escape the Java-meaning of the backlash, so the backslash can be ...
🌐
YouTube
youtube.com › watch
How to Split Strings in Java Using Backslash as a Separator - YouTube
September 1, 2025 - Discover how to efficiently split strings in Java using a `backslash` as a separator. Learn the right method to retain all tokens, even empty ones!---This vi...
Find elsewhere
🌐
Squash
squash.io › how-to-split-a-string-in-java
How To Split A String In Java - Squash Labs
In this example, we split the string ... backslash (\). However, since backslash is also a special character in Java strings, we need to escape it again with another backslash (\\)....
🌐
SitePoint
sitepoint.com › get started
Split on a back slash (\\) - Get Started - SitePoint Forums | Web Development & Design Community
September 21, 2007 - String filePath = "c:\\\\documents and settings\\\\you\\\\desktop"; String[] fileParts = filePath.split("\\\\");
🌐
Javajee
javajee.com › string-replace-and-string-split-in-java-with-examples
String Replace and String Split in Java with Examples | JAVAJEE.COM
April 13, 2018 - The second parameter of String.replaceAll is a replacement string and a backslash appearing in a replacement string escapes the following character, which is the ending double quotes.
🌐
Blogger
abhinandanmk.blogspot.com › 2011 › 04 › how-to-split-on-or-backslash-in-java.html
How to split on "\" or backslash in Java | Abhi
April 30, 2011 - String path = "C:\\Users\\Ken"; Let's try to populate the folderNames array with "C:", "Users" and "Ken" by spliting on "\" - backslash · String[] folderNames= path.split("\\"); BUT We get the following exception:- /* Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1 \ ^ at java.util.regex.Pattern.error(Unknown Source) at java.util.regex.Pattern.compile(Unknown Source) at java.util.regex.Pattern.
🌐
Rahul Chandna
rahulchandna.com › posts › java-split-backslash
Java split backslash | Rahul Chandna
May 25, 2010 - Configuring java servlet for kerberos authentication Domain Name : dummydomain Service Account Name : myserviceaccount Service Account Password : afk1K2##$#dlkajsf SPNEGO SourceForg...
🌐
Medium
medium.com › sina-ahmadi › java-regex-6e4d073aab85
Java RegEx. special characters issue in Java split… | by Sina | My journey as a software developer | Medium
June 20, 2018 - Replace all special characters using Java’s “replaceAll” method in the input string, with escaped special characters ... escapedString = nonEscapedString.replaceAll("\\*", "\\\\*"); splittedString = escapedString.split("\\*")
🌐
Regular-Expressions.info
regular-expressions.info › java.html
Using Regular Expressions in Java
To insert a dollar sign as literal text, use \$ in the replacement text. When coding the replacement text as a literal string in your source code, remember that the backslash itself must be escaped too: "\\$". myString.split("regex") splits the string at each regex match.