⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lineends.java

📁 java操作excel的类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	public final static int STYLE_N = 2;	/**	 * The UNIX and Java line ending ("\n")	 *	 * @since ostermillerutils 1.00.00	 */	public final static int STYLE_JAVA = 2;	/**	 * The Macintosh line ending ("\r")	 *	 * @since ostermillerutils 1.00.00	 */	public final static int STYLE_MAC = 3;	/**	 * The Macintosh line ending ("\r")	 *	 * @since ostermillerutils 1.00.00	 */	public final static int STYLE_R = 3;	/**	 * Buffer size when reading from input stream.	 *	 * @since ostermillerutils 1.00.00	 */	private final static int BUFFER_SIZE = 1024;	private final static int STATE_INIT = 0;	private final static int STATE_R = 1;	private final static int MASK_N = 0x01;	private final static int MASK_R = 0x02;	private final static int MASK_RN = 0x04;	/**	 * Change the line endings of the text on the input stream and write	 * it to the output stream.	 *	 * The current system's line separator is used.	 *	 * @param in stream that contains the text which needs line number conversion.	 * @param out stream where converted text is written.	 * @return true if the output was modified from the input, false if it is exactly the same	 * @throws BinaryDataException if non-text data is encountered.	 * @throws IOException if an input or output error occurs.	 *	 * @since ostermillerutils 1.00.00	 */	public static boolean convert(InputStream in, OutputStream out) throws IOException {		return convert(in, out, STYLE_SYSTEM, true);	}	/**	 * Change the line endings of the text on the input stream and write	 * it to the output stream.	 *	 * @param in stream that contains the text which needs line number conversion.	 * @param out stream where converted text is written.	 * @param style line separator style.	 * @return true if the output was modified from the input, false if it is exactly the same	 * @throws BinaryDataException if non-text data is encountered.	 * @throws IOException if an input or output error occurs.	 * @throws IllegalArgumentException if an unknown style is requested.	 *	 * @since ostermillerutils 1.00.00	 */	public static boolean convert(InputStream in, OutputStream out, int style) throws IOException {		return convert(in, out, style, true);	}	/**	 * Change the line endings of the text on the input stream and write	 * it to the output stream.	 *	 * The current system's line separator is used.	 *	 * @param in stream that contains the text which needs line number conversion.	 * @param out stream where converted text is written.	 * @param binaryException throw an exception and abort the operation if binary data is encountered and binaryExcepion is false.	 * @return true if the output was modified from the input, false if it is exactly the same	 * @throws BinaryDataException if non-text data is encountered.	 * @throws IOException if an input or output error occurs.	 *	 * @since ostermillerutils 1.00.00	 */	public static boolean convert(InputStream in, OutputStream out, boolean binaryException) throws IOException {		return convert(in, out, STYLE_SYSTEM, binaryException);	}	/**	 * Change the line endings of the text on the input stream and write	 * it to the output stream.	 *	 * @param in stream that contains the text which needs line number conversion.	 * @param out stream where converted text is written.	 * @param style line separator style.	 * @param binaryException throw an exception and abort the operation if binary data is encountered and binaryExcepion is false.	 * @return true if the output was modified from the input, false if it is exactly the same	 * @throws BinaryDataException if non-text data is encountered.	 * @throws IOException if an input or output error occurs.	 * @throws IllegalArgumentException if an unknown style is requested.	 *	 * @since ostermillerutils 1.00.00	 */	public static boolean convert(InputStream in, OutputStream out, int style, boolean binaryException) throws IOException {		byte[] lineEnding;		switch (style) {			case STYLE_SYSTEM: {				 lineEnding = System.getProperty("line.separator").getBytes();			} break;			case STYLE_RN: {				 lineEnding = new byte[]{(byte)'\r',(byte)'\n'};			} break;			case STYLE_R: {				 lineEnding = new byte[]{(byte)'\r'};			} break;			case STYLE_N: {				 lineEnding = new byte[]{(byte)'\n'};			} break;			default: {				throw new IllegalArgumentException("Unknown line break style: " + style);			}		}		byte[] buffer = new byte[BUFFER_SIZE];		int read;		int state = STATE_INIT;		int seen = 0x00;		while((read = in.read(buffer)) != -1){			for (int i=0; i<read; i++){				byte b = buffer[i];				if (state==STATE_R){					if(b!='\n'){						out.write(lineEnding);						seen |= MASK_R;					}				}				if (b=='\r'){					state = STATE_R;				} else {					if (b=='\n'){						if (state==STATE_R){							seen |= MASK_RN;						} else {							seen |= MASK_N;						}						out.write(lineEnding);					} else if(binaryException && b!='\t' && b!='\f' && (b & 0xff)<32){						throw new BinaryDataException(labels.getString("binaryexcepion"));					} else {						out.write(b);					}					state = STATE_INIT;				}			}		}		if (state==STATE_R){			out.write(lineEnding);			seen |= MASK_R;		}		if (lineEnding.length==2 && lineEnding[0]=='\r' && lineEnding[1]=='\n'){			return ((seen & ~MASK_RN)!=0);		} else if (lineEnding.length==1 && lineEnding[0]=='\r'){			return ((seen & ~MASK_R)!=0);		} else if (lineEnding.length==1 && lineEnding[0]=='\n'){			return ((seen & ~MASK_N)!=0);		} else {			return true;		}	}	/**	 * Change the line endings on given file.	 *	 * The current system's line separator is used.	 *	 * @param f File to be converted.	 * @return true if the file was modified, false if it was already in the correct format	 * @throws BinaryDataException if non-text data is encountered.	 * @throws IOException if an input or output error occurs.	 *	 * @since ostermillerutils 1.00.00	 */	public static boolean convert(File f) throws IOException {		return convert(f, STYLE_SYSTEM, true);	}	/**	 * Change the line endings on given file.	 *	 * @param f File to be converted.	 * @param style line separator style.	 * @return true if the file was modified, false if it was already in the correct format	 * @throws BinaryDataException if non-text data is encountered.	 * @throws IOException if an input or output error occurs.	 * @throws IllegalArgumentException if an unknown style is requested.	 *	 * @since ostermillerutils 1.00.00	 */	public static boolean convert(File f, int style) throws IOException {		return convert(f, style, true);	}	/**	 * Change the line endings on given file.	 *	 * The current system's line separator is used.	 *	 * @param f File to be converted.	 * @param binaryException throw an exception and abort the operation if binary data is encountered and binaryExcepion is false.	 * @return true if the file was modified, false if it was already in the correct format	 * @throws BinaryDataException if non-text data is encountered.	 * @throws IOException if an input or output error occurs.	 *	 * @since ostermillerutils 1.00.00	 */	public static boolean convert(File f, boolean binaryException) throws IOException {		return convert(f, STYLE_SYSTEM, binaryException);	}	/**	 * Change the line endings on given file.	 *	 * @param f File to be converted.	 * @param style line separator style.	 * @param binaryException throw an exception and abort the operation if binary data is encountered and binaryExcepion is false.	 * @return true if the file was modified, false if it was already in the correct format	 * @throws BinaryDataException if non-text data is encountered.	 * @throws IOException if an input or output error occurs.	 * @throws IllegalArgumentException if an unknown style is requested.	 *	 * @since ostermillerutils 1.00.00	 */	public static boolean convert(File f, int style, boolean binaryException) throws IOException {		File temp = null;		InputStream in = null;		OutputStream out = null;		boolean modified = false;		try {			in = new FileInputStream(f);			temp = File.createTempFile("LineEnds", null, null);			out = new FileOutputStream(temp);			modified = convert (in, out, style, binaryException);			in.close();			in = null;			out.flush();			out.close();			out = null;			if (modified){				FileHelper.move(temp, f, true);			} else {				if (!temp.delete()){					throw new IOException(						MessageFormat.format(							labels.getString("tempdeleteerror"),							new String[] {temp.toString()}						)					);				}			}		} finally {			if (in != null){				in.close();				in = null;			}			if (out != null){				out.flush();				out.close();				out = null;			}		}		return modified;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -