5

I'm trying to read /dev/input/js0 from Java but I keep getting

java.io.IOException: Invalid argument
at java.io.FileInputStream.read0(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:207)
at Test.main(Test.java:7)

My code is

import java.io.*;

final class Test {
    public static final void main(String[] args) {
            try {
                    FileInputStream in = new FileInputStream("/dev/input/js0");
                    System.out.println(in.read());
            } catch(IOException e) {
                    e.printStackTrace();
            }
    }
}

My end goal is to be able to read input from my controller but I can't even seem to read one byte. What am I doing wrong? My user does have read and write access to the file.

1 Answer 1

4

Wrapping the FileInputStream in a BufferedInputStream seems to fix the issue.

Sign up to request clarification or add additional context in comments.

2 Comments

What did you do once you had this information? I can't seem to figure out the byte structure.
@MatthewSegreti the structure is listed here kernel.org/doc/Documentation/input/joystick-api.txt what I recommend is doing DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("/dev/input/js0"))); and then using the DataInputStream to read data since you can easily read into int, short, and byte data types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.