高手帮忙啊,编制一个程序,对两个已经存在的文件实现复制功能。

wangqingyu0088 2007-12-18 10:00:52
编制一个程序,对两个已经存在的文件实现复制功能
编制一个程序,完成多名学生的成绩输入。
初学者,请高手指点
...全文
242 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wusilou 2007-12-19
  • 打赏
  • 举报
回复
package myjava;
import java.io.*;
public class CopyFile
{
public static void main(String[] args)
{
Copy("/b.txt","/a.txt");
}
public static void Copy(String from_name,String to_name)
{
File from_file=new File("/b.txt");
File to_file=new File("/a.txt");
if(!from_file.exists())
System.out.println("the from_file do not exists!");
if(!from_file.isFile())
System.out.println("from_file is not a file!");
if(!from_file.canRead())
System.out.println("the from_file can not read!");
if(!to_file.isDirectory())
to_file=new File(to_file,from_file.getName());
if(to_file.exists())
{
if(!to_file.canWrite())
{
try
{
System.out.println("the file is unwriteable!");
}
catch(Exception e1)
{
System.out.println(e1.getMessage());
}
}
}
FileInputStream from=null;
FileOutputStream to=null;
try
{
from=new FileInputStream(from_file);
to=new FileOutputStream(to_name);
byte[] buffer=new byte[4096];
int bytes_read;
while((bytes_read=from.read(buffer))!=-1)to.write(buffer,0,bytes_read);
}
catch(Exception e2)
{
System.out.println(e2.getMessage());
}
finally
{
if(from!=null)
try
{
from.close();
}
catch(IOException err)
{
System.out.println(err.getMessage());
}
if(to!=null)
{
try
{
to.close();
}
catch(IOException err1)
{
System.out.println(err1.getMessage());
}
}
}
}
}
把b.txt文件中的内容复制到a.txt,删了很多,易于理解点!
wusilou 2007-12-19
  • 打赏
  • 举报
回复
package myjava;
import java.io.*;
public class CopyFile
{
public static void main(String[] args)
{
Copy("/b.txt","/a.txt");
}
public static void Copy(String from_name,String to_name)
{
File from_file=new File("/b.txt");
File to_file=new File("/a.txt");
if(!from_file.exists())
System.out.println("the from_file do not exists!");
if(!from_file.isFile())
System.out.println("from_file is not a file!");
if(!from_file.canRead())
System.out.println("the from_file can not read!");
if(!to_file.isDirectory())
to_file=new File(to_file,from_file.getName());
if(to_file.exists())
{
if(!to_file.canWrite())
{
try
{
System.out.println("the file is unwriteable!");
System.out.println("Overwrite existing file"+to_file.getName()+"?(Y/N):");
System.out.flush();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String response=in.readLine();
if(!response.equals("Y")&&!response.equals("y"))
System.out.println("existsing file was not overwrten");
}
catch(IOException e1)
{
System.out.println(e1.getMessage());
}
}
}
else
{
String parent=to_file.getParent();
if(parent==null)
parent=System.getProperty("user.dir");
File dir=new File(parent);
if(!dir.exists())
System.out.println("the file do not exists"+parent);
if(!dir.isFile())
System.out.println("dir is not a file"+parent);
if(!dir.canWrite())
System.out.println("dir is unwriteable!"+parent);
}
FileInputStream from=null;
FileOutputStream to=null;
try
{
from=new FileInputStream(from_file);
to=new FileOutputStream(to_name);
byte[] buffer=new byte[4096];
int bytes_read;
while((bytes_read=from.read(buffer))!=-1)to.write(buffer,0,bytes_read);
}
catch(Exception e2)
{
System.out.println(e2.getMessage());
}
finally
{
if(from!=null)
try
{
from.close();
}
catch(IOException err)
{
System.out.println(err.getMessage());
}
if(to!=null)
{
try
{
to.close();
}
catch(IOException err1)
{
System.out.println(err1.getMessage());
}
}
}
}
}

复制的,不知道是不是你要的!mark!
victoryzll 2007-12-19
  • 打赏
  • 举报
回复
都当成2进制读取 好些
newflypig 2007-12-19
  • 打赏
  • 举报
回复
lz说是文件拷贝,可是如果不是txt文件,ls这样的方法能正常使用吗
zhitaorao 2007-12-19
  • 打赏
  • 举报
回复
package com.util;

import java.io.*;

public class DealFile {
/** 输入文件 */
String infile = "";

/** 输出文件 */
String outfile = "";

/** 输入文件流 */
FileInputStream fis = null;

/** 输出文件流 */
FileOutputStream fos = null;

public DealFile() {
}

/**
* 建立输入文件流, 通过参数中指定的文件路径 fis = new FileInputStream(infile);
*/
public void connFIS(String i) {
try {
infile = i;
fis = new FileInputStream(infile);
} catch (IOException ioe) {
System.out.println("调用DealFile.connFIS()函数错误:\r\n" + ioe);
}
}

/**
* 建立输出文件流,通过参数中指定的文件路径 fis = new FileInputStream(outfile);
*/
public void connFOS(String o) {
try {
outfile = o;
fos = new FileOutputStream(outfile);
} catch (IOException ioe) {
System.out.println("调用DealFile.connFOS()函数错误:\r\n" + ioe);
}
}

/**
* 关闭输入文件流
*/
public void closeFIS() {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ioe) {
System.out.println("调用DealFile.closeFIS()函数错误:\r\n" + ioe);
}
}

/**
* 关闭输出文件流
*/
public void closeFOS() {
try {
if (fos != null) {
fos.close();
}
} catch (IOException ioe) {
System.out.println("调用DealFile.closeFOS()函数错误:\r\n" + ioe);
}
}

/**
* 取得输入流
*/
public FileInputStream getFIS() {
return fis;
}

/**
* 取得输出流
*/
public FileOutputStream getFOS() {
return fos;
}

/**
* 删除文件, df是指字的文件名
*/
public void deleteFile(String df) {
File file = new File(df);
file.delete();
}

/**
* 拷贝文件内容fis->fos
*/
public void movefile_FileStream() {
try {
File f = new File(infile);
byte b[] = new byte[(int) (f.length())];
fis.read(b);
fos.write(b);
} catch (IOException ioe) {
System.out.println("调用DealFile.movefile_FileStream()函数错误:\r\n"
+ ioe);
}
}

/**
* 拷贝文件内容fis->fos
*/
public void movefile_BufferedByteStream() {
try {
BufferedInputStream in = new BufferedInputStream(fis);
BufferedOutputStream out = new BufferedOutputStream(fos);
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
in.close();
out.close();
} catch (IOException ioe) {
System.out
.println("调用DealFile.movefile_BufferedByteStream()函数错误:\r\n"
+ ioe);
}
}

/**
* 拷贝文件内容infile->outfile
*/
public void movefile_BufferedCharStream() {
try {
BufferedReader in = new BufferedReader(new FileReader(infile));
BufferedWriter out = new BufferedWriter(new FileWriter(outfile));
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
in.close();
out.close();
} catch (IOException ioe) {
System.out
.println("调用DealFile.movefile_BufferedCharStream()函数错误:\r\n"
+ ioe);
}
}

/**
* 读中文字符串infile->
*/
public String readCHStr() {
return readCHStr(fis);
}

/**
* 读中文字符串infile->
*/
public String readCHStr(InputStream is) {
String str = "";
try {
// 建立Unicode字符流
InputStreamReader isw = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isw);
// 读Unicode字符串
String s = "";
while ((s = br.readLine()) != null) {
if (!str.equals("")) {
str = str + "\r\n" + s;
} else {
str = str + s;
}
}
br.close();
isw.close();
} catch (IOException ioe) {
System.out.println("调用DealFile.readCHStr()函数错误:\r\n" + ioe);
}
return str;
}

/**
* 将字符串转换为数据流
*/
public FileInputStream toInputStream(String str) {
FileInputStream fis_t = null;
try {
// 将字符串写入临时文件,再从文件生成数据流
FileOutputStream fos_t = new FileOutputStream("tmp.txt");
writeCHStr(fos_t, str);
fis_t = new FileInputStream("tmp.txt");
} catch (IOException ioe) {
System.out.println("调用DealFile.toInputStream()函数错误:\r\n" + ioe);
}
return fis_t;
}

/**
* 写中文字符串到文件->outfile
*/
public void writeCHStr(String str) {
writeCHStr(fos, str);
}

/**
* 写中文字符串到文件->outfile
*/
public void writeCHStr(OutputStream os, String str) {
try {
// 建立Unicode字符流
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
// 写Unicode字符串
bw.write(str, 0, str.length());
bw.newLine();
bw.close();
osw.close();
} catch (IOException ioe) {
System.out.println("调用DealFile.writeCHStr()函数错误:\r\n" + ioe);
}
}

/**
* 追加到文件结尾
*/
public void appendCHStr(String outfile, String str) {
try {
RandomAccessFile rf = new RandomAccessFile(outfile, "rw");
rf.seek(rf.length());
rf.writeBytes(str);
rf.close();
} catch (IOException ioe) {
System.out.println("调用DealFile.appendCHStr()函数错误:\r\n" + ioe);
}
}

/**
* 从位置cur开始检索第一个str的位置
*/
public long seekStrPos(long cur, String str) {
long fcur = cur;
try {
RandomAccessFile file = new RandomAccessFile(new File(infile), "r");
long flen = file.length();
int slen = str.length();
byte b[] = new byte[slen];

for (; fcur < flen; fcur++) {
file.seek(fcur);
// 文件尾剩余长度不再够时
if ((flen - fcur) < slen) {
fcur = -1;
break;
}
// 判断当前位置读取的字符串是否与参数字符串匹配,不是则继续搜索
file.read(b, 0, slen);
String bstr = new String(b);
if (str.equals(bstr)) {
break;
}
}
file.close();
} catch (IOException ioe) {
System.out.println("调用DealFile.seekStrPos()函数错误:\r\n" + ioe);
}
return fcur;
}

/**
* 在文件中定位某一个字符串在文件中的位置(左起)
*/
public long seekStrPos(String str) {
return seekStrPos(0, str);
}

/**
* 在文件中定位从字符串str1开始, 第一个字符串str2的位置
*/
public long seekStrPos(String str1, String str2) {
long cur = seekStrPos(0, str1);
return seekStrPos(cur, str2);
}

/**
* 从文件的pos位置开始, 取长度为len的字符串
*/
public String substring(long pos, int len) {
String str = "";
try {
RandomAccessFile file = new RandomAccessFile(new File(infile), "r");
long flen = file.length();
// 当不能返回时返回空值
if (pos < 0 || (flen - pos) < len) {
return "";
}
file.seek(pos);
byte b[] = new byte[len];
file.read(b, 0, len);
str = new String(b);
file.close();
} catch (IOException ioe) {
System.out.println("调用DealFile.substring()函数错误:\r\n" + ioe);
}
return str;
}

/**
* 从字符串str1开始 , 检索str2后, 长度为len的字符串
*/
public String substring(String str1, String str2, int len) {
long pos = seekStrPos(str1, str2);
return substring(pos + str2.length(), len).trim();
}

public static void main(String args[]) throws IOException,
ArrayIndexOutOfBoundsException {
DealFile df = new DealFile();

// *追加文件方法(一旦作为输出文件被打开,即被清空)
df.connFIS("out.txt");
String str = df.readCHStr() + "测试追加WWWWWWWWTTTTW我的还";

df.connFOS("out.txt");
df.writeCHStr(str);
df.closeFIS();
df.closeFOS();
// 拷贝文件
df.connFIS("out.txt");
df.connFOS("in.txt");
df.movefile_FileStream();
df.movefile_BufferedByteStream();
df.movefile_BufferedCharStream();
df.closeFIS();
df.closeFOS();
df.connFIS("out.txt");
System.out.println(df.substring("flyline", "background-color:", 7)
.toUpperCase());
df.closeFIS();
}
}
goodsong 2007-12-19
  • 打赏
  • 举报
回复
文件复制调用系统的copy函数阿,不是更简单
chaorenwopashei 2007-12-19
  • 打赏
  • 举报
回复
我怎么连文件都找不到啊,我放到项目里面了
weity 2007-12-19
  • 打赏
  • 举报
回复
使用Spring框架的文件拷贝实例

package com.weity.Spring.test.Sample;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.OutputStream;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.FileCopyUtils;

public class FileCopyUtilsExample {

public static void main(String[] args) throws Throwable {
Resource res = new ClassPathResource("file1.txt");

byte[] fileData = FileCopyUtils.copyToByteArray(res.getFile());

String fileStr = FileCopyUtils.copyToString(new FileReader(res.getFile()));

FileCopyUtils.copy(res.getFile(),
new File(res.getFile().getParent()+ "/file2.txt"));


OutputStream os = new ByteArrayOutputStream();
FileCopyUtils.copy(res.getInputStream(), os);
}

}

shadow55 2007-12-19
  • 打赏
  • 举报
回复
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStream;

public class FileCopy {

/**
* @param args
* @throws Exception
*/

public static void fileCopy(String file_path,File file) throws Exception{
FileReader f = new FileReader(file_path);
BufferedReader buff = new BufferedReader(f);
String data;
OutputStream out = new FileOutputStream(file);
while((data = buff.readLine()) != null){
byte[] b = data.getBytes();
out.write(b, 0, b.length);
out.flush();
}
out.close();
}


public static void main(String[] args) throws Exception {
FileCopy.fileCopy("d:/resume.xml", new File("d:/as.doc"));
}

}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧