📄 textio.java
字号:
catch (Exception e) { } } out = newout; writingStandardOutput = false; outputFileName = selectedFile.getName(); outputErrorCount = 0; return true; } /** * If TextIO is currently reading from a file, then the return value is the name of the file. * If the class is reading from standard input or from a stream, then the return value is null. */ public static String getInputFileName() { return inputFileName; } /** * If TextIO is currently writing to a file, then the return value is the name of the file. * If the class is writing to standard output or to a stream, then the return value is null. */ public static String getOutputFileName() { return outputFileName; } // *************************** Output Methods ********************************* /** * Write a single value to the current output destination, using the default format * and no extra spaces. This method will handle any type of parameter, even one * whose type is one of the primitive types. */ public static void put(Object x) { out.print(x); out.flush(); if (out.checkError()) outputError("Error while writing output."); } /** * Write a single value to the current output destination, using the default format * and outputting at least minChars characters (with extra spaces added before the * output value if necessary). This method will handle any type of parameter, even one * whose type is one of the primitive types. * @param x The value to be output, which can be of any type. * @param minChars The minimum number of characters to use for the output. If x requires fewer * then this number of characters, then extra spaces are added to the front of x to bring * the total up to minChars. If minChars is less than or equal to zero, then x will be printed * in the minumum number of spaces possible. */ public static void put(Object x, int minChars) { if (minChars <= 0) out.print(x); else { out.printf("%" + minChars + "s", x); out.flush(); } if (out.checkError()) outputError("Error while writing output."); } /** * This is equivalent to put(x), followed by an end-of-line. */ public static void putln(Object x) { out.println(x); out.flush(); if (out.checkError()) outputError("Error while writing output."); } /** * This is equivalent to put(x,minChars), followed by an end-of-line. */ public static void putln(Object x, int minChars) { put(x,minChars); out.println(); out.flush(); if (out.checkError()) outputError("Error while writing output."); } /** * Write an end-of-line character to the current output destination. */ public static void putln() { out.println(); out.flush(); if (out.checkError()) outputError("Error while writing output."); } /** * Writes formatted output values to the current output destination. This method has the * same function as System.out.printf(); the details of formatted output are not discussed * here. The first parameter is a string that describes the format of the output. There * can be any number of additional parameters; these specify the values to be output and * can be of any type. This method will throw an IllegalArgumentException if the * format string is null or if the format string is illegal for the values that are being * output. */ public static void putf(String format, Object... items) { if (format == null) throw new IllegalArgumentException("Null format string in TextIO.putf() method."); try { out.printf(format,items); } catch (IllegalFormatException e) { throw new IllegalArgumentException("Illegal format string in TextIO.putf() method."); } out.flush(); if (out.checkError()) outputError("Error while writing output."); } // *************************** Input Methods ********************************* /** * Test whether the next character in the current input source is an end-of-line. Note that * this method does NOT skip whitespace before testing for end-of-line -- if you want to do * that, call skipBlanks() first. */ public static boolean eoln() { return peek() == '\n'; } /** * Test whether the next character in the current input source is an end-of-file. Note that * this method does NOT skip whitespace before testing for end-of-line -- if you want to do * that, call skipBlanks() or skipWhitespace() first. */ public static boolean eof() { return peek() == EOF; } /** * Reads the next character from the current input source. The character can be a whitespace * character; compare this to the getChar() method, which skips over whitespace and returns the * next non-whitespace character. An end-of-line is always returned as the character '\n', even * when the actual end-of-line in the input source is something else, such as '\r' or "\r\n". * This method will throw an IllegalArgumentException if the input is at end-of-file (which will * not ordinarily happen if reading from standard input). */ public static char getAnyChar() { return readChar(); } /** * Returns the next character in the current input source, without actually removing that * character from the input. The character can be a whitespace character and can be the * end-of-file character (specfied by the constant TextIO.EOF).An end-of-line is always returned * as the character '\n', even when the actual end-of-line in the input source is something else, * such as '\r' or "\r\n". This method never causes an error. */ public static char peek() { return lookChar(); } /** * Skips over any whitespace characters, except for end-of-lines. After this method is called, * the next input character is either an end-of-line, an end-of-file, or a non-whitespace character. * This method never causes an error. (Ordinarly, end-of-file is not possible when reading from * standard input.) */ public static void skipBlanks() { char ch=lookChar(); while (ch != EOF && ch != '\n' && Character.isWhitespace(ch)) { readChar(); ch = lookChar(); } } /** * Skips over any whitespace characters, including for end-of-lines. After this method is called, * the next input character is either an end-of-file or a non-whitespace character. * This method never causes an error. (Ordinarly, end-of-file is not possible when reading from * standard input.) */ private static void skipWhitespace() { char ch=lookChar(); while (ch != EOF && Character.isWhitespace(ch)) { readChar(); if (ch == '\n' && readingStandardInput && writingStandardOutput) { out.print("? "); out.flush(); } ch = lookChar(); } } /** * Skips whitespace characters and then reads a value of type byte from input, discarding the rest of * the current line of input (including the next end-of-line character, if any). When using standard IO, * this will not produce an error; the user will be prompted repeatedly for input until a legal value * is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found. */ public static byte getlnByte() { byte x=getByte(); emptyBuffer(); return x; } /** * Skips whitespace characters and then reads a value of type short from input, discarding the rest of * the current line of input (including the next end-of-line character, if any). When using standard IO, * this will not produce an error; the user will be prompted repeatedly for input until a legal value * is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found. */ public static short getlnShort() { short x=getShort(); emptyBuffer(); return x; } /** * Skips whitespace characters and then reads a value of type int from input, discarding the rest of * the current line of input (including the next end-of-line character, if any). When using standard IO, * this will not produce an error; the user will be prompted repeatedly for input until a legal value * is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found. */ public static int getlnInt() { int x=getInt(); emptyBuffer(); return x; } /** * Skips whitespace characters and then reads a value of type long from input, discarding the rest of * the current line of input (including the next end-of-line character, if any). When using standard IO, * this will not produce an error; the user will be prompted repeatedly for input until a legal value * is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found. */ public static long getlnLong() { long x=getLong(); emptyBuffer(); return x; } /** * Skips whitespace characters and then reads a value of type float from input, discarding the rest of * the current line of input (including the next end-of-line character, if any). When using standard IO, * this will not produce an error; the user will be prompted repeatedly for input until a legal value * is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found. */ public static float getlnFloat() { float x=getFloat(); emptyBuffer(); return x; } /** * Skips whitespace characters and then reads a value of type double from input, discarding the rest of * the current line of input (including the next end-of-line character, if any). When using standard IO, * this will not produce an error; the user will be prompted repeatedly for input until a legal value * is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found. */ public static double getlnDouble() { double x=getDouble(); emptyBuffer(); return x; } /** * Skips whitespace characters and then reads a value of type char from input, discarding the rest of * the current line of input (including the next end-of-line character, if any). Note that the value * that is returned will be a non-whitespace character; compare this with the getAnyChar() method. * When using standard IO, this will not produce an error. In other cases, an error can occur if * an end-of-file is encountered. */ public static char getlnChar() { char x=getChar(); emptyBuffer(); return x; } /** * Skips whitespace characters and then reads a value of type boolean from input, discarding the rest of * the current line of input (including the next end-of-line character, if any). When using standard IO, * this will not produce an error; the user will be prompted repeatedly for input until a legal value * is input. In other cases, an IllegalArgumentException will be thrown if a legal value is not found. * <p>Legal inputs for a boolean input are: true, t, yes, y, 1, false, f, no, n, and 0; letters can be * either upper case or lower case. One "word" of input is read, using the getWord() method, and it * must be one of these; note that the "word" must be terminated by a whitespace character (or end-of-file). */ public static boolean getlnBoolean() { boolean x=getBoolean(); emptyBuffer(); return x; } /** * Skips whitespace characters and then reads one "word" from input, discarding the rest of * the current line of input (including the next end-of-line character, if any). A word is defined as * a sequence of non-whitespace characters (not just letters!). When using standard IO, * this will not produce an error. In other cases, an IllegalArgumentException will be thrown * if an end-of-file is encountered. */ public static String getlnWord() { String x=getWord(); emptyBuffer(); return x; } /** * This is identical to getln(). */ public static String getlnString() { return getln(); } /** * Reads all the charcters from the current input source, up to the next end-of-line. The end-of-line * is read but is not included in the return value. Any other whitespace characters on the line are retained, * even if they occur at the start of input. The return value will be an empty string if there are no * no characters before the end-of-line. When using standard IO, this will not produce an error. * In other cases, an IllegalArgumentException will be thrown if an end-of-file is encountered. */ public static String getln() { StringBuffer s = new StringBuffer(100); char ch = readChar(); while (ch != '\n') { s.append(ch); ch = readChar(); } return s.toString(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -