1

Do I can open a file (linux character device) for read+write, and use the two classes to implement a dialog like client-server?

Something like this:

File file = new File("/dev/ttyS0");  
FileOutpuStream fo = new FileOutputStream(file)
FileInputStream fi = new FileInputStream(file)

After the above declarations, can I continuously send pollings (questions) to the file, and read its replies? (Of course, attached to ttyS0 there is a kind of server)

5
  • 1
    This sounds more like an anti pattern. The normal thing in Java when you have to read and write from/to the same file would be the RandomAccessFile class. Beyond that, I have a bit of doubt that this can work when both streams are kept open. But then: what exactly prevents you from trying? Commented Jul 7, 2019 at 9:43
  • @GhostCat I am trying to collect information, because I'm new to Java. Good information is RandomAccessFile, but does it work with character devices - which are not "random"? Commented Jul 7, 2019 at 10:05
  • @GhostCat: anyway... what is an "anti pattern"? As you see, I do not chew Java! Commented Jul 7, 2019 at 10:06
  • Anti pattern: having two streams ending in the same file. Commented Jul 7, 2019 at 10:08
  • 1
    This is not a regular file. It is a device "file". Commented Jul 7, 2019 at 11:35

3 Answers 3

3

I was not able to test it, but you might want to give RandomAccessFile a try. It does not give you the opertunity to create streams, but it implements DataInput and DataOutput. Thats maybe good enough for your purpose? RandomAccessFile docs

String file = "/dev/ttyS0";
try {
    RandomAccessFile f = new RandomAccessFile(file, "rwd");
} catch (IOException e){
    e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

Comments

0

The /dev/ttyS0 file is a device file for a serial terminal.

If the device has been configured appropriately to connect to a serial terminal line, then you should be able to read and write like that. However, on a typical desktop or laptop, it probably won't work because there won't be connected serial line.

(For example, when I do this on my PC:

$ sudo bash -c "cat < /dev/ttyS0"

I get this:

cat: -: Input/output error

which is saying that the device cannot be read from.)

Note that a /dev/tty* device does not behave like a regular file. The characters that are written in no way relate to the characters that you read back. Also note that it is not possible to make ioctl requests using the standard Java APIs. So configuring the terminal driver from Java would be problematic.


If you were talking abour reading and writing a regular file, it should work too. However, the behavior could be a rather confusing, especially if you have buffering in your streams. One issue you need to deal with is that the two file descriptors are independent of each other.

If you need to do this kind of thing with a regular file, you should probably use RandomAccessFile.

1 Comment

TY for your explanation, I know what a character device is and how to configure it. What I don't know is how to read and write to it from java in Android - and my question is about that only. Android runs on top of a Linux kernel, so two different file descriptors could work well, but RandomAccessFile looks suspicious. I will test tomorrow.
0

I didn't try RandomAccessFile, it could also work... it worked smoothly with FileInputStream and FileOutputStream, see this answer in SO: https://stackoverflow.com/a/56935267/7332147

Comments

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.