IO流基本操作

时间:2023-03-08 17:43:38
IO流基本操作
第一种分法:.输入流2.输出流
第二种分法:.字节流2.字符流
第三种分法:.节点流2.处理流
////////////////////////////////////////////////////////////////////////////字节流: InputStream 的子类是 FileInputStream;
InputStream :int read(byte[]b,int off,int len)从第几位读,读几个,返回读取的个数
OutputStream 的子类是FileOutputStream
OutputStream :void write(byte[]b,int off,int len)
public static void main(String args[]){
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("e:/from.txt");//输入流(从文件读数据,即数据输入到程序里)
out = new FileOutputStream("e:/to.txt");//输出流(写入文件数据,即数据输出到文件里)
byte[] buffer =new byte[];//字节数组,用来存放读取的数据
int num = in.read(buffer, , buffer.length);//从第0位读取,读from.txt文件的 buffer.length个,放进buffer内
out.write(buffer, , num);//read方法返回读取的个数
//out.write(buffer, 0, 2);//从0位开始,把buffer数组的2个字节写进to.txt文件里
for(int i = ;i<buffer.length;i++){
System.out.print(buffer[i]);
}
String s = new String(buffer);//转换成原来的字符
s= s.trim();//trim方法:去掉首尾空格和空字符,中间的空格不去掉
System.out.print(s);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
in.close();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} 读取大文件:
public static void main(String args[]) {
FileInputStream in = null;
FileOutputStream out = null;
int a = ;
try {
in = new FileInputStream("e:/from.txt");//输入流(从文件读数据,即数据输入到程序里)
out = new FileOutputStream("e:/to.txt");//输出流(写入文件数据,即数据输出到文件里)
byte[] buffer =new byte[];//字节数组,用来存放读取的数据
while(true){ a++;
System.out.print(a);
int num = in.read(buffer, , buffer.length);//从第0位读取,读from.txt文件的 buffer.length个,放进buffer内
out.write(buffer, , num);//read方法返回读取的个数
if(num == -){//返回-1,说明读取完毕
break;
}
} } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
in.close();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} ////////////////////////////////////////字符流:读写文件时,以字符为基础
字节输入流:Reader 的子类是FileReader
int read(char [] c,int off,int len)
字节输出流:Writer 的子类是FileWriter
void write(char [] c,int off,int len) public static void main(String args[]) { FileReader reader =null;
FileWriter writer = null;
try{
reader = new FileReader("E:/from.txt");
writer = new FileWriter("e:/to.txt"); char [] buffer = new char[];
int num = reader.read(buffer, , buffer.length);
writer.write(buffer, , num);
for(int i = ;i<num;i++){
System.out.print(buffer[i]);
}
}
catch(Exception e){
System.out.print(e);
}
finally{
try{
reader.close();
writer.close();
}
catch(Exception e){
System.out.print("success!!!");
}
}
}
/////////////////////////////////////////////////一次性读取一行数据
public static void main(String args[]) { FileReader fileReader = null;
BufferedReader bufferedReader = null;
try{
fileReader = new FileReader("e:/from.txt");
bufferedReader = new BufferedReader(fileReader);
String line = null;
while(true){
line = bufferedReader.readLine();
if(line == null){
break;
}
System.out.println(line);
}
}
catch(Exception e){
System.out.println(e); }
finally{
try{
bufferedReader.close();
fileReader.close();
}
catch(Exception e){
System.out.println(e);
} }