Discuss / Java / 交作业

交作业

Topic source

哔哗bihua

#1 Created at ... [Delete] [Delete and Lock User]
package com.itranswarp.learnjava;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.nio.charset.StandardCharsets;/** * Learn Java from https://www.liaoxuefeng.com/ * * @author liaoxuefeng */public class CopyFile {    private final static int LENGTH = 2;    public static void main(String[] args) throws IOException {        if (args.length != LENGTH) {            System.err.println("Usage:\n  java CopyFile.java <source> <target>");            System.exit(1);        }        copy(args[0], args[1]);    }    static void copy(String source, String target) throws IOException {        // 友情提示:测试时请使用无关紧要的文件        // TODO:        StringBuilder copy = new StringBuilder();        int n;        try (InputStream input = new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8))) {            while ((n = input.read()) != -1) {                copy.append((char) n);            }        String s = copy.toString();        if (s.equals(target)){            System.out.println("复制成功!");        }        else {            System.out.println("复制失败!");        }        }    }}

  • 1

Reply