如果用字节流来操作的话(不需要编码了- -):public static void copy4(){ InputStream is = null; OutputStream os = null; try { is = new FileInputStream("C:\\Documents and Settings\\Administrator\\桌面\\22.txt");//目前这个地方报错,应该如何修改呢 os = new FileOutputStream("C:\\Documents and Settings\\Administrator\\桌面\\11.txt");//目前这个地方报错,应该如何修改呢 byte[] buffer = new byte[1024]; int len = 0; while(-1 != (len = is.read(buffer))){ os.write(buffer, 0, len); os.flush(); }
你应该看看Reader和InputStream的区别。
建议看一下java io流的类库结构,不过这一段确实比较繁琐
但是新的问题又出来了,代码如下public static void copy3(){
InputStreamReader frs=null;
OutputStreamWriter fws=null;
try {
frs=new InputStreamReader(new BufferedInputStream(new FileInputStream("C:\\Documents and Settings\\Administrator\\桌面\\22.txt")),"UTF-8");
fws=new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream("C:\\Documents and Settings\\Administrator\\桌面\\11.txt")),"UTF-8");
byte[] bt=new byte[1024];
int len=0;
while((len=frs.read(bt))!=-1){
fws.write(bt,0,len);
}
} catch (Exception e) {
throw new RuntimeException("读取错误");
}finally{
try {
if(frs!=null)
frs.close();
} catch (Exception e2) {
throw new RuntimeException("读错误");
}
try {
if(fws!=null)
fws.close();
} catch (Exception e3) {
throw new RuntimeException("写错误");
}
}
}
while((len=frs.read(bt))!=-1){ //这个地方报错
fws.write(bt,0,len);// //这个地方报错
}
建议lz再把字节流和字符流看看
我帮你改了下package com.tiantian.learn;import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;/**
* @author wangjie
* @version 创建时间:2013-1-17 下午7:09:37
*/
public class MyTest {
public static void main(String[] args) {
copy3();
}
public static void copy3(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Documents and Settings\\Administrator\\桌面\\22.txt"), "utf-8"));//目前这个地方报错,应该如何修改呢
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\Documents and Settings\\Administrator\\桌面\\11.txt"), "utf-8"));//目前这个地方报错,应该如何修改呢
String line = "";
while(null != (line = br.readLine())){
bw.write(line);
bw.newLine();
System.out.println(line);
bw.flush();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}
如果用字节流来操作的话(不需要编码了- -):public static void copy4(){
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream("C:\\Documents and Settings\\Administrator\\桌面\\22.txt");//目前这个地方报错,应该如何修改呢
os = new FileOutputStream("C:\\Documents and Settings\\Administrator\\桌面\\11.txt");//目前这个地方报错,应该如何修改呢
byte[] buffer = new byte[1024];
int len = 0;
while(-1 != (len = is.read(buffer))){
os.write(buffer, 0, len);
os.flush();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
fws=new BufferedOutputStream(new OutputStreamWriter(new FileOutputStream("C:\\Documents and Settings\\Administrator\\桌面\\11.txt"),"UTF-8"));//目前这个地方报错,应该如何修改呢确实这两句错了,应该是frs=new BufferedReader()
fws=new BufferedWriter()
给分,谢谢