读文件A>>写文件B
用字节流.如果是转移,直接可以用file1.renameTo(file2);

解决方案 »

  1.   

    Runtime.getRuntime().exec("copy c:/1.txt c:/2.txt");
      

  2.   

    package com.ocean.jproject.io;/*
     A set of handy methods for common (text)file-related operations. Read, write, append, prepend, read from a zip, load into a Properties object, read from console. Simple but useful.
     */
    import java.io.*;
    import java.util.*;
    import java.util.zip.*;import com.ocean.jproject.util.*;/**
     Provides useful operations on files.  Most of the operations are intended
     only for text files.
     @date: 7/6/00
     @author G. Bright
     **/
    public class FileTool {
    //_______________________________________________________________________
      /** Append a given string to the text file with the given filename.<P>
       @param strstring to append to file
       @param filenamename of file
       **/
      public void appendToFile(String str, String filename) throws Exception {
    // Open up an outputstream writer to the file, and append str to it.
        FileOutputStream stream; //provides file access
        OutputStreamWriter writer; //writes to the file
        try {
          stream = new FileOutputStream(filename, true);
          writer = new OutputStreamWriter(stream);
          writer.write(str);
          writer.close();
          stream.close();
        } //try
        catch (Exception e) {
          throw e;
        } //catch
      } //appendToFile//_______________________________________________________________________
    //_______________________________________________________________________
      /** Prepend a given string to the text file with the given filename. <P>
       @param strstring to prepend to file
       @param filenamename of file
       **/
      

  3.   

    public void prependToFile(String str, String filename) throws Exception {
    // Read the file, then prepend str to the file text and write back
    // to the file.
        String newStr; //final string to be written
        try {
          newStr = str + readFile(filename);
          writeFile(newStr, filename);
        } //try
        catch (Exception e) {
          throw e;
        } //catch
      } //prependToFile//_______________________________________________________________________
    //_______________________________________________________________________
      /** Read the text file with the given filename and return as a string. <P>
       @param filenamename of file to read
       @returnstring of text of file
       **/
      public String readFile(String filename) throws Exception {
    //Read the file into a string buffer, then return as a string.
        StringBuffer buf; //the intermediary, mutable buffer
        BufferedReader breader; //reader for the template files
        //if(JDebug.DEBUG){JDebug.print("readFile:"+filename);}
        try {
          breader = new BufferedReader(new FileReader(filename)); //header
          buf = new StringBuffer();
          while (breader.ready()) {
            buf.append( (char) breader.read());
          }
          breader.close();
        } //try
        catch (Exception e) {      throw e;
        } //catch
        return buf.toString();
      } //readFile//_______________________________________________________________________
    //_______________________________________________________________________
      /** Read the given text file and return as a vector of lines. <P>
       @param filenamename of file to read
       @returnvector, in which the <I>ith</I> element is a string of the
       <I>ith</I> line of the  text file.
       **/
      public List readFileToList(String filename) throws Exception {
    //Read the file into a List, then return.
        BufferedReader breader; //reader for the template files
        List list; //target vector
        String line; //line from file
        list = new ArrayList();
        try {
          breader = new BufferedReader(new FileReader(filename)); //header
          while ( (line = breader.readLine()) != null) {
            list.add(line);
          }
          breader.close();
        } //try
        catch (Exception e) {
          throw e;
        } //catch
        return list;
      } //readFileToVector//_______________________________________________________________________
    //_______________________________________________________________________
      /** Write the given string to the file with the given filename. <P>
       @param strstring to write to file
       @param filenamename of file
       <P>
       Note: The pre-existing contents of the file are completely over written.
       **/
      

  4.   

    public void writeFile(String str, String filename) throws Exception {
        //if(JDebug.DEBUG){JDebug.print("writeFile="+filename);}
        String strFilePath = filename.substring(0, filename.lastIndexOf("\\"));
        //if(JDebug.DEBUG){JDebug.print("file path="+strFilePath);}
        File filePath = new File(strFilePath);
        if (!filePath.exists()) {
          filePath.mkdirs();
        }// Open a writer to the file, then write the string.
        BufferedWriter bwriter; //writer to the file
        String fullfilepath; //path for the output file    try {
          bwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
              filename)));
          bwriter.write(str);
          bwriter.flush();
          bwriter.close();
        } //try
        catch (Exception e) {
          if (JDebug.DEBUG) {
            JDebug.print(filename + e.toString());
          }
          throw e;
        } //catch
      } //writeFile//_______________________________________________________________________
    //_______________________________________________________________________
      /** Copy a file to another location.
       @param sourcenamename of file to copy
       @param targetnamename of file to copy to
       **/
      public void copyFile(String sourcename, String targetname) throws Exception {
    // Open up a reader from sourcename and a writer to targetname.
    // Write each character from sourcename to targetname, then close.
        BufferedReader breader; //reader from source
        BufferedWriter bwriter; //writer to target
        try {
          breader = new BufferedReader(new FileReader(sourcename));
          bwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
              targetname)));
          while (breader.ready()) {
            bwriter.write(breader.read());
          }
          breader.close();
          bwriter.close();
        } //try
        catch (Exception e) {
          throw e;
        } //catch
      } //copyFile//_______________________________________________________________________
    //_______________________________________________________________________
      /** Read a text file from a given zip file and return as a string.
       @param textFilenamethe zipped text file to read
       @param zipFilenamezipp file to open
       @returntext of the file
       **/
      public String readZippedFile(String textFilename, String zipFilename) throws
          Exception {
    // Open a stream from the entry in the zip file for the file.
    // Read the file into a string buffer, then return as a string.
        StringBuffer buf; //the intermediary, mutable buffer
        BufferedReader breader; //reader for the template files
        ZipFile zipFile; //zip file
        ZipEntry zipEntry; //entry in zip
        InputStream iStream; //input stream of file
        try {
          zipFile = new ZipFile(zipFilename);
          zipEntry = zipFile.getEntry(textFilename);
          iStream = zipFile.getInputStream(zipEntry);
          breader = new BufferedReader(new InputStreamReader(iStream));
          buf = new StringBuffer();
          while (breader.ready()) {
            buf.append( (char) breader.read());
          }
          breader.close();
        } //try
        catch (Exception e) {
          throw e;
        } //catch
        return buf.toString();
      } //readZippedFile//_______________________________________________________________________
    //______________________________________________________________
      /** Read a line from System.in. (block) <P>
       @return input read from console.
       **/
      public String readConsole() throws Exception {
    // Create a buffered reader with System.in, then
    // read a line from it.
        BufferedReader breader;
        breader = new BufferedReader(new InputStreamReader(System.in));
        return breader.readLine();
      } //readConsole//______________________________________________________________
    //______________________________________________________________
      /** Return a properties object loaded from the given file. <P>
       @parampropsFilename of props file to load
       @return properties object from file
       */
      public Properties loadProperties(String propsFile) throws Exception {
    // Open up an input stream from the file, then read it into
    // a properties hashtable and return.
        FileInputStream instream; //the input stream
        Properties props; //props object
        instream = null;
        props = new Properties();
        try {
          instream = new FileInputStream(propsFile);
          props.load(instream);
        } //try
        finally {
          try {
            instream.close();
          } //try
          catch (Exception e1) {}
        } //finally
        return props;
      } //loadProperties
    //______________________________________________________________
    } //FileTool