检查文件

@throws IORuntimeException  IO异常
355486
private void checkFile() throws IORuntimeException {
		Assert.notNull(file, "File to write content is null !");
		if(this.file.exists() && false == file.isFile()){
			throw new IORuntimeException("File [{}] is not a file !", this.file.getAbsoluteFile());
		}
	}
374081
public static String file(File file) throws IOException {
        FileInputStream fi = null;
        try {
            fi = new FileInputStream(file);
            return stream(fi, file.length());
        } finally {
            if (fi != null) {
                try {
                    fi.close();
                } catch (Throwable t) {
                }
            }
        }

    }
845513
public static String readFile(InputStream file) {
        try (BufferedInputStream stream = new BufferedInputStream(file)) {
            byte[] buff = new byte[1024];
            StringBuilder builder = new StringBuilder();
            int read;
            while ((read = stream.read(buff)) != -1) {
                builder.append(new String(buff, 0, read, StandardCharsets.UTF_8));
            }
            return builder.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
996309
public static FastByteArrayOutputStream read(InputStream in) throws IORuntimeException {
		final FastByteArrayOutputStream out = new FastByteArrayOutputStream();
		copy(in, out);
		return out;
	}
147851
public static void writeToStream(String fullFilePath, OutputStream out) throws IORuntimeException {
		writeToStream(touch(fullFilePath), out);
	}
487336
public static List<String> readUtf8Lines(File file) throws IORuntimeException {
		return readLines(file, CharsetUtil.CHARSET_UTF_8);
	}
467050
public static <T> T load(File file, Charset charset, ReaderHandler<T> readerHandler) throws IORuntimeException {
		return FileReader.create(file, charset).read(readerHandler);
	}
1248581
public File write(byte[] data, int off, int len) throws IORuntimeException {
		return write(data, off, len, false);
	}
95919
public static FileInputStream toStream(File file) {
		try {
			return new FileInputStream(file);
		} catch (FileNotFoundException e) {
			throw new IORuntimeException(e);
		}
	}
482073
public static void copyFile(File from, File to) throws RuntimeIOException {
		copyFile(from, to, false);
	}
284712
@Override
    public <T> T runScript(File sourceFile) throws IOException {
        try (FileReader reader = new FileReader(sourceFile)) {
            return runScript(reader, sourceFile.getPath());
        }
    }
41871
public static File writeBytes(byte[] data, File dest) throws IORuntimeException {
		return writeBytes(data, dest, 0, data.length, false);
	}
414639
private List<String> readLine() throws IORuntimeException {
		final List<String> currentFields = new ArrayList<>(maxFieldCount > 0 ? maxFieldCount : DEFAULT_ROW_CAPACITY);

		final StrBuilder localCurrentField = currentField;
		final char[] localBuf = this.buf;
		int localBufPos = bufPos;//当前位置
		int localPreChar = preChar;//前一个特殊分界字符
		int localCopyStart = copyStart;//拷贝起始位置
		int copyLen = 0; //拷贝长度

		while (true) {
			if (bufLen == localBufPos) {
				// 此Buffer读取结束，开始读取下一段

				if (copyLen > 0) {
					localCurrentField.append(localBuf, localCopyStart, copyLen);
				}
				try {
					bufLen = reader.read(localBuf);
				} catch (IOException e) {
					throw new IORuntimeException(e);
				}

				if (bufLen < 0) {
					// CSV读取结束
					finished = true;

					if (localPreChar == config.fieldSeparator || localCurrentField.hasContent()) {
						//剩余部分作为一个字段
						currentFields.add(localCurrentField.toStringAndReset());
					}
					break;
				}

				//重置
				localCopyStart = localBufPos = copyLen = 0;
			}

			final char c = localBuf[localBufPos++];

			if (inQuotes) {
				//引号内，做为内容，直到引号结束
				if (c == config.textDelimiter) {
					// End of quoted text
					inQuotes = false;
				} else {
					if ((c == CharUtil.CR || c == CharUtil.LF) && localPreChar != CharUtil.CR) {
						lineNo++;
					}
				}
				copyLen++;
			} else {
				if (c == config.fieldSeparator) {
					//一个字段结束
					if (copyLen > 0) {
						localCurrentField.append(localBuf, localCopyStart, copyLen);
						copyLen = 0;
					}
					currentFields.add(StrUtil.unWrap(localCurrentField.toStringAndReset(), config.textDelimiter));
					localCopyStart = localBufPos;
				} else if (c == config.textDelimiter) {
					// 引号开始
					inQuotes = true;
					copyLen++;
				} else if (c == CharUtil.CR) {
					if (copyLen > 0) {
						localCurrentField.append(localBuf, localCopyStart, copyLen);
					}
					currentFields.add(StrUtil.unWrap(localCurrentField.toStringAndReset(), config.textDelimiter));
					localPreChar = c;
					localCopyStart = localBufPos;
					break;
				} else if (c == CharUtil.LF) {
					if (localPreChar != CharUtil.CR) {
						if (copyLen > 0) {
							localCurrentField.append(localBuf, localCopyStart, copyLen);
						}
						currentFields.add(StrUtil.unWrap(localCurrentField.toStringAndReset(), config.textDelimiter));
						localPreChar = c;
						localCopyStart = localBufPos;
						break;
					}
					localCopyStart = localBufPos;
				} else {
					copyLen++;
				}
			}

			localPreChar = c;
		}

		// restore fields
		bufPos = localBufPos;
		preChar = localPreChar;
		copyStart = localCopyStart;

		return currentFields;
	}
179912
private void readBody(InputStream in) throws IORuntimeException {
		if (ignoreBody) {
			return;
		}

		int contentLength = Convert.toInt(header(Header.CONTENT_LENGTH), 0);
		final FastByteArrayOutputStream out = contentLength > 0 ? new FastByteArrayOutputStream(contentLength) : new FastByteArrayOutputStream();
		try {
			IoUtil.copy(in, out);
		} catch (IORuntimeException e) {
			if (e.getCause() instanceof EOFException || StrUtil.containsIgnoreCase(e.getMessage(), "Premature EOF")) {
				// 忽略读取HTTP流中的EOF错误
			} else {
				throw e;
			}
		}
		this.bodyBytes = out.toByteArray();
	}
14161
public <T> File writeLines(Collection<T> list) throws IORuntimeException {
		return writeLines(list, false);
	}
1337306
public static String readString(File file, Charset charset) throws IORuntimeException {
		return FileReader.create(file, charset).readString();
	}
766060
private function open()
    {
        if (null === ($this->handle = fopen($this->file, 'rb'))) {
            $this->handle = null;

            throw new RuntimeException(
                sprintf(
                    'The file "%s" could not be opened for reading.',
                    $this->file
                )
            );
        }
    }