Discuss / Java / 作业

作业

Topic source

K_Gulmibot

#1 Created at ... [Delete] [Delete and Lock User]

package com.itranswarp.learnjava;

import java.io.*;

/**

 * Learn Java from https://www.liaoxuefeng.com/

 * 

 * @author liaoxuefeng

 */

public class CopyFile {

public static void main(String[] args) throws IOException {

if (args.length != 2) {

System.err.println("Usage:\n  java CopyFile.java <source> <target>");

System.exit(1);

}

copy(args[0], args[1]);

System.out.println("Done!");

}

static void copy(String source, String target) throws IOException {

// 友情提示:测试时请使用无关紧要的文件

// TODO:

String s;

StringBuilder sb=new StringBuilder ();

InputStream input=new FileInputStream(source);

OutputStream output=new FileOutputStream(target);

    byte[] buffer=new byte[1024];

try(input){

int n;

    while((n=input.read(buffer))!=-1) {    

    sb.append(new String(buffer, "UTF-8" ));     

    }     

    s=sb.toString();

    //System.out.println(s);

    output.write(s.getBytes("UTF-8"));

}

}

}


  • 1

Reply