java文件输出流需要写一个工程下个文件夹,和一个文件名!需要怎么写?

FileOutputStream fos = new FileOutputStream("/img/filename.jpg");
我这么写报错

最好能把代码全部贴出来,复制粘贴多快啊。我写了个,希望对你有帮助,还有什么问题的话再继续问吧

import java.io.*;
public class FileStream {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//File f= new File("d:\\JAVA/hello.txt");
//File f= new File("D:\\JAVA/lomboz-all-in-one-win32/eclipse/workspace/FileOutputStream1/hello.jpg");
File f= new File("d:\\hello.jpg");
try{
FileOutputStream out=new FileOutputStream(f);
byte buff[]="JAVA".getBytes();
out.write(buff);
out.close();
}catch(Exception e){
System.out.println(e.getMessage());
}

try{
FileInputStream in=new FileInputStream(f);
byte [] buff =new byte[1024];
int len=in.read(buff);
System.out.println(new String(buff,0,len));
}catch(Exception e){
System.out.println(e.getMessage());
}
}

}
可以用记事本打开hello.jpg查看内容。

wepoom的代码也不错。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-10-27
在window的环境下开发要写成这样:
FileOutputStream fos = new FileOutputStream("\\img\\filename.jpg");
第2个回答  2011-10-27
java在读流的时候需要确保文件已经存在。
如果不存在你要先创建文件才行。
第3个回答  2011-10-27
要捕捉异常:
try {
FileOutputStream fos = new FileOutputStream("/img/filename.jpg");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
相似回答