Java 从InputStream中删除换行符
我想从Java 从InputStream中删除换行符,java,file,inputstream,Java,File,Inputstream,我想从java.io.InputStream中删除所有换行符(用于\n和\r\n),读取文件时,相应的方法如下所示: /** * @param target {@linkplain File} * @return {@linkplain InputStream} * @throws Exception */ protected InputStream initInput(final File file) throws Exception { InputStream str
java.io.InputStream
中删除所有换行符(用于\n和\r\n),读取文件时,相应的方法如下所示:
/**
* @param target {@linkplain File}
* @return {@linkplain InputStream}
* @throws Exception
*/
protected InputStream initInput(final File file)
throws Exception {
InputStream stream = null;
try {
if (file.isDirectory()) {
// throw exception
}
if (!file.exists()) {
// throw another exception
}
//
// *remove newlines here*
//
stream = new FileInputStream(file);
} catch (FileNotFoundException e) {
// throw another exception
}
return stream;
}
有时,标准Java库无法提供足够的方法来操作其核心类。ApacheCommonsLang提供了这些额外的方法
如果您可以使用该库,则该方法可能很有用。您可以将其转换为字符串,并将新行字符替换为零:
InputStream is = new ByteArrayInputStream("file content".getBytes());
//read it with BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line.replace("\r","").replace("\n",""))
System.out.println(sb.toString());
考虑到您的文本不包含与您相关的“\n”和“\r”,这将是一件好事。您可以自己实现
java.io.FileInputStream
,并在阅读时跳过\r
和\n
覆盖读取方法
Hier是示例实现(没有任何错误处理)
对于一些基本测试
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import junit.framework.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class NoNewLineFileInputStreamTest {
private final static String txt = "testnl.txt";
@BeforeClass
public static void genTestFile() throws IOException {
OutputStream os = new FileOutputStream(txt);
os.write((
"Hello\n" +
",\r\n" +
"World!\r" +
"").getBytes());
os.close();
}
@Test
public void readInt() throws IOException {
InputStream is = new NoNewLineFileInputStream(txt);
int c = is.read();
while(c != -1) {
Assert.assertTrue(c != '\n' && c != '\r');
c = is.read();
}
is.close();
}
@Test
public void readBytes() throws IOException {
InputStream is = new NoNewLineFileInputStream(txt);
int l = is.available();
if(l > 0) {
byte[] content = new byte[l];
int n = is.read(content);
String expected = "Hello,World!";
Assert.assertEquals(expected.getBytes().length, n);
Assert.assertEquals(expected, new String(content, 0, n));
}
is.close();
}
@Test
public void readBytesOffset() throws IOException {
InputStream is = new NoNewLineFileInputStream(txt);
int l = is.available();
if(l > 0) {
byte[] content = new byte[l*3];
int n = is.read(content, 3, 5);
String expected = "Hello";
Assert.assertEquals(expected.getBytes().length, n);
Assert.assertEquals(expected, new String(content, 3, n));
}
is.close();
}
}
您的方法如下所示
/**
* @param target {@linkplain File}
* @return {@linkplain InputStream}
* @throws Exception
*/
protected InputStream initInput(final File file)
throws Exception {
InputStream stream = null;
try {
if (file.isDirectory()) {
// throw exception
}
if (!file.exists()) {
// throw another exception
}
//
// read operations using this implementation will jump over all '\n' and '\r'
//
stream = new NoNewLineFileInputStream(file);
} catch (FileNotFoundException e) {
// throw another exception
}
return stream;
}
为了更好地与java.io.InputStream
抽象类兼容,您可能需要覆盖类中的所有方法
/**
* @param target {@linkplain File}
* @return {@linkplain InputStream}
* @throws Exception
*/
protected InputStream initInput(final File file)
throws Exception {
InputStream stream = null;
try {
if (file.isDirectory()) {
// throw exception
}
if (!file.exists()) {
// throw another exception
}
//
// read operations using this implementation will jump over all '\n' and '\r'
//
stream = new NoNewLineFileInputStream(file);
} catch (FileNotFoundException e) {
// throw another exception
}
return stream;
}