我对Java知之甚少.我需要从FilePath(String)windows 构造一个URI的字符串表示.有时inputFilePath我得到的是:file:/C:/a.txt有时是:C:/a.txt.现在,我正在做的是:
new File(inputFilePath).toURI().toURL().toExternalForm()
Run Code Online (Sandbox Code Playgroud)
以上工作适用于路径,它没有前缀file:/,但对于前缀为路径的路径file:/.toURI方法是通过附加当前dir的值将其转换为无效的URI,因此路径变为无效.
请通过建议正确的方法为这两种路径获取正确的URI来帮助我.
我正在运行的系统是Windows XP,JRE 1.6.
我这样做:
public static void main(String[] args) {
try {
System.out.println(new File("C:\\test a.xml").toURI().toURL());
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
我明白了 file:/C:/test%20a.xml
为什么给定的URL之前没有两个斜杠C:?我期待file://C:....这是正常的行为吗?
编辑:
来自Java源代码:java.net.URLStreamHandler.toExternalForm(URL)
result.append(":");
if (u.getAuthority() != null && u.getAuthority().length() > 0) {
result.append("//");
result.append(u.getAuthority());
}
Run Code Online (Sandbox Code Playgroud)
似乎文件URL的Authority部分为null或为空,因此跳过双斜杠.那么URL的权限部分是什么?它是否真的不存在于文件协议中?
我有一个bash脚本,可以从用户那里收到一组文件.这些文件有时位于名称中带有空格的目录下.不幸的是,与此问题不同,所有文件名都通过命令行界面传递.让我们假设路径在用户传入时正确引用,因此空格(保存为引用空格)是路径之间的分隔符.如何以保留引用空格的方式将这些参数转发到我的bash脚本中的子例程?
有人可以告诉我如何获取文件data.xml的正确文件路径?这是文件所在的位置:

这是我的C#代码,它检查提供的路径是否存在该文件:
public class Parser
{
static void Main(string[] args)
{
Console.WriteLine(File.Exists("App_Data/data.xml"));
Console.Read();
}
}
Run Code Online (Sandbox Code Playgroud)
我一直变弱,意味着这样的文件不存在.所以到目前为止我尝试过的文件路径包括:
"~/App_Data/data.xml"
"/App_Data/data.xml"
"App_Data/data.xml"
Run Code Online (Sandbox Code Playgroud)
如果这是一个Web应用程序,我会知道该怎么做,通过使用HttpContext并获取该文件.但由于这是一个控制台应用程序,我不知道.
在相关的说明中,Console应用程序和Executable应用程序之间有什么区别?我是否正确,没有区别,因为如果控制台应用程序具有Main方法,它可以是可执行应用程序?
谢谢
我一直在尝试按照Android教程共享文件.我这样设置FileProvider:
在主清单xml上:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.mysecondapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
res/xml/filpaths.xml文件:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="myexternalimages" path="SpCars_album/" />
</paths>
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我尝试以下方法:
File requestFile = new File(mImageFilenames[position]);
try {
fileUri = FileProvider.getUriForFile(
SeventhActivity.this,
"com.example.mysecondapp.fileprovider",
requestFile);
} catch (IllegalArgumentException e) {
Log.e("File Selector",
"The selected file can't be shared: " +
mImageFilenames[position]);
}
Run Code Online (Sandbox Code Playgroud)
requestFile使用适当的文件工作路径进行实例化.该文件的路径完全以getExternalFilesDir(Environment.DIRECTORY_PICTURES)返回的内容开始.我只是无法理解是什么引起了错误,因为一切似乎都适合.提前致谢.
android filepath illegalargumentexception android-fileprovider
有没有办法用它们的绝对路径返回目录中的文件列表.
当我做
getDirectoryContents dir
Run Code Online (Sandbox Code Playgroud)
它给了我一个目录中的文件名列表.如果我在其他地方使用这些文件名,我需要知道它们的绝对路径或相对于当前工作目录的路径.
如何从URL或String中删除文件名?
String os = System.getProperty("os.name").toLowerCase();
String nativeDir = Game.class.getProtectionDomain().getCodeSource().getLocation().getFile().toString();
//Remove the <name>.jar from the string
if(nativeDir.endsWith(".jar"))
nativeDir = nativeDir.substring(0, nativeDir.lastIndexOf("/"));
//Load the right native files
for(File f : (new File(nativeDir + File.separator + "lib" + File.separator + "native")).listFiles()){
if(f.isDirectory() && os.contains(f.getName().toLowerCase())){
System.setProperty("org.lwjgl.librarypath", f.getAbsolutePath()); break;
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我现在所拥有的,它的工作原理.据我所知,因为我使用"/"它只适用于Windows.我想让它与平台无关
我的角度项目的路径是这样的
web
server.py ##flask server program
app
static
app.js
controllers.js
etc...
templates
index.html
home.html
Run Code Online (Sandbox Code Playgroud)
的index.html
<!-- this didn't work -->
<ng-include src="templates/home.html"><ng-include>
<!-- nor did this -->
<ng-include src="home.html"></ng-include>
Run Code Online (Sandbox Code Playgroud)
home.html的
<h1> home! </h1>
Run Code Online (Sandbox Code Playgroud)
除了我在输出中没有看到部分(home.html).
有谁看到我的错误?
是否可以在不使用C#的情况下获取文件的大小System.IO.FileInfo?
我知道你可以分别使用Path.GetFileName(yourFilePath)和得到其他东西,比如Name和Extension Path.GetExtension(yourFilePath),但显然不是文件大小?有没有其他方法我可以获得文件大小而不使用System.IO.FileInfo?
唯一的原因是,如果我是正确的,FileInfo会抓取比我真正需要的更多的信息,因此如果我需要的唯一东西是文件的大小,那么收集所有这些FileInfo需要更长的时间.有更快的方法吗?