文章包含一下内容:
(点击即可跳转哦~)
- 字节输出流(FileOutputStream)向文件写入内容
- 字节输入流(FileInputStream)读取文件内容
- 字符输出流(FileWrite)向文件写入内容
- 字符输入流(FileReader)读取文件内容
- 最终案例,读取文件数据并转入到另一个文件
什么??你对IO流还是一脸懵逼状态,那就戳我跳转到I/O常用实例大汇总,学习一下基础知识吧
ps:这个常用实例大汇总目前只是在创建阶段 嘻嘻 (^__^)
在学习之前我对输入输出的概念非常懵逼,不知道什么时候用输出流什么时候用输入流,现在我总结出来了:我向一个地方(程序/文件)发送/写入数据—输出流,我要读取/获得一个地方的数据—输入
向文件写入数据——FileOutputStream
1.构造方法:
FileOutputStream is=new FileOutputStream(String name) ;
FileOutputStream is=new FileOutputStream(File file) ;
2.写入方法:
int write();
int write(byte b [] );
int write(byte b[],int off,int len);
(后面的两个方法比较常用)
String txt="我是中国人";
//把内容转换成字节数组写进去
byte[]b2=txt.getBytes();
String name="C:\\Users\\lenovo\\Desktop\\text\\b.txt";
File file2 = new File(name);
try {
FileOutputStream fileOutputStream=new FileOutputStream(file2);
fileOutputStream.write(b2);//写入字节数组的内容
//关闭流
fileOutputStream.close();
System.out.println("写入成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
效果:
步骤回顾:
1.实例化要写入的文件
2.实例化文件字节输出流
3.写要编辑的内容,并转换成字节数组
4.调用写入的方法
5.输出(可有可无)
文件字符输入输出流
读取文件的数据——FileInputStream
1.构造方法
FileInputStream is=new FileInputStream(String name) ;
FileInputStream is=new FileInputStream(File file) ;
2.读数据的方法(不断地读取数据,所以要放到一个while循环中)
int read();
int read(byte b [] );
int read(byte b[],int off,int len);
(后面的两个方法比较常用)
String name="C:\\Users\\lenovo\\Desktop\\text\\a.txt";
File file = new File(name);
// 实例化一个字节数组,将读取到的内容放到字节数组中
byte[] b = new byte[100];// 定义长度,一次读多少个字节
// read方法会返回每次读取的字节数
//读取了100个字节读会返回100,读到尾部返回-1
int count = 0;//装载read方法的返回值
String string = null;
try {
//实例化
FileInputStream fileInputStream = new FileInputStream(file);
// 读取文件(让文件一直读,直到读到尾部)
while ((count = fileInputStream.read(b)) != -1) {
//while循环什么都不放,因为fileInputStream.read(b)这一步就是在执行读取的代码了
}
// 字节数组(byte)没有办法直接打印,所以要转换成一个字符串(如果长度比较长可以使用StringBuffer)
string = new String(b, 0, count);
System.out.println(string);
//关闭流
fileInputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
效果:
步骤回顾:
1.实例化要读取的文件
2.实例化文件字节输入流
3.创建一个字节数组用来装载读取的数据
4.使用while循环,不断地读取数据,直到读完
5.输出
向文件中写入字符——FileWrite
1.构造方法:
FileWrite fw=new FileWrite(String name);
FileWrite fw=new FileWrite(File file);
2.写入方法:
int write();
int write(char c [] );
int write(char c[],int off,int len);
(后面的两个方法比较常用)
//读取字符
String content="我爱JAVA,JAVA使我快乐";//呕...
File file=new File("C:\\Users\\lenovo\\Desktop\\text\\c.txt");
//将字符串转换成字符数组
char [] c=content.toCharArray();
try {
FileWriter fw=new FileWriter(file);
fw.write(c);
fw.close();
System.out.println("写入字符成功");
} catch (IOException e) {
e.printStackTrace();
}
效果
步骤回顾:
1.实例化要写入的文件
2.实例化文件字符输出流
3.写要编辑的内容,并转换成字符数组
4.调用写入的方法
5.输出(可有可无)
从文件中读取字符——FileReader
1.构造方法:
都是套路。。。。。。
2.读取方法:
我好懒呀。。。。。。
实例
try {
String name="C:\\Users\\lenovo\\Desktop\\text\\d.txt";
File file = new File(name);
FileReader fr = new FileReader(file);
char c2[] = new char[10];// 每次读取10个字符
int count = 0;
//这里选择用可拼接的StringBuffer来接收读取的字符
StringBuffer sb = new StringBuffer();
while ((count = fr.read(c2)) != -1) {
sb.append(c2);
}
System.out.println(sb.toString());
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
效果
总结步骤:
此处省略5小条。。。。。。
我是大BOSS
//读取c中的文件,并写入到e
File fileC=new File("C:\\Users\\lenovo\\Desktop\\text\\c.txt");
File fileE=new File("C:\\Users\\lenovo\\Desktop\\text\\e.txt");
try {
FileReader fr=new FileReader(fileC);
FileWriter fw=new FileWriter(fileE);
char[]tmp=new char[100];//一次读取100个字符
int count=0;
while((count=fr.read(tmp))!=-1){
fw.write(tmp);
}
fw.close();
fr.close();
System.out.println("操作成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
效果:
已经不想放效果了。。。。。。
步骤总结:
1.先把两个要操作的文件定义一下
2.实例化输入输出流
3.创建一个装载读取文件的字符数组,用while循环将文件内容读取出来
4.用文件输出流将 内容写入
那就这样吧,谢谢观看,再见!