1.I/O系统操作的目标是为了对数据进行读写操作
输入:从数据源当中读取数据
//导入类
import java.io.*;
class Test{
public static void main(String args[]){
//声明输入流引用
FileInputStream fis = null;
//声明输出流引用
FileOutputStream fos = null;
try{
//生成代表输入流的对象
fis = new FileInputStream("D:/from.txt");
//生成代表输出流的对象
fos = new FileOutputStream("D:/to.txt");
//生成一个字节数组
byte [] buffer = new byte[100];
//调用输入流的read的方法,输入数据
fis.read(buffer,0,buffer.length);
String s = new String(buffer);
//调用一个String对象的trim方法,将会去除掉这个字符串的首位空格和空字符
s = s.trim();
System.out.println(s);
}
catch(Exception e){
System.out.println(e);
}
}
}输出:将数据写入到数据目的地当中
//导入类
import java.io.*;
class Test{
public static void main(String args[]){
//声明输入流引用
FileInputStream fis = null;
//声明输出流引用
FileOutputStream fos = null;
try{
//生成代表输入流的对象
fis = new FileInputStream("D:/from.txt");
//生成代表输出流的对象
fos = new FileOutputStream("D:/to.txt");
//生成一个字节数组
byte [] buffer = new byte[100];
//调用输入流的read的方法,输入数据
int temp = fis.read(buffer,0,buffer.length);//输入流的返回值为写入的数据长度
fos.write(buffer,0,temp);
}
catch(Exception e){
System.out.println(e);
}
}
}
2.IO的分类
输入流和输出流、字节流和字符流、节点流和处理流
3.核心类的核心方法
InputStream://读取数据
intread(byte[]b,int off,int len)//读进来的数据从b的第off(offside:偏移量)为开始,读取一次最多读取len个数据,返回值为读取数据的字节数
OutputStream://数据写到文件中
voidwrite(byte[]b,int off,int len)//要往文件中写入的数据要先变成比特数据,从b的第off(offside:偏移量)为开始写,一共写入len个数据