这样的代码也不会写?如果我是老板我一定会着急的。:(比如说 C:\\1.dat
FileInputStream fileIn = new FileInputStream("C:\\1.dat");
byte data = new byte[fileIn.avaliable()];
fileIn.read(data);
fileIn.close();FileOutputStream fileOut = new FileOutputStream("C:\\2.dat");
fileOut.write(data);
fileOut.close();当然你也可以一边读一边写。
FileInputStream fileIn = new FileInputStream("C:\\1.dat");
byte data = new byte[fileIn.avaliable()];
fileIn.read(data);
fileIn.close();FileOutputStream fileOut = new FileOutputStream("C:\\2.dat");
fileOut.write(data);
fileOut.close();当然你也可以一边读一边写。
String file = "aa.txt";
String sss = "";
try
{
fff =new RandomAccessFile(file,"rw");
filesize = fff.length();
System.out.println("File size :"+ filesize);
while ( filesize > filepointer)
{
filepointer = fff.getFilePointer();
sss = fff.readLine();
System.out.println("filepointer : "+filepointer+" "+sss);
}
fff.close();
}
catch(FileNotFoundException dd)
{
System.out.println("no file");
}
catch(IOException s)
{
System.out.println("read file error");
}
save to
RandomAccessFile fffd;
String file = "d:\\aa.txt";
try
{
fffd =new RandomAccessFile(file,"rw");
fffd.writeChars(sss) ;
fff.close();
}
catch(FileNotFoundException dd)
{
System.out.println("no file");
}
catch(IOException s)
{
System.out.println("read file error");
}
public static void main(String[] args) throws IOException {
File inputFile = new File("farrago.txt");
File outputFile = new File("outagain.txt"); FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c; while ((c = in.read()) != -1)
out.write(c); in.close();
out.close();
}
}
int len = 0;
FileInputStream input = new FileInputStream(srcfile);
FileOutputStream output = new FileOutputStream(destfile);
try {
while ((len = input.read(bytearray)) != -1) {
output.write(bytearray, 0, len);
}
} catch (FileNotFoundException exc) {
exc.printStackTrace();
} catch (SecurityException exc) {
exc.printStackTrace();
} finally {
input.close();
output.close();
}
import java.io.*;public class jCOPY {
public static void main(String args[]){
try {
jCOPY j = new jCOPY();
j.CopyFile(new File(args[0]),new File(args[1]));
}
catch (Exception e) {
e.printStackTrace();
}
} public void CopyFile(File in, File out) throws Exception {
FileInputStream fis = new FileInputStream(in);
FileOutputStream fos = new FileOutputStream(out);
byte[] buf = new byte[1024];
int i = 0;
while((i=fis.read(buf))!=-1) {
fos.write(buf, 0, i);
}
fis.close();
fos.close();
}
}
import java.io.*;
import java.util.*;
/**
目录文件拷贝(jdk)
@version v1.0 2001-9-27
@auther <a href="mailto:[email protected]">Qixy</a>
*/public class Copy {
/**
Null & simple construct .
*/
public Copy()
{
//void to construct for a class ! }
/**
Application main 使用方法
<p> usage:java Copy [-x(包含子目录)] s_dirorfile(源文件夹/文件) t_dirorfile(目标文件夹/文件)
*/ public static void usage()
{
System.out.println("usage:java Copy -x [s_dirorfile] [t_dirorfile]");
System.exit(0);
return;
}
/**
Application main entrance
@see usage
*/
public static void main(String[] args) throws IOException { /** To be corrected ... */
boolean bx=false;
Copy c=new Copy();
//test ...
File ff=new File("e:\\eeeee\\dfe\\s.x");
System.out.println("ff.getAbsolutePath()="+ff.getAbsolutePath());
System.out.println("ff.getName()="+ff.getName());
System.out.println("ff.getPath()="+ff.getPath());
DayTime dt=new DayTime();
for(int ii=0;ii<200;ii++)
{ dt.advance(1); File ww=new File("Q:\\"+dt.getDateString2()+"\\s.x");
File w2=new File("Q:\\"+dt.getDateString2()+"\\misc\\s.x");
File w3=new File("Q:\\"+dt.getDateString2()+"\\source\\s.x");
File w4=new File("Q:\\"+dt.getDateString2()+"\\doc\\s.x");
File w5=new File("Q:\\"+dt.getDateString2()+"\\javadoc\\s.x"); w2.getParentFile().mkdirs();
w3.getParentFile().mkdirs();
w4.getParentFile().mkdirs();
w5.getParentFile().mkdirs();
c.copy("e:\\readme.doc","Q:\\"+dt.getDateString2()+"\\readme.doc");
System.out.print("OK?");
}
try{ System.in.read() ;}catch(Exception w){}
if (args.length<1)
{
usage(); }
if (args.length==2)
{ c.copy(args[0],args[1]);
//c.copy("e:\\Copy.java","e:\\xxx\\s.txt");
try{ System.in.read() ;}catch(Exception w){}
return;
} if (args.length>=3 && args[0].equals("-x"))
c.xcopy(args[1],args[2]);
else
usage();
// c.copy(args[0],args[1]); try{ System.in.read() ;}catch(Exception w){}
} /**
<big>简</big>单拷贝文件 不包含子文件夹及其文件!
<p> 注:1。 如果源为文件夹且目的是文件夹则目的省略掉文件名
<p> 2。 当源文件或文件夹不存在时或目标文件或文件夹不存在时抛出IOException
@param s 源文件/文件夹
@param t 目的文件/文件夹
@throws 当源文件或文件夹不存在时或目标文件或文件夹不存在时抛出IOException
*/
public void copy(String s,String t) throws IOException
{
init(s,t); if(bs_file )
{
copyfile(fs,ft);
return;
} //both directory
}
/**
<big>遍</big>历拷贝!
<P> 注:1。 如果源为文件夹且目的是文件夹则目的省略掉文件名
<p> 2。 当源文件或文件夹不存在时或目标文件或文件夹不存在时抛出IOException
@param s 源文件/文件夹
@param t 目的文件/文件夹
@throws 当源文件或文件夹不存在时或目标文件或文件夹不存在时抛出IOException
*/
public void xcopy(String s,String t) throws IOException
{
init(s,t);
if(bs_file )
copyfile(fs,ft);
//both directory
}
/** init*/
private void init(String s,String t)
{
this.fs=new File(s);
this.ft=new File(t); if(!fs.exists()){
new IOException("Err:file no exists!");
}
//判断是否以 File.pathSeparator 结尾断定
// ft.getAbsolutePath() ft.getParentFile().mkdirs();
// System.out.println("failed");
// new IOException("Err:file/directory create failure!");
this.bt_file=ft.isFile()?true:false;
this.bs_file=fs.isFile()?true:false;
// System.out.println(bt_file);
/*
if ( !ft.exists())
{
ft.createNewFile();
} if (bt_file)
if(!ft.getParentFile().mkdirs()) System.out.println("failed");
else
if(!ft.mkdirs())
new IOException("Err:file/directory create failure!");
else
if (bs_file) ft=new File(ft,fs.getName());
*/
// ft=new File(ft,fs.getName());
if(bt_file && !bs_file)
ft=ft.getParentFile();
} /**
File to File copy
@param fas source file
@param fat target file
*/
public void copyfile(File fas,File fat)throws IOException
{
FileReader in = new FileReader(fas);
FileWriter out = new FileWriter(fat);
int c; while ((c = in.read()) != -1)
out.write(c); in.close();
out.close(); } /** 源文件对象 */
private File fs;
/** 目的文件对象 */
private File ft;
/** 源文件对象是否为文件 */
private boolean bs_file;
/** 目的文件对象是否为文件 */
private boolean bt_file;}
Runtime.getRuntime().exec("c:\\winnt\\system32\\cmd.exe /c copy d:\\1.txt d:\\2.txt");
大侠能不能列出在UNIX 下怎么 用,有没有判断windows/unix的方法?
加上这两条我想用处就大了,
请大家接着贴