FileOutputStream读取文件夹时出错

wangwffgqeg 2010-03-10 08:23:14
FileOutputStream fi = new FileOutputStream("C:\\test");
如果 C:\\test是文件夹时就会出现异常:
java.io.FileNotFoundException: E:\snake (拒绝访问。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at test_1.FileChannelTest.main(FileChannelTest.java:25)
这个问题怎么解决呀。怎么才能和文件夹建硫呀。
...全文
1082 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
哈特中尉 2010-03-13
  • 打赏
  • 举报
回复
文件夹怎么读呢?读的是文件啊~!虽然file在Java中指的是文件和文件夹!

字符的写入和读取:
package IO;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
/**
*
*用read和write实现文件的写入和读取(字符的写入和读取)
*
*/
public class ReadAndWrite {
//定义文件的路径,一边写入和读取使用
String path="e:\\abc.txt";

public static void main(String[] args) {
ReadAndWrite r=new ReadAndWrite ();
r.writeFile("我乃邪恶少年是也!");
r.readFile();
}
private void readFile() {
// TODO Auto-generated method stub
try {
Reader r=new BufferedReader (new FileReader(path));
String test="";
int temp=0;
try {
while ((temp=r.read())!=-1) {
test+=(char)temp;
}
System.out.println(test);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void writeFile(String content) {
// TODO Auto-generated method stub
File f=new File (path);
if (f.exists()==false) {
try {
f.createNewFile();
FileWriter fw=new FileWriter (f);
fw.write(content);
fw.flush();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}else {
System.out.println(path+"已存在,写入失败,源文件是");
}
}
}

字节的写入和读取:
package IO;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* test.getBytes("iso8859-1"))
* 用OutputStream和InputStream实现文件的写入和读取(字节的写入和读取)
*
*/
public class InAndOut {
String path="e:\\abc.txt";
public static void main(String[] args) {
InAndOut io=new InAndOut ();
io.write("我是邪恶少年!");
io.read();
}
public void write(String content)
{
File f=new File (path);
boolean add=true;//是否写入
try {
if (f.exists()==false) {
f.createNewFile();
FileOutputStream os=new FileOutputStream (f);//不用判断是否写入
//FileOutputStream os=new FileOutputStream (f,add);//要判断是否写入
os.write(content.getBytes());
os.flush();
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void read()
{
try {
InputStream is=new FileInputStream(path);
//BufferedInputStream bis=new BufferedInputStream(new FileInputStream(path));
int temp=0;
String test="";
try {
while ((temp=is.read())!=-1) {
test+=(char)temp;
}
System.out.println(new String (test.getBytes("iso8859-1")));//会出现乱码
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
wanbosen 2010-03-13
  • 打赏
  • 举报
回复
读取一个文件要这么多东西, 我天儿,学习中。。。。。。
liguang168 2010-03-11
  • 打赏
  • 举报
回复
楼上已经有答案了。回复内容太短了!
YouLoveApple 2010-03-10
  • 打赏
  • 举报
回复
可以先用File f=new File("c:\\test");if(f.isFile())如果是TRUE就直接使用FileInputStream建立流,否则就是一个问价夹了。
duiduiaa 2010-03-10
  • 打赏
  • 举报
回复
考 不能是文件夹!!!!!如果想读文件夹中的所有文件, 需要判断是否是文件夹,然后读子文件和文件


对于文件,用流读取,对于文件夹调用本身,一个递归函数。
hui135 2010-03-10
  • 打赏
  • 举报
回复
Creates an output file stream to write to the file with the specified name
这里
hui135 2010-03-10
  • 打赏
  • 举报
回复
public FileOutputStream(String name)
throws FileNotFoundException
Creates an output file stream to write to the file with the specified name. A new FileDescriptor object is created to represent this file connection.
First, if there is a security manager, its checkWrite method is called with name as its argument.

If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.
这里说得很清楚了啊!
cwjieNo1 2010-03-10
  • 打赏
  • 举报
回复
C:\\test是文件夹时就会出现异常:
这个只能读取文件啊,文件夹怎么可以读取啊
梦_枫 2010-03-10
  • 打赏
  • 举报
回复
哪有和文件夹建流的啊,,,,

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧