方向
在java中进行文件的输入输出,要以程序为中心,也就是把自己想象成写的程序。向文件中写东西的时候是“写”,“write",”output“;从文件中读东西的时候是“读”,“read”,“input”。
方法
文件的输入输出常用的方法有三种:1.按字节(一个或多个)输入输出,2.按字符(一个或多个)输入输出,3.按行(一行)输入输出。
文件的输入输出套路有四个步骤:
- 创建源:File创建要操作文件的对象。------------------------------------------选择目标
- 选择流:选择合适的方法(上述3种常见方法)创建对应的流。---------选择工具
- 操作:读文件或者写文件。--------------------------------------------------------动手
- 关闭:把流关闭,释放资源。-----------------------------------------------------把工具放回去
按字节
read()返回读到的字节,read(byte[])返回读到的长度
为了提高效率,一般都多个字节读,后面的程序也是多个字符读
从文件中读
//创建源
File src = new File(name);
InputStream is = null;
//选择流
try {
is = new FileInputStream(src);
//操作
// 1,一个一个读
// int temp;
// while((temp = is.read()) != -1) {
// System.out.print((char)temp);
// }
//
// read()返回读到的字节,read(byte[])返回读到的长度
// 为了提高效率,一般都多个字节读,后面的程序也是多个字符读
//
// 2,一段一段读
byte[] flush = new byte[1024];
int len = -1;
while((len = is.read(flush)) != -1) {
String temp = new String(flush,0,len);
System.out.print(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
try {
if(is != null)
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
向文件中写
缓冲区要存满或流关闭才会向文件中写
为了防止有数据没写进文件,每次向缓冲区写后,调用flush()方法刷新
//1,创建源
File file = new File(name);
//2,选择流
OutputStream os = null;
try {
os = new FileOutputStream(file,false);
//3,操作
String msg = "IO is so easy";
byte[] bt = msg.getBytes();
os.write(bt,0,bt.length);
//缓冲区要存满或流关闭才向文件中写
//为了防止有数据没写进文件,将缓冲区刷新
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//4,关闭
try {
if(os != null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
按字符
从文件中读
//1,源
File file = new File(name);
//2,流
Reader mr = null;
try {
mr = new FileReader(file);
char[] flush = new char[1024];
int len = -1;
while((len = mr.read(flush)) != -1) {
String temp = new String(flush,0,len);
System.out.print(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(mr != null)
mr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
向文件中写
//1,源
File file = new File(name);
//2,流
Writer mw = null;
try {
//3,操作
mw = new FileWriter(file);
String msg = "王二狗是天才";
mw.write(msg.toCharArray());
mw.flush();
//mw.append(msg.toCharArray());
mw.append("王二狗是天才");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(mw != null)
mw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
按行
从文件中读
当程序中有两个流要关闭时,注意要将两个流单独关闭,即用两个try-catch分别包围它们,这是为了防止当一个发生异常的时候影响到另一个流的关闭。
FileInputStream fip = null;
BufferedReader bfr = null;
try {
fip =new FileInputStream(name); //str1为读取的文件路径
bfr=new BufferedReader(new InputStreamReader(fip)); //如果单纯的使用fip.read()一个字节一个字节的读取,则比较耗费时间,采用BufferedReader则可以一行行的读取
String data=null;
while((data=bfr.readLine())!=null){
System.out.println(data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//两个流单独关闭,防止相互影响
try {
if(bfr != null)
bfr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fip != null)
fip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
没有按行向文件中写数据(要自己加 ‘\n’)