1.简述java流的分类
字节流:输入流InputStream和输出流OutputStream
字符流:输入流Reader 和输出流Writer
2.异常:
FileNotFoundException(文件找不到)和IOException
3.
package com.bdqn7;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class zuoye1 {
public static void main(String[] args) {
BufferedReader br=null;
BufferedWriter wr=null;
FileReader fr;
FileWriter fw;
try {
fr = new FileReader("c:\\source.txt");
fw = new FileWriter("d:\\targer.txt");
br=new BufferedReader(fr);
wr=new BufferedWriter(fw);
String line=null;
StringBuffer sbf=new StringBuffer();
//循环追加
while((line=br.readLine())!=null){
sbf.append(line);
}
wr.write(sbf.toString());//写入文件
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(wr!=null){
try {
wr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
package com.bdqn7;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class zuoye2 {
public static void main(String[] args) {
FileInputStream fis = null;
DataInputStream dis = null;
try {
//创建输出流对象
fis = new FileInputStream("C:\\Windows\\win.ini");
dis = new DataInputStream(fis);
//创建输入流对象
/* int temp;
//读取文件并写入文件
while ( (temp = dis.read()) != -1) {
System.out.print((char)temp);
}*/
byte words[]=new byte[1024];
int len;
if((len=dis.read(words))!=-1){
System.out.println(new String(words,0,len));
}
}catch (FileNotFoundException e) {
System.out.println("无此文件");
}catch (IOException ioe) {
ioe.printStackTrace();
}finally{
try {
if(fis!=null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}