Java 如何无损读取文本文件呢?
以下是有损的
- @Deprecated
- public static String getFullContent(File file, String charset) {
- BufferedReader reader = null;
- if (!file.exists()) {
- System.out.println("getFullContent: file(" + file.getAbsolutePath()
- + ") does not exist.");
- return null;
- }
- if (charset == null) {
- charset = SystemHWUtil.CHARSET_ISO88591;
- }
- try {
- reader = getBufferReaderFromFile(file, charset);
- return getFullContent(reader);
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- } finally {
- if (null != reader) {
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return null;
- }
- public static BufferedReader getBufferReaderFromFile(File file,
- String charset) throws FileNotFoundException {
- InputStream ss = new FileInputStream(file);
- InputStreamReader ireader;
- BufferedReader reader = null;
- try {
- if (charset == null) {
- ireader = new InputStreamReader(ss,
- SystemHWUtil.CHARSET_ISO88591);
- } else {
- ireader = new InputStreamReader(ss, charset);
- }
- reader = new BufferedReader(ireader);
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return reader;
- }
- /**
- * have closed reader
- *
- * @param reader
- * @return
- */
- @Deprecated
- public static String getFullContent(BufferedReader reader) {
- StringBuilder sb = new StringBuilder();
- String readedLine = null;
- try {
- while ((readedLine = reader.readLine()) != null) {
- sb.append(readedLine);
- sb.append(SystemHWUtil.CRLF);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- String content = sb.toString();
- int length_CRLF = SystemHWUtil.CRLF.length();
- if (content.length() <= length_CRLF) {
- return content;
- }
- return content.substring(0, content.length() - length_CRLF);//
- }
测试:
- @Test
- public void test_getFullContent(){
- String filepath="D:\\bin\\config\\conf_passwd.properties";
- try {
- InputStream in =new FileInputStream(filepath);
- System.out.print(FileUtils.getFullContent(filepath, "UTF-8"));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
介绍三种无损读取的方式
方式一:使用InputStreamReader,指定编码
- /***
- * 指定字符编码,无损地读取文本文件.
- *
- * @param in
- * : 输入流,会关闭
- * @param charset
- * : 字符编码
- * @return
- * @throws IOException
- */
- public static String getFullContent3(InputStream in, String charset)
- throws IOException {
- StringBuffer sbuffer = new StringBuffer();
- InputStreamReader inReader;
- //设置字符编码
- inReader = new InputStreamReader(in, charset);
- char[] ch = new char[SystemHWUtil.BUFF_SIZE_1024];
- int readCount = 0;
- while ((readCount = inReader.read(ch)) != -1) {
- sbuffer.append(ch, 0, readCount);
- }
- inReader.close();
- in.close();
- return sbuffer.toString();
- }
测试:
- @Test
- public void test_getFullContent3(){
- String filepath="D:\\bin\\config\\conf_passwd.properties";
- try {
- InputStream in =new FileInputStream(filepath);
- System.out.print(FileUtils.getFullContent3(in, "UTF-8"));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
方式二:先读取出字节数组,再使用String的构造方法
- public static String getFullContent4(InputStream in, String charset) throws IOException{
- byte[]bytes=FileUtils.readBytes3(in);
- return new String(bytes,charset);
- }
- /***
- * Has been tested
- *
- * @param in
- * @return
- * @throws IOException
- */
- public static byte[] readBytes3(InputStream in) throws IOException {
- BufferedInputStream bufin = new BufferedInputStream(in);
- int buffSize = BUFFSIZE_1024;
- ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize);
- // System.out.println("Available bytes:" + in.available());
- byte[] temp = new byte[buffSize];
- int size = 0;
- while ((size = bufin.read(temp)) != -1) {
- out.write(temp, 0, size);
- }
- bufin.close();
- in.close();
- byte[] content = out.toByteArray();
- out.flush();
- out.close();
- return content;
- }
方式三:使用System.arraycopy,所以效率不高,因为有拷贝操作(不推荐)
- public static String getFullContent2(InputStream in, String charset)
- throws IOException {
- int step = BUFFSIZE_1024;
- BufferedInputStream bis = new BufferedInputStream(in);
- // Data's byte array
- byte[] receData = new byte[step];
- // data length read from the stream
- int readLength = 0;
- // data Array offset
- int offset = 0;
- // Data array length
- int byteLength = step;
- while ((readLength = bis.read(receData, offset, byteLength - offset)) != -1) {
- // Calculate the current length of the data
- offset += readLength;
- // Determine whether you need to copy data , when the remaining
- // space is less than step / 2, copy the data
- if (byteLength - offset <= step / 2) {
- byte[] tempData = new byte[receData.length + step];
- System.arraycopy(receData, 0, tempData, 0, offset);
- receData = tempData;
- byteLength = receData.length;
- }
- }
- return new String(receData, 0, offset, charset);
- }
时间: 2024-10-28 21:23:20