JAVA编程·Eclipse

编写一程序,如有a.txt文档,该程序功能有两点:⒈可以把a.txt文档里俩们的内容全部复制到另一个文档(名字自定义,如xx.txt),如果有XX.txt文档,则提示“该文件已经存在,是否覆盖?”点“Yes”为覆盖,点“No”则取消!如果没有a.txt文档,则提示a.txt文档不存在!⒉该程序还可以复制图片,如a.jpg!要求跟上一功能一样,就是能够复制txt和jpg类型的文件!星期四中午之前要用!谢谢哥哥姐姐帮忙!
各位帮帮忙。快点写出来啊!就是复制文本文档和图片的功能的小程序啊!

第1个回答  2009-05-20
public final class AccessTextFile {

public void readToBuffer(StringBuffer buffer, InputStream is)
throws IOException {
String line; // 用来保存每行读取的内容
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
line = reader.readLine(); // 读取第一行
while (line != null) { // 如果 line 为空说明读完了
buffer.append(line); // 将读到的内容添加到 buffer 中
buffer.append("\n"); // 添加换行符
line = reader.readLine(); // 读取下一行
}
}

public void writeFromBuffer(StringBuffer buffer, OutputStream os) {

PrintStream ps = new PrintStream(os);
ps.print(buffer.toString());
}

public void copyStream(InputStream is, OutputStream os) throws IOException {

String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(os));
line = reader.readLine();
while (line != null) {
writer.println(line);
line = reader.readLine();
}
writer.flush(); // 最后确定要把输出流中的东西都写出去了
// 这里不关闭 writer 是因为 os 是从外面传进来的
// 既然不是从这里打开的,也就不从这里关闭
// 如果关闭的 writer,封装在里面的 os 也就被关了
}

public void copyTextFile(String inFilename, String outFilename)
throws IOException {
// 先根据输入/输出文件生成相应的输入/输出流
InputStream is = new FileInputStream(inFilename);
OutputStream os = new FileOutputStream(outFilename);
copyStream(is, os); // 用 copyStream 拷贝内容
is.close(); // is 是在这里打开的,所以需要关闭
os.close(); // os 是在这里打开的,所以需要关闭
}

public static void main(String[] args) throws IOException {
int sw = 1; // 三种测试的选择开关
AccessTextFile test = new AccessTextFile();

switch (sw) {
case 1: // 测试读
{
InputStream is = new FileInputStream("E:\\test.txt");
StringBuffer buffer = new StringBuffer();
test.readToBuffer(buffer, is);
System.out.println(buffer); // 将读到 buffer 中的内容写出来
is.close();
break;
}
case 2: // 测试写
{
StringBuffer buffer = new StringBuffer("Only a test\n");
test.writeFromBuffer(buffer, System.out);
break;
}
case 3: // 测试拷贝
{
test.copyTextFile("E:\\test.txt", "E:\\r.txt");
}
break;
}
}

}
第2个回答  2009-05-20
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ReadPic {
public static void main(String[] args) {
try {

FileInputStream fis=new FileInputStream("./pic/a.jpg");//原图片
FileOutputStream fos = new FileOutputStream ("./pic/temp.jpg");//目标图片
int read=fis.read();
while(read!=-1){
fos.write(read);
read=fis.read();
}
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}//可以满足你的部分要求本回答被提问者采纳
第3个回答  2009-05-19
有吗?
相似回答