我想一般的server/client程序加上queue代码让检查’quit‘
就对出就行了

解决方案 »

  1.   

    import java.io.*;public class ch4_9
    {
       public static void main(String args[])
       {
          String temp;
          File   sourceFile,targetFile;
          BufferedReader source;
          BufferedWriter target;
     
          try
          {
             InputStreamReader stdin = new InputStreamReader(System.in);
             BufferedReader bufin = new BufferedReader(stdin);
          
             System.out.print("请输入来源文件路径: ");
             sourceFile = new File(bufin.readLine());
             source = new BufferedReader(new FileReader(sourceFile));
             System.out.print("请输入目的文件路径: ");
             targetFile = new File(bufin.readLine());
             target = new BufferedWriter(new FileWriter(targetFile));
             
             System.out.print("确定要复制?(y/n) ");
             if((bufin.readLine()).equals("y"))
             {     
                while((temp=source.readLine()) != null)
                {
                   target.write(temp);
                   target.newLine();
                   target.flush();
                }
                System.out.println("复制文件完成!!!");
             }
             else
             {
                System.out.println("复制文件失败!!!");  
                return;
             }
             stdin.close();
             bufin.close();
          }
          catch(IOException E)
          {
             System.out.println("发生I/O错误!!!");
          }
       }
    }
      

  2.   

    你自己写的东西帖出来,这样别人能帮你吗,你不帖出来,别人都 以为你是在找思路,所以才没有人给代码的。你的程序实现很简单的,自己,试试便知道了。自己真的的不会,我愿帮你的。
    email: [email protected]
      

  3.   

    源代码如下:import java.io.*;public class copy{ File from_f=null,to_f=null;

    public copy(String s,String s1){
    from_f=new File(s);
    to_f=new File(s1);
    }

    public void copy_file(){
    try{
    System.out.println("开始拷贝文件......");
    FileInputStream f_in=new FileInputStream(from_f);
    FileOutputStream f_out=new FileOutputStream(to_f);
    int p=0;
    while(true){
    byte b[]=new byte[1024];
    p=f_in.read(b);
    if(p<0)
      break;
    f_out.write(b,0,p);
    }
    System.out.println("拷贝文件结束");
    }catch(Exception e){
    System.out.println("拷贝文件错误!");
    System.out.println("错误原因:"+e.toString());
    }
    }

    public static void main(String[] args){

      if(args.length<2)
      copy.show();
      (new copy(args[0],args[1])).copy_file();
      while(true)
      try{
      byte b[]=new byte[50];
      int p=System.in.read(b);
      if((p-2)>=0)
    p=p-2;//去掉回车 
      String command=new String(b,0,p);
      if(command.equals("quit")){
      System.out.println("回头见!");
      System.exit(0);
      }
      }catch(Exception e){}


    }


    public static void  show(){

    System.out.println("命令格式如下:");
    System.out.println("java copy 源文件名 目标文件名");
    System.exit(0);

    }

    }执行命令:
    javac copy.java
    java copy copy.java bak.java祝你好运!
      

  4.   

    呵呵,你可真是幸运啊,那么多人给你帮忙。最好是自己动手吧,以后不要这样做罗,这可不是在帮你,会对你有害哦,呵呵....import java.io.*;public class FileIO{
    public static void main(String[] args){
    String str="";
    System.out.println("======================File I/O Testing=========================");
    System.out.println("--input 'quit' to exit--");
    //screen input
    String sFile="";
    String dFile="";
    InputStreamReader in=new InputStreamReader(System.in);
    BufferedReader br=new BufferedReader(in);
    //file io
    FileInputStream fin=null;
    FileOutputStream fout=null;
    File f=null;
    //get the source file name from screen input and judge the validation
    while(!isQuit(str)){
    System.out.print("The source file: ");
    try{
    str=br.readLine();
    }catch(IOException x){

    }
    //System.out.print("debug: " + str);
    if(isQuit(str)){
    System.exit(0);
    }else{
    //test wether the file exsiting
    try{
    f=new File(str);
    if(!(f.exists() && f.isFile())){
    System.out.println("!!!Error: the file you defined does not exist, try it again...");
    continue;
    }
    sFile=str;
    fin=new FileInputStream(str);
    }catch(FileNotFoundException x){

    }
    str="";
    break;
    }
    }
    //get the destination file name from screen and judge the validation
    while(!isQuit(str)){
    System.out.print("The destination file: ");
    try{
    str=br.readLine();
    }catch(IOException x){

    }
    if(isQuit(str)){
    //remember to close the io!
    if(fin!=null){
    try{
    fin.close();
    }catch(IOException x){
    }
    }
    System.exit(0);
    }else{
    //test wether the file exsiting
    boolean isContinued=false;
    //boolean isBreak=false;
    f=new File(str);
    if(f.exists() & f.isFile()){
    while(true){
    System.out.print("The file has been existed, do you want to override it?(y-YES,n-NO,q-Quit):");
    String tempStr="";
    try{
    tempStr=br.readLine();
    }catch(IOException x){
    }
    if(tempStr.equalsIgnoreCase("y")){
    break;
    }else if(tempStr.equalsIgnoreCase("n")){
    System.out.println("Please define another destination file again...");
    isContinued=true;
    break;
    }else if(tempStr.equalsIgnoreCase("q")){
    //remember to close the io!
    if(fin!=null){
    try{
    fin.close();
    }catch(IOException x){
    }
    }
    System.exit(0);
    }else{
    continue;
    }
    }
    }


    if(isContinued)
    continue;
    dFile=str;
    if(sFile.trim().equals(dFile.trim())){
    System.out.println("Invalid operation, it is not allowable to export a file to himself...");
    System.out.println("Please redefine another destination file...");
    continue;
    }

    try{

    fout=new FileOutputStream(str);

    }catch(FileNotFoundException x){

    System.out.println("!!!Error: the output file can not be found, try it again...");
    continue;
    }
    break;
    }
    }
    BufferedInputStream bIn=null;
    BufferedOutputStream bOut=null;
    System.out.println("Begin to export file...");
    try{
    //read source file
    bIn=new BufferedInputStream(fin);
    int bNum=bIn.available();
    byte[] bt=new byte[bNum];
    bIn.read(bt,0,bNum);
    //write to destination file
    bOut=new BufferedOutputStream(fout);
    bOut.write(bt,0,bNum);
    bOut.flush();
    bIn.close();
    bOut.close();
    System.out.println("Success to export file from '" + sFile + "' to '" + dFile + "'.");
    }catch(IOException x){
    System.out.println("Fail to read/write file...");
    x.printStackTrace();
    }

    }

    public static boolean isQuit(String str){
    return str.equalsIgnoreCase("quit");
    }}