我有一个包含以下内容的文件:
This is the content to write into file
POST /api/v1/jobs HTTP/1.1
Cache-Control: no-cache
---------------------------A495ukjfasdfddrg4hztzu898aA0jklm
Content-Disposition: form-data; name="file"; filename="sample.txt"
Content-Type: text/plain
X-compId: data
Content-Length: 10
1234567891
---------------------------A495ukjfasdfddrg4hztzu898aA0jklm
当我尝试读取此文件中的换行符时,它无法识别换行符。 我知道我们有BufferedReader,它逐行读取内容,但我需要识别此文件中的新行。下面是我用来查找换行符索引的代码。
byte[] buffer = new byte[READ_BUFFER_SIZE];
int bytesRead=0;
InputStream _baseStream = new FileInputStream("slot.txt");
BufferedInputStream buff =new BufferedInputStream(_baseStream);
buff.mark(10);
try {
bytesRead = buff.read(buffer, 0, READ_BUFFER_SIZE);
System.out.println("1. "+bytesRead);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String stringBuffer = new String(buffer,java.nio.charset.Charset.defaultCharset());
int indexOfCrLf = stringBuffer.indexOf("\r\n");
int indexOfPageStart = stringBuffer.indexOf("\r\n\r\n");
我也尝试过使用
String newline = System.getProperty("line.separator");
对于换行符char.still,我将索引设为-1。