BufferedReader bufReader = new BufferedReader(new FileReader("D:\\books.xml"));
BufferedWriter bufWriter = new BufferedWriter(new FileWriter("D:\\books.xml"));
String s = null;
while((s=bufReader.readLine())!=null){
System.out.println(s);
bufWriter.write(s+"\n");
}
bufReader.close();
bufWriter.close();
Reader reader = new FileReader("D:\\books.xml");
BufferedReader bufReader = new BufferedReader(reader);
Writer writer = new FileWriter("D:\\books.xml");
BufferedWriter bufWriter = new BufferedWriter(writer);
String s = null;
while((s=bufReader.readLine())!=null){
System.out.println(s);
bufWriter.write(s+"\n");
}
bufReader.close();
bufWriter.close();
按字符读写
BufferedReader bufReader = new BufferedReader(new FileReader("D:\\books.xml"));
BufferedWriter bufWriter = new BufferedWriter(new FileWriter("D:\\books1.xml"));
int c;
while((c=bufReader.read())!=-1){
System.out.print((char)c);
bufWriter.write((char)c);
}
bufReader.close();
bufWriter.close();
按固定大小读写
BufferedReader bufReader = new BufferedReader(new FileReader("D:\\books.xml"));
BufferedWriter bufWriter = new BufferedWriter(new FileWriter("D:\\books1.xml"));
char[]cbuf = new char[50];
int len;
while((len=bufReader.read(cbuf))!=-1){
for(int i=0;i<len;i++)
System.out.print(cbuf[i]);
bufWriter.write(cbuf,0,len);
}
bufReader.close();
bufWriter.close();
注意,为什么不可以直接用bufWriter.write(cbuf)写文件,为什么不可以直接用System.out.print(cbuf)输出文件内容,原因是,read(cbuf)这个方法在向字符数组写字符的时候,并没有把原来的数据删掉,而是直接在上面覆盖,所以,最后一趟读数据时,如果字符个数不是刚好50个,那么数组后面的字符依然还在,这时cbuf的数据包含了本次读出的数据和前一次读数据的一部分,当然你也可以每次读数据之前把cbuf清空或新建一个数组,但后面还是要判断读出的数据长度,否则null会作为一个字符写进文件。
FileInputStream fis = new FileInputStream("D:\\test.jpg");
FileOutputStream fos = new FileOutputStream("D:\\test1.jpg");
int n = fis.available();
byte[]file = new byte[n];
fis.read(file);
fos.write(file);
fis.close();
fos.close();
按字节读取
FileInputStream fis = new FileInputStream("D:\\test.jpg");
FileOutputStream fos = new FileOutputStream("D:\\test1.jpg");
int c;
while((c=fis.read())!=-1){
fos.write(c);
}
fis.close();
fos.close();
FileInputStream fis = new FileInputStream("D:\\test.jpg");
FileOutputStream fos = new FileOutputStream("D:\\test1.jpg");
byte []b = new byte[128];
int len;
while((len=fis.read(b))!=-1){
fos.write(b,0,len);
}
fis.close();
fos.close();
可对读取到的字符数据经过指定编码转换成字节
OutputStreamWriter: 字符到字节的桥梁
FileInputStream fis = new FileInputStream("D:\\books.xml");
Reader reader = new InputStreamReader(fis,"UTF-8");
BufferedReader bufReader = new BufferedReader(reader);
BufferedWriter bufWriter = new BufferedWriter(new FileWriter("D:\\books1.xml"));
String s = null;
while((s=bufReader.readLine())!=null){
System.out.println(s);
bufWriter.write(s+"\n");
}
bufReader.close();
bufWriter.close();
BufferedReader bufReader = new BufferedReader(new FileReader("D:\\books.xml"));
FileOutputStream fos = new FileOutputStream("D:\\books1.xml");
Writer writer = new OutputStreamWriter(fos,"UTF-8");
BufferedWriter bufWriter = new BufferedWriter(writer);
String s = null;
while((s=bufReader.readLine())!=null){
System.out.println(s);
bufWriter.write(s+"\n");
}
bufReader.close();
bufWriter.close();
同时指定读写编码:
上面两者结合便可