标签:out java io while 不包含 文本 fileread 必须 跨平台 数据
IO即输入与输出,Java把不同来源和目标的数据抽象为流,在此基础上对流进行数据操作
顶层父类 | 描述 |
---|---|
InputStrream | 输入字节流 |
OutputStream | 输出字节流 |
Reader | 输入字符流 |
Writer | 输出字符流 |
demo
import java.io.*;
public class IoTest {
public static void main(String[] args) throws IOException {
FileInputStream fi=new FileInputStream("a.txt");
FileOutputStream fo=new FileOutputStream("b.txt");
int len;
byte[] buffer=new byte[1024*8];//缓冲区大小
while ((len=fi.read(buffer))!=-1)//read()返回-1表示到结尾,返回正数表示读到的有效字节数
fo.write(buffer,0,len);
fi.close();
fo.close();
}
}
FileOutputStream的追加模式
FileOutputStream(File file, boolean append)
//创建文件输出流,将append设置为1即为追加模式
demo
import java.io.*;
public class IoTest {
public static void main(String[] args) throws IOException {
BufferedInputStream fi=new BufferedInputStream(new FileInputStream("a.txt"));
BufferedOutputStream fo=new BufferedOutputStream(new FileOutputStream("b.txt"));
int temp;
while ((temp=fi.read())!=-1)//read()返回-1表示到结尾
fo.write(temp);
fi.close();
fo.close();
}
}
默认缓冲区大小是8192字节
private static int DEFAULT_BUFFER_SIZE = 8192;
flush和close方法的区别
序列流可以把多个字节输入流整合成一个输入流, 从序列流中读取数据时, 按照整合顺序一一读取
整合两个字节流
FileInputStream fis1 = new FileInputStream("a.txt"); //创建输入流对象,关联a.txt
FileInputStream fis2 = new FileInputStream("b.txt"); //创建输入流对象,关联b.txt
SequenceInputStream sis = new SequenceInputStream(fis1, fis2); //将两个流整合成一个流
FileOutputStream fos = new FileOutputStream("c.txt"); //创建输出流对象,关联c.txt
int b;
while((b = sis.read()) != -1) { //用整合后的读
fos.write(b); //写到指定文件上
}
sis.close();
fos.close();
整合多个字节流
FileInputStream fis1 = new FileInputStream("a.txt"); //创建输入流对象,关联a.txt
FileInputStream fis2 = new FileInputStream("b.txt"); //创建输入流对象,关联b.txt
FileInputStream fis3 = new FileInputStream("c.txt"); //创建输入流对象,关联c.txt
Vector<InputStream> v = new Vector<>(); //创建vector集合对象
v.add(fis1); //将流对象添加
v.add(fis2);
v.add(fis3);
Enumeration<InputStream> en = v.elements(); //获取枚举引用
SequenceInputStream sis = new SequenceInputStream(en); //传递给SequenceInputStream构造
FileOutputStream fos = new FileOutputStream("d.txt");
int b;
while((b = sis.read()) != -1) {
fos.write(b);
}
sis.close();
fos.close();
FileInputStream fi=new FileInputStream("a.txt");
ByteOutputStream bo=new ByteOutputStream();
int temp;
while ((temp=fi.read())!=-1)//read()返回-1表示到结尾
bo.write(temp);//将字节写入内存
System.out.println(bo);//将内存中的字节转换为字符再打印
fi.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.txt"));
Person p1 = (Person) ois.readObject();
Person p2 = (Person) ois.readObject();
System.out.println(p1);
System.out.println(p2);
ois.close();
FileReader fr = new FileReader("aaa.txt");//创建字符输入流,关联aaa.txt
FileWriter fw = new FileWriter("bbb.txt");//创建字符输出流,关联bbb.txt
int len;
char[] arr = new char[1024*8];//创建字符数组
while((len = fr.read(arr)) != -1) {//将数据读到字符数组中
fw.write(arr, 0, len);//从字符数组将数据写到文件上
}
fr.close();//关流释放资源
fw.close();
BufferedReader br = new BufferedReader(new FileReader("aaa.txt"));//创建字符输入流对象,关联aaa.txt
BufferedWriter bw = new BufferedWriter(new FileWriter("bbb.txt"));//创建字符输出流对象,关联bbb.txt
int ch;
while((ch = br.read()) != -1) {//read一次,会先将缓冲区读满,从缓冲去中一个一个的返给临时变量ch
bw.write(ch);//write一次,是将数据装到字符数组,装满后再一起写出去
}
br.close();//关流
bw.close();
readLine()和newLine()方法
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt"));
String line;
while((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();//跨平台的
}
br.close();
bw.close();
InputStreamReader isr=new InputStreamReader(new FileInputStream("a.txt"),"utf-8");//按照urf-8读a.txt
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("b.txt"),"gbk");//按照gbk写入到b.txt
int temp;
while ((temp=isr.read())!=-1)//read()返回-1表示到结尾
osw.write(temp);
isr.close();
osw.close();
LineNumberReader lnr = new LineNumberReader(new FileReader("a.txt"));
String line;
lnr.setLineNumber(100);//设置行号,从101开始打印,但还是对应第一行
while((line = lnr.readLine()) != null) {
System.out.println(lnr.getLineNumber() + ":" + line);//获取行号
}
lnr.close();
FileInputStream fis =null;
FileOutputStream fos =null;
try(
fis = new FileInputStream("aaa.txt");
fos = new FileOutputStream("bbb.txt");
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
)finally{
try{
if(fis!=null)//输入流如果打开了
fis.close;
}finally{
if(fos!=null)//输出流如果打开了
fos.close;
}
}
try(
FileInputStream fis = new FileInputStream("aaa.txt");
FileOutputStream fos = new FileOutputStream("bbb.txt");
){
int b;
while((b = fis.read()) != -1) {
fos.write(b);
}
}
标签:out java io while 不包含 文本 fileread 必须 跨平台 数据
原文地址:https://www.cnblogs.com/redo19990701/p/11580748.html