java-Java 里把 InputStream 转换成 String 的几种方法

1. 使用 JDK 5 的 Scanner

package cc.unmi.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Scanner;

/**
 * 
 * @author Unmi
 * @Creation date: 2013-02-01
 */
public class Test {

    /**
     * @param args
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException {
        InputStream inputStream = new FileInputStream("d:/sample.txt");
        Scanner scanner = new Scanner(inputStream, "UTF-8");
        String text = scanner.useDelimiter("\\A").next();
        System.out.println(text);
        scanner.close();
    }
}

2. JDK1.4 及之前的 BufferedReader 法

package cc.unmi.test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 
 * @author Unmi
 * @Creation date: 2013-02-01
 */
public class Test {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream("d:/sample.txt");
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        boolean firstLine = true;
        String line = null; ;
        while((line = bufferedReader.readLine()) != null){
            if(!firstLine){
                stringBuilder.append(System.getProperty("line.separator"));
            }else{
                firstLine = false;
            }
            stringBuilder.append(line);
        }
        System.out.println(stringBuilder.toString());
    }
}

中间那些判断是不是第一行来决定是否加换行符是些杂音。

3. JDK1.4 及之前的 readBytes 法

package cc.unmi.test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 
 * @author Unmi
 * @Creation date: 2013-02-01
 */
public class Test {

    /**
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream("d:/sample.txt");
        
        byte[] buffer = new byte[2048];
        int readBytes = 0;
        StringBuilder stringBuilder = new StringBuilder();
        while((readBytes = inputStream.read(buffer)) > 0){
            stringBuilder.append(new String(buffer, 0, readBytes));
        }
        
        System.out.println(stringBuilder.toString());
    }
}

缓冲区的大小自己根据实际来调,比 BufferedReader 还简洁些,不需管换行符的事情。

4. Apache commons IOUtils.toString 法

package cc.unmi.test;

import java.io.*;

import org.apache.commons.io.IOUtils;

/**
 * 
 * @author Unmi
 * @Creation date: 2013-02-01
 */
public class Test {

    /**
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream("d:/sample.txt");
        String text = IOUtils.toString(inputStream);
        System.out.println(text);
    }
}

第三方库就是第三方库,人家充分考虑到了你的感受,你对 JDK 库的抱怨,多简洁,一行搞定。IOUtils 还能把内容拷入其他的 Writer 中,如 IOUtils.copy(inputStream, new StringWriter())。

5. Google guava 的  CharStreams 方法

package cc.unmi.test;

import java.io.*;

import com.google.common.io.CharStreams;

/**
 * 
 * @author Unmi
 * @Creation date: 2013-02-01
 */
public class Test {

    /**
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        InputStream inputStream = new FileInputStream("d:/sample.txt");
        String text = CharStreams.toString(new InputStreamReader(inputStream, "UTF-8"));
        System.out.println(text);
    }
}

CharSteams 不是直接作用在 InputSteam 上的,还要靠 InputStreamReader 拱个桥。

6. JDK 7 的 NIO readAllBytes

package cc.unmi.test;

import java.io.IOException;
import java.nio.file.*;

/**
 * 
 * @author Unmi
 * @Creation date: 2013-02-01
 */
public class Test {

    /**
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        byte[] bytes = Files.readAllBytes(Paths.get("d:/sample.txt"));
        String text = new String(bytes);
        System.out.println(text);
    }
}

这让我们相信 JDK  一直还有人在管,虽然不可能象动态语言的方法那么快捷,上面的  readAllBytes 在处理大文件时肯定会很被动的。而 Files.readAllLines 会把文件的内容读入一个 List<String> 对象中,往内存不断放东西就得掂量下内存会不会被爆。在 java.nio.file.* 还有很多新事物可供发掘。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 使用Apache POI库进行转换: ```java import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.converter.WordToConverter; import org.apache.poi.hwpf.usermodel.Range; import java.io.*; public class DocToDocxConverter { public static void main(String[] args) throws Exception { // 读取doc文件 InputStream inputStream = new FileInputStream("test.doc"); HWPFDocument document = new HWPFDocument(inputStream); // 转换为docx文件 WordToConverter converter = new WordToConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); converter.processDocument(document); // 写入docx文件 OutputStream outputStream = new FileOutputStream("test.docx"); converter.getDocument().write(outputStream); // 关闭 outputStream.close(); inputStream.close(); } } ``` 2. 使用Aspose.Words库进行转换: ```java import com.aspose.words.Document; import com.aspose.words.SaveFormat; import java.io.*; public class DocToDocxConverter { public static void main(String[] args) throws Exception { // 读取doc文件 InputStream inputStream = new FileInputStream("test.doc"); Document document = new Document(inputStream); // 转换为docx文件 document.save("test.docx", SaveFormat.DOCX); // 关闭 inputStream.close(); } } ``` 3. 使用JodConverter库进行转换: ```java import com.documents4j.api.DocumentType; import com.documents4j.api.IConverter; import com.documents4j.job.LocalConverter; import java.io.*; public class DocToDocxConverter { public static void main(String[] args) throws Exception { // 创建IConverter对象 IConverter converter = LocalConverter.builder().build(); // 读取doc文件 InputStream inputStream = new FileInputStream("test.doc"); // 写入docx文件 OutputStream outputStream = new FileOutputStream("test.docx"); // 转换为docx文件 converter.convert(inputStream).as(DocumentType.MS_WORD).to(outputStream).as(DocumentType.DOCX).execute(); // 关闭 outputStream.close(); inputStream.close(); // 关闭IConverter对象 converter.shutDown(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值