标签:fileinput fileread 代码 system throw close 冗余 源码 ade
如果在代码中写入大量的数据,会增加代码的冗余度,通过读取文件的方式,可以精简代码,便于数据的修改和代码的维护
IO流的分类:字节流和字符流
字符输入流:读取文本文件的,抽象基类java.io.Reader。读的方法read,很多重载形式,读取单个字符、字符数组、字符数组的一部分。close关闭流对象。
#java
package study1;
import org.testng.annotations.Test;
import java.io.*;
public class IODemo {
@org.junit.Test
public void test() throws IOException {
//如果文件不存在,会自动创建
FileWriter fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt"));
fw.write("hello world\n");
fw.write("123\n");
/**
* 写入文件后,未执行其他操作,写入的内容不会保存
* 写入文件后,close流,会自动保存文件。
* 写入数据较多是,使用write写入后,使用flush保存写入的内容,降低对系统压力
*/
fw.flush();
fw.write(123);//此处写入的是ascii码值
fw.flush();
fw.close();//释放流资源
}
@org.junit.Test
public void test2() {
FileWriter fw= null ;
try{
fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt"));
}catch (IOException e){
e.printStackTrace();
}finally{
try{
fw.close();//此处释放流资源,必须要先初始化
}catch (IOException e){
e.printStackTrace();
}
}
}
@org.junit.Test
public void append() throws IOException{
//追加内容,而非覆盖
FileWriter fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt"),true);
fw.write("hello python\n");
fw.flush();
fw.close();
}
@org.junit.Test
public void test_read() throws IOException{
//读取文件
FileWriter fw = null;
fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt"));
fw.write("abcd");
fw.flush();
fw.close();
FileReader fr = null;
fr = new FileReader(new File("/Users/chenshanju/Desktop/data/a.txt"));
System.out.println(fr.read());//输出字符的ASCII码值,每执行一次就读取1次
System.out.println((char)fr.read());
System.out.println(fr.read());
System.out.println(fr.read());
System.out.println(fr.read());
System.out.println(fr.read());//没有内容,输出-1
fr.close();
}
@org.junit.Test
public void test_read2() throws IOException{
//读取单个字符
FileWriter fw = null;
fw = new FileWriter(new File("/Users/chenshanju/Desktop/data/a.txt"));
fw.write("abcd");
fw.flush();
fw.close();
FileReader fr = null;
fr = new FileReader(new File("/Users/chenshanju/Desktop/data/a.txt"));
int i=0;
while((i= fr.read())!=-1){
/**
* 获取字符的ascii码值,并根据ascii码值强转为字符
*/
System.out.println((char)i);
}
fr.close();
}
@org.junit.Test
public void test_read3() throws IOException{
//读取字符数组,一次读取2个字符
FileWriter fw = new FileWriter(new File("Data/b.txt"));
fw.write("java");
char [] cha = {‘a‘,‘b‘,‘c‘};
fw.write(cha);
fw.close();
FileReader fr = new FileReader(new File("Data/b.txt"));
int i = 0;
char [] ch = new char[2];//一次读取2个字符
while((i=fr.read(ch))!=-1){
System.out.print(i+"\t");
System.out.println(ch);
/**
* System.out.println(i+"\t"+new String(ch));
* 和int类型同时使用,不加new String,会返回哈希码[C@6a6824be
* 加上new String和上面2行输出结果一致
* 2 ja
* 2 va
* 2 ab
* 1 cb 最后1次只装入了1个字符,只将a替换为c,b并未替换
*/
}
}
@org.junit.Test
//单个字符的写入
public void test_cp() throws IOException{
FileReader fr = null;
FileWriter fw = null;
fr = new FileReader(new File("Data/b.txt"));
fw = new FileWriter(new File("Data/b1.txt"));
int i = 0;
while((i=fr.read())!=-1){
fw.write((char)i);
}
fw.close();
fr.close();
}
@org.junit.Test
//字符数组的写入
public void test_cp1() throws IOException{
FileReader fr = null;
FileWriter fw = null;
fr = new FileReader(new File("Data/b.txt"));
fw = new FileWriter(new File("Data/b2.txt"));
char [] ch = new char[2];
int i = 0;
while((i=fr.read(ch))!=-1){
fw.write(ch,0,i);
}
fw.close();
fr.close();
}
@org.junit.Test
public void test_out() throws IOException{
//字节输出流
FileOutputStream out = null;
out = new FileOutputStream(new File("Data/c.txt"));
out.write("abcdefgh".getBytes());
out.close();
}
@org.junit.Test
public void test_in() throws IOException{
//字节输入流
FileInputStream in = null;
in = new FileInputStream(new File("Data/c.txt"));
int i = 0;
while((i=in.read())!=-1){
System.out.println((char)i);
}
in.close();
}
@org.junit.Test
public void test_in_char() throws IOException{
FileInputStream in = new FileInputStream(new File("Data/c.txt"));
int i = 0;
byte [] bt = new byte[10];
while((i=in.read(bt))!=-1){
System.out.print(i+"\t"+new String(bt));
/**
* System.out.println(bt);返回哈希码[B@6a6824be
* 长度不满10位,会用空位补齐
*/
}
}
@org.junit.Test
public void test_IO() throws IOException{
//将某网页的源码保存为文件
FileInputStream in = new FileInputStream(new File("Data/d.html"));
FileOutputStream out = new FileOutputStream(new File("Data/d1.html"));
int i = 0;
byte [] bt = new byte[1024];
while((i=in.read(bt))!=-1){
out.write(bt,0,i);
}
out.close();
in.close();
System.out.println("success");
}
}
标签:fileinput fileread 代码 system throw close 冗余 源码 ade
原文地址:https://www.cnblogs.com/csj2018/p/9534743.html