scala> import io.Source
import io.Source
scala> import java.io.File
import java.io.File
scala> val f = new File(".")
f: java.io.File = .
scala> for (l <- f.listFiles) {
| val src = Source.fromFile(l).getLines
| println( (0 /: src) { (i, line) => i + 1 } )
| }
3658
java.nio.BufferUnderflowException
at java.nio.Buffer.nextGetIndex(Unknown Source)
at java.nio.HeapCharBuffer.get(Unknown Source)
at scala.io.BufferedSource$$anon$2.next(BufferedSource.scala:86)
at scala.io.BufferedSource$$anon$2.next(BufferedSource.scala:74)
at scala.io.Source$$anon$6.next(Source.scala:307)
at scala.io.Source$$anon$6.next(Source.scala:301)
at scala.Iterator$cla...
Run Code Online (Sandbox Code Playgroud)
为什么我这样做java.nio.BufferUnderflowException?
注意 - 我正在处理10个日志文件,每个文件大小约为1MB
我正在用Java编写一个小型UDP服务器.当服务器收到命令('GET_VIDEO')时,他会读取一个文件('video.raw'),然后将其发送给客户端.
这是我的代码:
public class ServeurBouchon {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
byte[] buff = new byte[64];
int port = 8080;
DatagramPacket packet = new DatagramPacket(buff, buff.length);
DatagramSocket socket = new DatagramSocket(port);
System.out.println("Server started at 8080 ...");
while (true) {
socket.receive(packet);
new ThreadVideo(socket, packet).run();
}
}
public static class ThreadVideo extends Thread {
private DatagramSocket socket;
private DatagramPacket packet;
public ThreadVideo(DatagramSocket socket, DatagramPacket packet) {
this.packet = packet;
this.socket = socket;
}
public …Run Code Online (Sandbox Code Playgroud) 我正在写一个文件的值.
值写得正确.在另一个应用程序中,我可以无任何例外地读取文件.
但是在我的新应用程序中,我Bufferunderflowexception在尝试读取文件时得到了一个.
我花了好几天来解决这个问题,但我只是不知道如何解决它.
做了很多研究.
该bufferunderflowexception指:
Double X1 = mappedByteBufferOut.getDouble(); //8 byte (double)
Run Code Online (Sandbox Code Playgroud)
这是我读取文件的代码:
@Override
public void paintComponent(Graphics g) {
RandomAccessFile randomAccessFile = null;
MappedByteBuffer mappedByteBufferOut = null;
FileChannel fileChannel = null;
try {
super.paintComponent(g);
File file = new File("/home/user/Desktop/File");
randomAccessFile = new RandomAccessFile(file, "r");
fileChannel = randomAccessFile.getChannel();
mappedByteBufferOut = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, randomAccessFile.length());
while (mappedByteBufferOut.hasRemaining()) {
Double X1 = mappedByteBufferOut.getDouble(); //8 byte (double)
Double Y1 = mappedByteBufferOut.getDouble();
Double X2 = mappedByteBufferOut.getDouble();
Double Y2 = mappedByteBufferOut.getDouble();
int …Run Code Online (Sandbox Code Playgroud) java filechannel exception bufferunderflowexception paintcomponent