📄 excelcsvprinter.java
字号:
error = true; throw iox; } } /** * Print a single line of comma separated values. * The values will be quoted if needed. Quotes and * and other characters that need it will be escaped. * <p> * This method never throws an I/O exception. The client may inquire as to whether * any errors have occurred by invoking checkError(). If an I/O Exception is * desired, the client should use the corresponding writeln method. * * @param values values to be outputted. * * @since ostermillerutils 1.00.00 */ public void print(String[] values){ try { write(values); } catch (IOException iox){ error = true; } } /** * Print a single line of comma separated values. * The values will be quoted if needed. Quotes and * and other characters that need it will be escaped. * * @param values values to be outputted. * @throws IOException if an error occurs while writing. * * @since ostermillerutils 1.02.26 */ public void write(String[] values) throws IOException { try { for (int i=0; i<values.length; i++){ write(values[i]); } } catch (IOException iox){ error = true; throw iox; } } /** * Print several lines of comma separated values. * The values will be quoted if needed. Quotes and * newLine characters will be escaped. * <p> * This method never throws an I/O exception. The client may inquire as to whether * any errors have occurred by invoking checkError(). If an I/O Exception is * desired, the client should use the corresponding writeln method. * * @param values values to be outputted. * * @since ostermillerutils 1.00.00 */ public void println(String[][] values){ try { writeln(values); } catch (IOException iox){ error = true; } } /** * Print several lines of comma separated values. * The values will be quoted if needed. Quotes and * newLine characters will be escaped. * * @param values values to be outputted. * @throws IOException if an error occurs while writing. * * @since ostermillerutils 1.02.26 */ public void writeln(String[][] values) throws IOException { try { for (int i=0; i<values.length; i++){ writeln(values[i]); } if (values.length == 0){ writeln(); } } catch (IOException iox){ error = true; throw iox; } } /** * Since ExcelCSV format does not support comments, * this method will ignore the comment and start * a new row. * <p> * This method never throws an I/O exception. The client may inquire as to whether * any errors have occurred by invoking checkError(). If an I/O Exception is * desired, the client should use the corresponding writelnComment method. * * @param comment the comment to output (ignored). * * @since ostermillerutils 1.00.00 */ public void printlnComment(String comment){ println(); } /** * Since ExcelCSV format does not support comments, * this method will ignore the comment and start * a new row. * * @param comment the comment to output (ignored). * @throws IOException if an error occurs while writing. * * @since ostermillerutils 1.02.26 */ public void writelnComment(String comment) throws IOException { writeln(); } /** * Print the string as the next value on the line. The value * will be quoted if needed. If value is null, an empty value is printed. * <p> * This method never throws an I/O exception. The client may inquire as to whether * any errors have occurred by invoking checkError(). If an I/O Exception is * desired, the client should use the corresponding println method. * * @param value value to be outputted. * * @since ostermillerutils 1.00.00 */ public void print(String value){ try { write(value); } catch (IOException iox){ error = true; } } /** * Print the string as the next value on the line. The value * will be quoted if needed. If value is null, an empty value is printed. * * @param value value to be outputted. * @throws IOException if an error occurs while writing. * * @since ostermillerutils 1.02.26 */ public void write(String value) throws IOException { try { if (value == null) value = ""; boolean quote = false; if (alwaysQuote){ quote = true; } else if (value.length() > 0){ for (int i=0; i<value.length(); i++){ char c = value.charAt(i); if (c==quoteChar || c==delimiterChar || c=='\n' || c=='\r'){ quote = true; } } } else if (newLine) { // always quote an empty token that is the firs // on the line, as it may be the only thing on the // line. If it were not quoted in that case, // an empty line has no tokens. quote = true; } if (newLine){ newLine = false; } else { out.write(delimiterChar); } if (quote){ out.write(escapeAndQuote(value)); } else { out.write(value); } if (autoFlush) flush(); } catch (IOException iox){ error = true; throw iox; } } /** * Enclose the value in quotes and escape the quote * and comma characters that are inside. * * @param value needs to be escaped and quoted. * * @return the value, escaped and quoted. * @since ostermillerutils 1.00.00 */ private String escapeAndQuote(String value){ String s = StringHelper.replace(value, Character.toString(quoteChar), Character.toString(quoteChar) + Character.toString(quoteChar)); return (new StringBuffer(2 + s.length())).append(quoteChar).append(s).append(quoteChar).toString(); } /** * Flush any data written out to underlying streams. * * @since ostermillerutils 1.02.26 */ public void flush() throws IOException { out.flush(); } /** * Close any underlying streams. * * @since ostermillerutils 1.02.26 */ public void close() throws IOException { out.close(); } /** * Flush the stream if it's not closed and check its error state. * Errors are cumulative; once the stream encounters an error, * this routine will return true on all successive calls. * * @return True if the print stream has encountered an error, * either on the underlying output stream or during a format conversion. * * @since ostermillerutils 1.02.26 */ public boolean checkError(){ try { if (error) return true; flush(); if (error) return true; if (out instanceof PrintWriter){ error = ((PrintWriter)out).checkError(); } } catch (IOException iox){ error = true; } return error; } /** * Set whether values printers should always be quoted, or * whether the printer may, at its discretion, omit quotes * around the value. * * @param alwaysQuote true if quotes should be used even when not strictly needed. * * @since ostermillerutils 1.02.26 */ public void setAlwaysQuote(boolean alwaysQuote){ this.alwaysQuote = alwaysQuote; } /** * Set flushing behavior. Iff set, a flush command * will be issued to any underlying stream after each * print or write command. * * @param autoFlush should auto flushing be enabled. * * @since ostermillerutils 1.02.26 */ public void setAutoFlush(boolean autoFlush){ this.autoFlush = autoFlush; } /** * Write some test data to the given file. * * @param args First argument is the file name. System.out used if no filename given. * * @since ostermillerutils 1.00.00 */ private static void main(String[] args) { OutputStream out; try { if (args.length > 0){ File f = new File(args[0]); if (!f.exists()){ f.createNewFile(); if (f.canWrite()){ out = new FileOutputStream(f); } else { throw new IOException("Could not open " + args[0]); } } else { throw new IOException("File already exists: " + args[0]); } } else { out = System.out; } ExcelCSVPrinter p = new ExcelCSVPrinter(out); p.print("unquoted"); p.print("escaped\"quote"); p.println("comma,comma"); p.print("!quoted"); p.print("!unquoted"); p.print(" quoted"); p.println("quoted "); p.print("one"); p.print(""); p.print(""); p.print(""); p.println(""); p.println("two"); p.print("\nthree\nline\n"); p.println("\ttab"); } catch (IOException e){ System.out.println(e.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -