java 输入输出流的复制文件的问题

import java.io.*;
public class fuji
{

public static void main(String arg[])

{
int shuru,shuchu;//记录是否输入输出完成
byte ru[]=new byte[2000];//存放读取的内容
byte chu[]=new byte[2000];
//读入内容
try
{
FileInputStream in1=new FileInputStream("E:\\java\\test.java");
if((shuru=in1.read(ru,0,2000))==-1)
System.out.println("读取文件结束!");//可不知道怎么才能为读完
System.out.println("读取完成!");
}
catch (Exception e)
{
String err=e.toString();
System.out.println(err+" 读入出错!");
}

//复制内容
try
{
FileOutputStream out1=new FileOutputStream("1.text");
//if((shuchu=out1.write(ru,0,2000))==-1)
//{
out1.write(ru,0,2000);//这里的2000要小于ru数组的大小。
out1.close();
System.out.println("复制成功!");
// }
}
catch (Exception e)
{
String err=e.toString();
System.out.println(e+"复制失败!");
}

}
}

用什么方法能确定我读完了E:\\java\\test.java文件的内容呢.
或者说该怎么处理才能刚好用一个语句来读完test.java文件里的内容.

可以通过“BufferedReader”读取文件内容,之后通过OutputStreamWriter 写入新文件,举例:
OutputStreamWriter pw = null;//定义一个流
pw = new OutputStreamWriter(new FileOutputStream(“D:/testCopy.txt”),"GBK");//确认流的输出文件和编码格式,此过程创建了“test.txt”
bre = new BufferedReader(new FileReader("D:/test.txt"));//此时获取到的bre就是整个文件的缓存流
while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环
{
pw.write(str );//将要写入文件的内容,可以多次write
};
bre .close();//关闭流
pw.close();//关闭流
备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-10-17
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

public class fuji {

public static void main(String arg[])
{
//文件复制最好用完整一点的输入输出流,InputStreamReader将字节流变为字符流的桥梁。
//然后BufferedReader以行为单位(readLine()方法)把信息读成字符串。在java中一行为空
//或者有空格,并不代表这一行是null。只有当文件读完时,readLine()才会返回为null。所以
//用这个来判断是否读完。
//用字节来读取的话,逻辑不够严密,因为你不知道该文件具体要多少个字节来存放。

// int shuru, shuchu;// 记录是否输入输出完成
// byte ru[] = new byte[2000];// 存放读取的内容
// byte chu[] = new byte[2000];

StringBuffer sb = new StringBuffer();
String content = null;
// 读入内容
try
{
FileInputStream in1 = new FileInputStream("E:\\java\\test.java");
InputStreamReader isr = new InputStreamReader(in1);
BufferedReader br = new BufferedReader(isr);

while (true)
{
String str = br.readLine();
if (null == str)
{
break;
}
sb.append(str + "\n");
}
content = sb.toString();
System.out.println("读取完成!");
} catch (Exception e) {
String err = e.toString();
System.out.println(err + " 读入出错!");
}

// 复制内容
try
{
FileOutputStream out1 = new FileOutputStream("E:\\1.text");
//第一种实现的方法,但输入到1.text中格式无法保证。
// method1(content,out1);
PrintStream ps = new PrintStream(out1);

//为了保持格式,还是用第二种方法吧,虽然麻烦一点点。
method2(content,ps);
// out1.write(ru, 0, 2000);// 这里的2000要小于ru数组的大小。
// out1.close();
System.out.println("复制成功!");
// }
} catch (Exception e) {
String err = e.toString();
System.out.println(e + "复制失败!");
}
}

//方法一,可以实现,但不保证格式。
public static void method1(String content,FileOutputStream fos) throws IOException
{
byte[] b;
try
{
b = content.getBytes("GBK");
fos.write(b,0,b.length);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
}

//方法二,保持原格式。其实应该是读一行,就复制一行容易实现,但为了风格统一只有写在这里了。
public static void method2(String content,PrintStream ps)
{
String[] str = content.split("\\n");
for (int i = 0; i < str.length; i ++)
{
ps.println(str[i]);
}
}
}
第2个回答  推荐于2017-09-29
import java.io.*;

class UseIOj{
public static void main(String [] args){
File f1 = new File("E:/1.jpg");
File f2 = new File("D:/2.jpg");
BufferedInputStream in = null;
PrintStream out = null;
byte[] b = new byte[1024];
long i = 0L;
try{
in = new BufferedInputStream(new FileInputStream(f1));
out =new PrintStream(new BufferedOutputStream(new FileOutputStream(f2)));
while((i = in.read(b,0,b.length)) != -1){
System.out.println(i);
out.write(b);
}

}
catch(IOException io){

}
finally{
try{
if(in != null){
in.close();
}
if(out != null){
out.close();
}
}
catch(IOException io){

}

}
}

}

自带了方法 你可能不知道 查下API吧 我这个是好使的 直接就能用 你可以看一下

最好是用BUFFER系列的来读本回答被提问者和网友采纳
第3个回答  2008-10-16
你这样打出来似乎和问题一点关系都没有

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网