如何将从读取文本文件中检索到的每一行转换为字符串。例如:
RandomAccessFile file = new RandomAccessFile("C:text.txt", "r");
FileChannel channel = file.getChannel();
System.out.println("Size: " + channel.size());
ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
channel.read(buffer);
buffer.flip();//Restore buffer to position 0 to read it
System.out.println("Read ... ");
for (int i = 0; i < channel.size(); i++) {
System.out.print((char) buffer.get());
}
我试图在for循环中添加以下内容,每次都在“stringValueOf”中获取每一行,而是分别显示每个字符而不是每一行。
String stringValueOf = String.valueOf((char) buffer.get());
答案 0 :(得分:0)
InputStreamReader用于读取字符。 Java提供了BufferedReader,使读取行变得简单。
以下代码段从文件中读取行并打印到标准输出。
%ICONV% --from-code=utf-8 utf-8 --to-code=latin1 latin1 %1 > a.a