给你一个拷贝文件的例子:/*
* Created on 2004-9-11
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package copy;import java.io.FileInputStream;
import java.io.FileOutputStream;/**
* @author 崔占民
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Copy
{
final private static int INT_READLEN = 10240; public static void main(String[] args)
{
if (args.length == 0 || (args.length == 1 && !args[0].equals("/?")))
{
System.out.println("command syntax is invalidity!");
return;
}
else if (args[0].equals("/?"))
{
System.out.println("copy SrcFile DesFile");
System.out.println("SrcFile: Source File.");
System.out.println("DesFile: destination File.");
return;
}
Copy cpy = new Copy(args[0], args[1]);
} Copy(String aStrSrcFile, String aStrDesFile)
{
super ();
if (aStrSrcFile.equals("") || aStrDesFile.equals(""))
{
System.out.println("please input SrcFile and DesFile!");
System.out.println("copy SrcFile DesFile");
return;
} FileInputStream in = null;
FileOutputStream out = null;
try
{
int bytes = 0;
byte[] bteFile = new byte[INT_READLEN];
in = new FileInputStream(aStrSrcFile);
out = new FileOutputStream(aStrDesFile); while ((bytes = in.read(bteFile)) != -1)
{
out.write(bteFile, 0, bytes);
}
System.out.println("File Copy finished!");
}
catch (Exception e)
{
System.out.println(e.toString());
}
finally
{
try
{
in.close();
out.close();
}
catch (Exception e)
{
System.out.println(e.toString());
return;
}
}
}
}
* Created on 2004-9-11
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package copy;import java.io.FileInputStream;
import java.io.FileOutputStream;/**
* @author 崔占民
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Copy
{
final private static int INT_READLEN = 10240; public static void main(String[] args)
{
if (args.length == 0 || (args.length == 1 && !args[0].equals("/?")))
{
System.out.println("command syntax is invalidity!");
return;
}
else if (args[0].equals("/?"))
{
System.out.println("copy SrcFile DesFile");
System.out.println("SrcFile: Source File.");
System.out.println("DesFile: destination File.");
return;
}
Copy cpy = new Copy(args[0], args[1]);
} Copy(String aStrSrcFile, String aStrDesFile)
{
super ();
if (aStrSrcFile.equals("") || aStrDesFile.equals(""))
{
System.out.println("please input SrcFile and DesFile!");
System.out.println("copy SrcFile DesFile");
return;
} FileInputStream in = null;
FileOutputStream out = null;
try
{
int bytes = 0;
byte[] bteFile = new byte[INT_READLEN];
in = new FileInputStream(aStrSrcFile);
out = new FileOutputStream(aStrDesFile); while ((bytes = in.read(bteFile)) != -1)
{
out.write(bteFile, 0, bytes);
}
System.out.println("File Copy finished!");
}
catch (Exception e)
{
System.out.println(e.toString());
}
finally
{
try
{
in.close();
out.close();
}
catch (Exception e)
{
System.out.println(e.toString());
return;
}
}
}
}
期待高手提出更好的方法。
//infoName是要修改的关键字的名称;info是infoName的值;
{
try{
String tempString =null; //存放源文件中的readLine读取的内容
String nameTmp = fName+".bak"; //命名.bak文件.
boolean flag = true;//记录文件中是否有关键字的变量; 有则为fales,无则为true;
copy( fName, nameTmp);//将源文件写入备份文件
RandomAccessFile raf = new RandomAccessFile(fName,"rw");
RandomAccessFile tempFile = new RandomAccessFile(nameTmp,"r");
long Pos = tempFile.getFilePointer();
//System.out.println(Pos);
infoName=infoName.trim();
int countLength =0;//记录读取文件的长度; while((tempString = tempFile.readLine())!=null)
{
tempString=tempString.trim();
String myString = tempString;
int length = myString.length();
for(int i =0;i<length;i++)
{
if(myString.charAt(i) == '=') //取得关键字 =号前边的信息
{
myString = myString.substring(0,i);
myString.trim();
break;
}
}
//System.out.println(myString);
if(myString.equals(infoName.trim()))
{
myString += "=" + info;
tempString = "# "+tempString;//把以前的旧信息注释掉,前边加上#号
Pos=WriteInfo(raf,Pos,tempString);
Pos=WriteInfo(raf,Pos,myString);
flag = false;
}
else
{
Pos = WriteInfo(raf,Pos,tempString);
}
}
if(flag)
{
tempString = infoName + "=" + info;
Pos = WriteInfo(raf,Pos,tempString);
}
raf.close();
System.out.println("修改文本文件成功!");
}
catch(Exception e)
{
String err = e.toString();
System.out.println(err);
}
}