| | |
| | | } |
| | | |
| | | /** |
| | | * 读取文件某行 |
| | | * @param inputStream |
| | | * @param lineIndex 行索引 0based |
| | | * @return 行文本 |
| | | * @throws IOException |
| | | */ |
| | | public static String readLineAt(InputStream inputStream,int lineIndex) throws IOException { |
| | | byte[] bytes = inputStream.readAllBytes(); |
| | | ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes); |
| | | Charset charset = Charsets.charset(detectFromInputStream(byteStream)); |
| | | byteStream.reset();//重置,使继续可用 |
| | | |
| | | String lineText = ""; |
| | | try (BufferedReader reader = new BufferedReader(new InputStreamReader(byteStream,charset))) { |
| | | // 跳过第一行 |
| | | int current = 0; |
| | | while(current<=lineIndex && lineText!=null) { |
| | | lineText = reader.readLine(); |
| | | current++; |
| | | } |
| | | } |
| | | return lineText; |
| | | } |
| | | |
| | | /** |
| | | * 从输入流获取文本 |
| | | * @param ins 束流 |
| | | * @return 文本字符 |