如何从D:目录加载文件 [英] How to load a file from D: Directory

查看:92
本文介绍了如何从D:目录加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在c://images/flower.jpg中有一些图像文件



我使用下面的代码来读取文件的输入流。

 InputStream in = null; 
FileInputStream fis = new FileInputStream( c ://images/flower.jpg);
in = fis;
fis.close();





但是流总是返回Null。请帮我看看文件流。



谢谢,

Baskar G

解决方案

尝试删除之后的一个斜杠c:

来自

 FileInputStream fis =  new  FileInputStream(  c:/ /images/flower.jpg); 



to

 FileInputStream fis =  new  FileInputStream(  c:/images/flower.jpg); 


我的猜测是你认为正斜杠(/)是一个需要加倍的转义字符。它没有。



在Microsoft系统上,您可以使用正斜杠或反斜杠。背斜线需要加倍;不要使用正斜杠。



将您的代码更改为以下任意一个:



 FileInputStream fis =  new  FileInputStream(  c: /images/flower.jpg); 









 FileInputStream fis =  new  FileInputStream(  c:\\images\\flower.jpg); 


你也可以避免通过在字符串前加上 @ 前缀加倍反斜杠。



 FileInputStream fis =  new  FileInputStream( @  c:\ imagesm \\flower.jpg); 


Hi,

I have some image files in c://images/flower.jpg

I have used following code to read the input stream of the file.

InputStream in = null;
FileInputStream fis = new FileInputStream("c://images/flower.jpg");
in  = fis;
fis.close();



But stream always returns Null . Please help me to read the file stream .

Thanks,
Baskar G

解决方案

Try to remove one of the slashes after c:
From

FileInputStream fis = new FileInputStream("c://images/flower.jpg");


to

FileInputStream fis = new FileInputStream("c:/images/flower.jpg");


My guess is that you consider the forward slash ("/") to be an escape character that needs to be doubled. It doesn't.

On Microsoft systems, you can use either forward or back slashes. Back slashes need to be doubled; forward slashes do not.

Change your code to either one of:

FileInputStream fis = new FileInputStream("c:/images/flower.jpg");



or

FileInputStream fis = new FileInputStream("c:\\images\\flower.jpg");


You can also avoid the backslashes-doubling by prefixing your string with an @.

FileInputStream fis = new FileInputStream(@"c:\images\flower.jpg");


这篇关于如何从D:目录加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆