我试图在webapp中加载一个文件,FileNotFound
当我使用时我得到了一个例外FileInputStream
.但是,使用相同的路径,我能够在我这样做时加载文件getResourceAsStream()
.这两种方法之间有什么区别,为什么一种方法有效而另一方方法无效?
我只是想一个转换FileInputStream
到一个InputStream
,我该怎么办呢?
例如
FileInputStream fis = new FileInputStream("c://filename");
InputStream is = ?;
fis.close();
Run Code Online (Sandbox Code Playgroud) 可能重复:
java有效地获取文件大小
我有一个名为filename的文件,位于E://file.txt
.
FileInputStream fileinputstream = new FileInputStream(filename);
Run Code Online (Sandbox Code Playgroud)
我想要做的是以字节为单位计算此文件的大小.我怎么能这样做?
Reader和InputStream有什么区别?什么时候用?如果我可以使用Reader读取字符,为什么我会使用inputstream,我想读取对象?
我试图通过使用FileInputStream将文件读入数组,并且~800KB文件花了大约3秒来读入内存.然后我尝试了相同的代码,除了将FileInputStream包装到BufferedInputStream中,它花了大约76毫秒.为什么使用BufferedInputStream以字节逐字节读取文件,即使我仍在逐字节读取它?这是代码(代码的其余部分完全不相关).请注意,这是"快速"代码.如果你想要"慢"代码,你可以删除BufferedInputStream:
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(file));
int[] fileArr = new int[(int) file.length()];
for (int i = 0, temp = 0; (temp = is.read()) != -1; i++) {
fileArr[i] = temp;
}
Run Code Online (Sandbox Code Playgroud)
BufferedInputStream的速度提高了30多倍.远不止于此.那么,为什么会这样,并且可以使这个代码更有效(不使用任何外部库)?
我试图FileInputStream
在用户从图片库中选择的图像上获取一个对象.这是由URI
返回的androidandroid.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI
内容://媒体/外部/图像/媒体/ 3
当我尝试从这个对象构造一个java URI对象时,我得到一个IllegalArgumentException,其中包含URI中的异常描述Expected file scheme:content:// media/external/images/media/3而android URI将该方案显示为内容
从未找到原始问题的解决方案.但是如果你想要图片库中图像的字节流,这段代码就可以做到.
content://media/external/images/media/3
Run Code Online (Sandbox Code Playgroud) 让我在这篇文章前面加上一点谨慎.在Java方面,我是一个初学者.我已经开启和关闭了PHP一段时间,但我已经准备好制作一个桌面应用程序,所以我决定使用Java,原因有很多.
我正在处理的应用程序处于开始阶段(少于5个类),我需要从本地文件中读取字节.通常,文件目前小于512kB(但将来可能会变大).目前,我使用a FileInputStream
将文件读入三个字节数组,完全满足我的要求.但是,我已经看到了一个BufferedInputStream
提到的,并且想知道我目前这样做的方式是最好的,还是我应该使用一个BufferedInputStream
.
我做了一些研究,并在Stack Overflow上阅读了一些问题,但是我仍然遇到麻烦,了解何时使用而不使用的最佳情况BufferedInputStream
.在我的情况下,我读取字节的第一个数组只有几个字节(小于20).如果我收到的数据在这些字节中是好的,那么我将文件的其余部分读入另外两个不同大小的字节数组.
我也听过很多人提到分析,看看哪个在每个特定情况下哪个更有效,但是,我没有剖析经验,我不确定从哪里开始.我也会喜欢这方面的一些建议.
对于这么长的帖子我很抱歉,但我真的想学习并理解做这些事情的最佳方法.我总是习惯于第二次猜测我的决定,所以我会喜欢一些反馈.谢谢!
我正在尝试编写一个应用程序从url下载pdf,将它们存储在sd上,然后由adobe pdf reader或其他能够打开pdf的应用程序打开.
直到现在,我已经"成功下载并将其存储在Sd卡上"(但每当我尝试使用pdf阅读器打开pdf时,阅读器崩溃并发出意外错误),例如,http://maven.apache.组织/ Maven的1.x中/ maven.pdf
这是我的下载程序的代码:
//........code set ui stuff
//........code set ui stuff
new DownloadFile().execute(fileUrl, fileName);
private class DownloadFile extends AsyncTask<String, Void, Void>{
@Override
protected Void doInBackground(String... strings) {
String fileUrl = strings[0]; // -> http://maven.apache.org/maven-1.x/maven.pdf
String fileName = strings[1]; // -> maven.pdf
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "testthreepdf");
folder.mkdir();
File pdfFile = new File(folder, fileName);
try{
pdfFile.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
FileDownloader.downloadFile(fileUrl, pdfFile);
return null;
}
}
public class FileDownloader { …
Run Code Online (Sandbox Code Playgroud) 在我的java项目中,我将FileInputStream传递给一个函数,我需要将其转换(类型转换为FileInputStream为string),如何做到这一点.
public static void checkfor(FileInputStream fis) {
String a=new String;
a=fis //how to do convert fileInputStream into string
print string here
}
Run Code Online (Sandbox Code Playgroud) 我试图使用readObject读取二进制文件中的行数,但我得到IOException EOF.我这样做是对的吗?
FileInputStream istream = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(istream);
/** calculate number of items **/
int line_count = 0;
while( (String)ois.readObject() != null){
line_count++;
}
Run Code Online (Sandbox Code Playgroud) fileinputstream ×10
java ×8
file-io ×3
android ×2
inputstream ×2
download ×1
file ×1
filereader ×1
pdf ×1
uri ×1