You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1.4 KiB

CatInFile

Instructions

Create a file named CatInFile.java.

Write a function cat that reads from standard input and write to file given as parameter.

You may read from the input some binary content.

💡 Be aware of how much you read at once.

Expected Functions

import java.io.*;

public class CatInFile {
    public static void cat(String[] args) throws IOException {
        // your code here
    }
}

Usage

Here is a possible ExerciseRunner.java to test your function :

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

public class ExerciseRunner {
    public static void main(String[] args) throws IOException {
        String input = "input file test\n";
        ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes());
        System.setIn(in);
        CatInFile.cat(new String[]{"output"});
        String fileContent = new String(Files.readAllBytes(Paths.get("output")));
        System.out.println(fileContent.equals(input));
    }
}

and its output :

$ javac *.java -d build
$ java -cp build ExerciseRunner
true
$

Notions

Command-Line Arguments File Standard Input