在Java中是否有办法通过类加载器在jar中检索的资源上构造File实例?
我的应用程序使用jar中的一些文件(默认)或运行时指定的文件系统目录(用户输入).我正在寻找一种一致的方法:
a)将这些文件作为流加载
b)分别列出用户定义目录中的文件或jar中的目录
编辑:显然,理想的方法是完全远离java.io.File.有没有办法从类路径加载目录并列出其内容(包含在其中的文件/实体)?
我应该如何退出Qt程序,例如加载数据文件,发现文件损坏,用户是否需要退出此应用程序或重新启动数据文件?
我是不是该:
exit(EXIT_FAILURE)
QApplication::quit()
QCoreApplication::quit()
和(2)和(3)之间的区别?
事情很简单,但不能按原样运作.
我有一个文本文件作为原始资源添加.文本文件包含如下文本:
b)如果适用法律要求对软件提供任何担保,则所有此类担保的有效期限为交付日期后的第九十(90)天.
(c)虚拟定向,其经销商,经销商,代理商或雇员提供的任何口头或书面信息或建议均不构成保证或以任何方式增加此处提供的任何保证的范围.
(d)(仅限美国)有些州不允许排除默示担保,因此上述排除条款可能不适用于您.本担保赋予您特定的法律权利,您可能还拥有其他法律权利,这些权利因国家/地区而异.
在我的屏幕上,我有这样的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1.0"
android:layout_below="@+id/logoLayout"
android:background="@drawable/list_background">
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/txtRawResource"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dip"/>
</ScrollView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
读取原始资源的代码是:
TextView txtRawResource= (TextView)findViewById(R.id.txtRawResource);
txtDisclaimer.setText(Utils.readRawTextFile(ctx, R.raw.rawtextsample);
public static String readRawTextFile(Context ctx, int resId)
{
InputStream inputStream = ctx.getResources().openRawResource(resId);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int i;
try {
i = inputStream.read();
while (i != -1)
{
byteArrayOutputStream.write(i);
i = inputStream.read();
}
inputStream.close();
} catch (IOException e) {
return null;
}
return byteArrayOutputStream.toString();
}
Run Code Online (Sandbox Code Playgroud)
文本得到了显示,但在每一行后我得到一个奇怪的字符[]如何删除该字符?我认为这是新线.
工作解决方案
public static …
Run Code Online (Sandbox Code Playgroud) AndroidStudio 3.0/Android Gradle插件3.0
<vector>
<path android:fillColor="@color/image_button_disabled"/>
</vector>
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
构建时PNG生成不支持对其他资源的引用
是否有可能解决它或者它是一个弃用的解决方案?
我刚刚将Tomcat从版本7.0.52升级到8.0.14.
我得到了很多静态图像文件:
org.apache.catalina.webresources.Cache.getResource无法将[/base/1325/WA6144-150x112.jpg]上的资源添加到缓存中,因为在驱逐过期的缓存条目后可用空间不足 - 考虑增加最大大小缓存
我没有指定任何特定的资源设置,我没有得到这个7.0.52.
我已经在启动时发现了一个错误报告中提到的这种情况.对我来说,这不是在启动时发生,而是在请求资源时不断发生.
还有其他人有这个问题吗?
试图至少只是禁用缓存,但我找不到如何指定不使用缓存的示例.这些属性已经从Tomcat版本8中的上下文中消失了.尝试添加资源但无法正确配置.
<Resource name="file"
cachingAllowed="false"
className="org.apache.catalina.webresources.FileResourceSet"
/>
Run Code Online (Sandbox Code Playgroud)
谢谢.
我将PNG存储为程序集中的嵌入式资源.在同一个程序集中,我有一些像这样的代码:
Bitmap image = new Bitmap(typeof(MyClass), "Resources.file.png");
Run Code Online (Sandbox Code Playgroud)
名为"file.png"的文件存储在"Resources"文件夹中(在Visual Studio中),并标记为嵌入式资源.
代码失败,异常说:
在MyNamespace.MyClass类中找不到资源MyNamespace.Resources.file.png
我有相同的代码(在不同的程序集中,加载不同的资源)工作.所以我知道这种技术很合理.我的问题是我最终花了很多时间试图弄清楚正确的路径是什么.如果我可以简单地查询(例如在调试器中)程序集以找到正确的路径,那将为我节省大量的麻烦.
我想知道在Java中加载资源的最佳方法:
this.getClass().getResource() (or getResourceAsStream())
,Thread.currentThread().getContextClassLoader().getResource(name)
,System.class.getResource(name)
.假设我的iPhone应用程序的"Resources"文件夹中有一个名为"Documents"的文件夹.
有没有办法在运行时获取该文件夹中包含的所有文件的数组或某种类型的列表?
所以,在代码中,它看起来像:
NSMutableArray *myFiles = [...get a list of files in Resources/Documents...];
Run Code Online (Sandbox Code Playgroud)
这可能吗?
我正在编写几个需要共享资源和个人资源的WPF用户控件.
我已经找到了从单独的资源文件加载资源的语法:
<UserControl.Resources>
<ResourceDictionary Source="ViewResources.xaml" />
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时,我不能在本地添加资源,例如:
<UserControl.Resources>
<ResourceDictionary Source="ViewResources.xaml" />
<!-- Doesn't work: -->
<ControlTemplate x:Key="validationTemplate">
...
</ControlTemplate>
<style x:key="textBoxWithError" TargetType="{x:Type TextBox}">
...
</style>
...
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
我看过ResourceDictionary.MergedDictionaries,但这只能让我合并多个外部字典,而不是在本地定义更多资源.
我一定错过了一些微不足道的事情?
应该提到的是:我在WinForms项目中托管我的用户控件,因此在App.xaml中放置共享资源并不是一个真正的选择.
我什么时候应该使用其中一种?
我想在我的应用程序中使用的所有文件(图像,声音,xml文件等)都在.exe文件中,所以我不会使用一堆文件夹和文件进行部署.
谢谢(你的)信息.