IO流
字节流的读取和写入
import java.io.*;
//字节 流
public class test {
public static void main(String[] args) {
try {
FileInputStream f1= new FileInputStream("d:\\2.jpg");
FileOutputStream f2= new FileOutputStream("d:\\3.jpg");
BufferedInputStream b1=new BufferedInputStream(f1);
BufferedOutputStream b2=new BufferedOutputStream(f2);
byte [] b=new byte[1024];
int l=0;
try {
if (((l=b1.read())!=-1)) {
b2.write(b,0,l);
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
字符流的读取和写入
import java.io.*;
public class t2 {
//字符
public static void main(String[] args) {
BufferedReader b1=null;
BufferedWriter b2=null;
try {
b1=new BufferedReader(new FileReader("D:/ddd.txt"));//读取文件的地址,
b2=new BufferedWriter(new FileWriter("D:/adc.txt"));//写入文件的地址
String i="";
while ((i=b1.readLine())!=null){
b2.write(i);
b2.newLine();
}
b2.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}
字节流向字符流的转换。
import java.io.*;
//字节转字符
public class t1 {
public static void main(String[] args) {
// System.out.println(System.getProperty("file.encoding"));
BufferedReader br=null;
BufferedWriter bw=null;
br=new BufferedReader(new InputStreamReader(System.in));
try {
bw=new BufferedWriter(new FileWriter(new File("D:/dd.txt"),true));
String temp=null;
while ((temp=br.readLine())!=null){
if (temp.equals("quit")){
break;
}
bw.write(temp);
bw.newLine();
}
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}