1、路径分为绝对路径和相对路径。
绝对路径是指真实的路径,相对路径是相对于项目的名字所在的路径的,比如当前项目为
testfile,其下有个文件夹resource,resource下有个文件aa.txt,则txt的相对路径为resource/aa.txt
创建文件对象是使用下面的格式File file = new File("resource/aa.txt");
2、判断文件是否存在
File file = new File("resource/aa.txt");
if(!file.exists())
{
return false;
}
3、读取properties文件
方法1:文件在类路径下,比如bb.properties在src/testfile/bb.properties下,则读取该文件方法如下
InputStream in = TestClassPathProperties.class.getClassLoader()
.getResourceAsStream("testfile/bb.properties");
Properties pro = new Properties();
try
{
pro.load(in);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
方法2:文件不在类路径下,比如bb.properties在与src同级的config目录下,那么读取该文件的方法如下
Properties pro1 = new Properties();
File file = new File("config/bb.properties");
try
{
InputStream ins = new FileInputStream(file.getAbsoluteFile());
pro1.load(ins);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
4、读写文件常用的类为FileInputStream和FileReader,前面是字节流类,后面是字符类。
5、读写字节流的使用方法如下:
读字节流
File file = new File("src/testfile/bb.txt");
// 用于缓冲字节流,一般大小为1K
byte[] bytes = new byte[1024];
int n;
FileInputStream fis = null;
try
{
fis = new FileInputStream(file);
// 文件结束时返回值为-1
while((n = fis.read(bytes)) != -1)
{
// 将第0到n个字节解码为新的字符串
System.out.println(new String(bytes,0,n));
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
// 关闭字节流
fis.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
写字节流
File fileWrite = new File("D:\\test.txt");
String str = "aaaa写文件";
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(fileWrite);
fos.write(str.getBytes());
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
fos.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
利用输入字节流和输出字节流拷贝文件
File fileIn = new File("config/I18n.png");
File fileOut = new File("config/custom/I18n.png");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(fileIn);
fos = new FileOutputStream(fileOut);
byte[] by = new byte[1024];
while(fis.read(by) != -1)
{
fos.write(by);
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
fos.close();
fis.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
6、字符的读,FileReader
字符的写,FileWriter
演示从一个文件拷贝到另一个文件
FileReader fr = null;
FileWriter fw = null;
try
{
fr = new FileReader("D:\\a.txt");
fw = new FileWriter("D:\\java\\cc.txt");
//读取到缓冲区
char[] buffer = new char[1024];
int n = 0;
while ((n = fr.read(buffer)) != -1)
{
// 读取时指定其实字符和结束字符,因为有时1024长度的缓冲区不一定是满的
// 防止不满时,出现乱码情况
fw.write(buffer, 0, n);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
fr.close();
fw.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
7、一行一行的读取:BufferReader
一行一行的写入:BufferWriter
FileReader fr = null;
FileWriter fw = null;
BufferedReader br = null;
BufferedWriter bw = null;
String str = "";
try
{
fr = new FileReader("D:\\a.txt");
br = new BufferedReader(fr);
fw = new FileWriter("D:\\fw.txt");
bw = new BufferedWriter(fw);
while ((str = br.readLine()) != null)
{
bw.write(str + "\r\n");
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
bw.close();
fw.close();
br.close();
fr.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
8、参考文章