简体   繁体   English

如何在Java中声明文件路径

[英]How to declare a file path in java

I am making an android app that involves an internal database. 我正在制作一个涉及内部数据库的android应用。 I decided to open the database file rather than produce it within the app. 我决定打开数据库文件,而不是在应用程序中生成它。

this is the code: 这是代码:

private static String databasePath = "H:\Workspace\PetrocVLE\assets";
public SQLiteDatabase db = SQLiteDatabase.openDatabase(databasePath, null, 0);

this is the error that is being given: 这是正在给出的错误:

Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )

I thought this might mean that there are symbols within the databasePath variable which are not allowed within a string variable, however I have seen other String variables that used these within java. 我认为这可能意味着在stringPath变量中不允许使用databasePath变量中的符号,但是我看到了在Java中使用这些符号的其他String变量。

Can anybody tell me what I am doing wrong? 有人可以告诉我我做错了吗? thankyou 谢谢

\\ is a special character used in Java Strings , you would need to escape it, \\是Java Strings使用的特殊字符,您需要对其进行转义,

private static String databasePath = "H:\\Workspace\\PetrocVLE\\assets";

However, file paths on Android normally take this format 但是,Android上的文件路径通常采用这种格式

private static String databasePath = "/Workspace/PetrocVLE/assets";

使用/代替,因为它与操作系统无关:

private static String databasePath = "H:/Workspace/PetrocVLE/assets";

您可能还希望独立于驱动器,并引用代表您的驱动器号的变量或尝试使用相对路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM