我有一个BufferedinputStream,它包含文件文件的byte []表示,在此之前它包含文件名(“FileName.fileExtension”) 我想读取第一行,然后读取文件的字节表示,以便我可以将它转换为它的扩展名 我试过这个,但它不起作用,因为我必须保持文件的byte []表示
BufferedInputStream bis=new BufferedInputStream(sr.getInputStream());
BufferedReader in=new BufferedReader(new InputStreamReader(sr.getInputStream()));
String Filename=in.readLine();
String y=in.lines().collect(Collectors.joining());
答案 0 :(得分:0)
您不必总是使用流,但您需要关闭资源。也许您可以使用标准Scanner
和ByteArrayOutputStream
:
String fileName = null;
byte[] content = null;
try (InputStream in = sr.getInputStream()) {
Scanner s = new Scanner(in); // might have to call useDelimiter()
fileName = s.nextLine(); // skips line delimiter from the stream
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int read;
byte[] data = new byte[1024];
while ((read = in.read(data, 0, data.length)) != -1) {
buf.write(data, 0, read);
}
content = buf.toByteArray();
}
答案 1 :(得分:0)
您可以通过以下方式执行此操作:
List<String> data = new ArrayList<>();
//Read all the lines first.
List<String> lines = Files.lines(Paths.get(Filename)).collect(Collectors.toList());
//Store the first line in a header
String header = lines.stream().findFirst().get();
//Store the remaining lines in a list of lines.
lines.stream().skip(1).forEach(s -> data.add(s));