InputStreamReader的基本用法
// Reads a single character
// return The character read, or -1 if the end of the stream has been reached
int read();
int read(char[] cbuf, int offset, int length);
int read(char[] cbuf);
int read(CharBuffer target);
举个栗子
package com.wondream.myframework.app.basictest.io;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class InputStreamReaderTest {
Log logger = LogFactory.getLog(InputStreamReaderTest.class);
@Test
public void execute(){
InputStreamReader isr = null;
try {
File file = new File("./logs/oswData.txt");
isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
int len = 0;
char[] data = new char[4];
StringBuilder sb = new StringBuilder();
while((len = isr.read(data, 0, 4))!=-1){
sb.append(data, 0, len);
}
logger.info(sb.toString());
} catch (Exception e){
} finally {
try {
if(isr!=null){
isr.close();
isr = null;
}
} catch (Exception e){}
}
}
}