在项目中,我们通常会遇到文件的读写,
一般有两个问题需要处理。
1路径问题。
2读写问题。
路径的解决方案。
路径之间的连接。
eg1:D磁盘下的文件夹中的1.txt。
path="D://file//1.txt "
path="D:/file/1.txt "
path="D:file1.txt "
三个都行。
1绝对路径(绝对不推荐)。
也就是从电脑的根目录开始,驱动器c和驱动器d,详见eg1。
2相对路径
在java项目中,默认是从项目的根目录开始,如下所示。
获取此目录中的所有文件(获取一个目录)。/获取当前根目录。
字符串路径=。/";
文件f=新文件(路径);
file[]files=f . listfiles();
for(int I=0;ifiles.lengthI){ 0
System.out.println(文件[i])。getName());
}
./获取根目录下的父目录。想要得到多级父目录,只需要在web项目中写入n(需要注意的是,这种方法最多只能得到Windows磁盘下的根目录,也就是最多只能得到c盘或者d盘,不可能像Linux那样得到/root/d)。
主要是区分工作空间和发布空间。
例如,使用struts2文件上传时。
定义接受文件的目录。
ServletContext ServletContext=servletactioncontext . getservletcontext();
string str=servletcontext . getrealpath("/files/" file filename);
Eclipse暂时有点小问题。我稍后会写这个。
读写文档(如有误,请积极指出,共同进步)。
因为文件有不同的格式,就文本文件而言,有utf-8 GBK等等。
建议使用字节流(InputStream是所有字节输入流的祖先,OutputStream是所有字节输出流的祖先)代替字符流进行读取(Reader是所有读取字符串输入流的祖先,writer是所有输出字符串的祖先)。
其实内部一个是字节[]实现的,另一个是char[]实现的,看JDK的源代码就明白了。
具体字符流和字节流的区别请参考转载。
http://blog.csdn.net/zxman660/article/details/7875799
http://blog.csdn.net/cynhafa/article/details/6882061
读写文件
package com . wzh . utils;
导入Java . io . bufferedinputstream;
导入Java . io . bufferedoutputstream;
导入Java . io . file;
导入Java . io . FileInputStream;
导入Java . io . FileOutputStream;
导入Java . io . InputStream;
导入Java . io . OutputStream;
公共类InOutFile {
/**
* @param文件null,一个文件,一个文件目录,
* pre
*文件字节(空)=空
*文件字节(填充
e)=null file>2G
* fileToByte(文件目录)=null
* fileToByte(file)=byte[]
* </pre>
* @return null(文件不存在,null,文件目录,文件太大>2G) byte[](转换成功)
*/
public byte[] fileToByte(File file) {
byte[] buffer = null;
if (file == null) {
throw new IndexOutOfBoundsException();
}
if (file.isDirectory())
return buffer;
if (file.length() > Integer.MAX_VALUE)
return buffer;
if (file.isFile()) {
int filelength = (int) file.length();
InputStream inputStream = null;
BufferedInputStream bufferedInputStream = null;
OutputStream outputStream=null;
BufferedOutputStream bufferedOutputStream=null;
File outfile=new File(“files//out//”+file.getName());
int n = 0;
int off = 0;
int length = 4096;
try {
if(!outfile.exists())
outfile.createNewFile();
inputStream = new FileInputStream(file);
outputStream=new FileOutputStream(outfile, true);
bufferedInputStream = new BufferedInputStream(inputStream);
bufferedOutputStream=new BufferedOutputStream(outputStream);
buffer = new byte[filelength];
/*
* 添加(length <= filelength – off) ? length : filelength – off)的比较
* 为了防止读到最后buffer 剩余的长度没有4096 装不下那么多会导致读取不了IndexOutOfBoundsException()
* 当filelength – off=0时表示文件读取完毕但是read内部认为是其他线程占用io导致堵塞并不会认为文件读取完毕
* 所以要添加上filelength – off>0
*/
while ((filelength – off) > 0 && (n = bufferedInputStream.read(buffer, off,
((length <= filelength – off) ? length : filelength – off))) >= 0) {
bufferedOutputStream.write(buffer, off, n);
off += n;
}
}
catch (Exception e) {
}
finally {
closeInputStream(bufferedInputStream);
closeInputStream(inputStream);
closeOutputStream(bufferedOutputStream);
closeOutputStream(outputStream);
System.out.println(“end”);
}
}
return buffer;
}
/**
* close inoutstream
* @param inputStream null or the inputstream’s child
*/
private void closeInputStream(InputStream inputStream) {
if (inputStream == null)
return;
try {
inputStream.close();
}
catch (Exception e) {
}
}
/**
* close outputStream
* @param outputStream null or the outputStream child
*/
private void closeOutputStream(OutputStream outputStream) {
if (outputStream == null)
return;
try {
outputStream.flush();
outputStream.close();
}
catch (Exception e) {
}
}
}
当然字符流也可以进行读取文件
字符流也可以进行读取文件只不过要指定文件(文本文件)的编码
- /**
- * 创建不同格式的文本文件
- * @throws Exception
- */
- private void createFile() throws Exception {
- File file = new File(“files//kindsformat//utf//1.txt”);
- //File file = new File(“files//kindsformat//gbk//1.txt”);
- if (!file.exists())
- file.createNewFile();
- Writer writer = new OutputStreamWriter(new FileOutputStream(file, true), “UTF-8”);
- //Writer writer = new OutputStreamWriter(new FileOutputStream(file), “GBK”);
- BufferedWriter bufferedWriter = new BufferedWriter(writer);
- bufferedWriter.write(“我是中文测试啊测试啊”);
- bufferedWriter.flush();
- bufferedWriter.close();
- writer.close();
- }
有不对的地方欢迎指出,谢谢