public class IOUtils extends Object
Modifier and Type | Method and Description |
---|---|
static void |
copy(File in,
File out)
Copies the entire contents of the given in file into the given out file.
|
static String |
getCurrentDirectory()
The directory in which the current app is running.
|
static File |
getExpectedInputDirectory()
Returns a directory that is likely to contain expected input files for
a running Stanford lib project.
|
static File |
getExpectedOutputDirectory()
Returns a directory that is likely to contain expected output files for
a running Stanford lib project.
|
static String |
getExtension(File file)
The file name extension (such as mp3, doc, txt) of the given file's name.
|
static String |
getExtension(String fileName)
The file name extension (such as mp3, doc, txt) of the given file's name.
|
static String |
getFileName(String filePath)
Strips folders from file's name.
|
static String |
getFolder(String fileName)
The folder name of the given file path.
|
static String |
getTempFolder()
The full path to the server's temp directory, such as "/tmp/foobar/".
|
static String |
readEntireFile(File file)
Reads the given file's text fully and returns it as a String.
|
static String |
readEntireFile(String fileName)
Reads the given file's text fully and returns it as a String.
|
static byte[] |
readEntireFileBytes(File file)
Reads the full text from the given file,
and returns it as an array of bytes.
|
static byte[] |
readEntireFileBytes(String fileName)
Reads the full text from the file with the given name,
and returns it as an array of bytes.
|
static String |
readEntireReader(Reader reader)
Reads the given reader's text fully and returns it as a String.
|
static byte[] |
readEntireReaderBytes(Reader reader)
Reads the given reader's text fully and returns it as an array of bytes.
|
static String |
readEntireScanner(Scanner input)
Reads the given scanner's text fully and returns it as a String.
|
static String |
readEntireStream(InputStream stream)
Reads the given stream's text fully and returns it as a String.
|
static byte[] |
readEntireStreamBytes(InputStream stream)
Reads the given stream's text fully and returns it as an array of bytes.
|
static void |
redirectInput(String data)
Redirects System.in to pull input data from the text in the given string.
|
static ByteArrayOutputStream |
redirectOutput()
Redirects System.out to a byte array output stream, which is returned.
|
static ByteArrayOutputStream |
redirectOutput(boolean limit)
Redirects System.out to a byte array output stream, which is returned.
|
static String |
removeExtension(String fileName)
Removes the file's extension; e.g.
|
static String |
removeFolder(String fileName)
Removes any leading folders from the given file name;
e.g.
|
static void |
stopRedirectingInput()
Sets System.in back to its original state.
|
static void |
stopRedirectingOutput()
Sets System.out/err back to their original state.
|
static InputStream |
toInputStream(String data)
Creates and returns an input stream to read the characters of the given
string as bytes, one character at a time.
|
static void |
writeBytes(byte[] bytes,
String fileName)
Writes the given bytes into the given file, replacing any existing content.
|
static void |
writeEntireFile(String text,
File file)
Writes the given text into the given file, replacing any existing text.
|
static void |
writeEntireFile(String text,
String fileName)
Writes the given text into the file with the given name, replacing any existing text.
|
public static void copy(File in, File out)
public static String getCurrentDirectory()
public static File getExpectedInputDirectory()
public static File getExpectedOutputDirectory()
public static String getExtension(File file)
public static String getExtension(String fileName)
public static String getFileName(String filePath)
public static String getFolder(String fileName)
public static String getTempFolder()
public static String readEntireFile(File file)
FileNotFoundException
- if file cannot be readpublic static String readEntireFile(String fileName)
FileNotFoundException
- if file cannot be readpublic static String readEntireReader(Reader reader)
FileNotFoundException
- if file cannot be readpublic static String readEntireScanner(Scanner input)
public static String readEntireStream(InputStream stream)
FileNotFoundException
- if file cannot be readpublic static byte[] readEntireStreamBytes(InputStream stream)
IOException
- if stream cannot be readpublic static byte[] readEntireReaderBytes(Reader reader)
IOException
- if stream cannot be readpublic static byte[] readEntireFileBytes(String fileName)
IOException
- if stream cannot be readpublic static byte[] readEntireFileBytes(File file)
IOException
- if stream cannot be readpublic static InputStream toInputStream(String data)
public static void redirectInput(String data)
public static ByteArrayOutputStream redirectOutput()
public static ByteArrayOutputStream redirectOutput(boolean limit)
limit
- if true, sets a limit on the amount of output that can be
produced before an ExcessiveOutputException will occur
(so students can't infinitely println).public static String removeExtension(String fileName)
public static String removeFolder(String fileName)
public static void stopRedirectingInput()
public static void stopRedirectingOutput()
public static void writeEntireFile(String text, String fileName)
public static void writeEntireFile(String text, File file)
public static void writeBytes(byte[] bytes, String fileName)