Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

FileInputStream ET FileOutputStream on same /dev/tty ?

8 réponses
Avatar
pcouas
Bonsoir

Et il possible de faire simultanement un FileInputStrream, et un
FileOutputStream, ouvert sur le meme /dev/tty ?
Afin de pouvoir lire et ecrire sur le tty ?

Cordialement
Philippe

8 réponses

Avatar
TestMan
Bonsoir

Et il possible de faire simultanement un FileInputStrream, et un
FileOutputStream, ouvert sur le meme /dev/tty ?
Afin de pouvoir lire et ecrire sur le tty ?

Cordialement
Philippe



Bonsoir,

http://java.sun.com/j2se/1.5.0/docs/api/java/io/RandomAccessFile.html ?

Si vous recherchez des besoins plus fin et un meilleur controle sur les
perfs utilisez le via un FileChannel :
http://java.sun.com/j2se/1.5.0/docs/api/java/io/RandomAccessFile.html#getChannel()

A+
TM

Avatar
pcouas
Bonjour

J'ai essaye

1
String portName=new String("/dev/ptyqf");
fos2 = new FileOutputStream(portName);

// fis2 = new FileInputStream(portName); // Ne fonctionne pas
FileDescriptor fd = fos2.getFD();
fis2 = new FileInputStream(fd);

=> Cela fonctionne en ecriture mais en lecture j'ai "Mauvais File
Descriptor !

2 Avec un RandomAccessFile
ra=new RandomAccessFile(portName,"rw");
FileDescriptor fd = ra.getFD();
fos2 = new FileOutputStream(fd);

=> le RandomAccessFile ne s'ouvre pas


Cordialement
Philippe
Avatar
pcouas
Les 3 options de RandomAccessFile ne donne pas mieux


ra=new RandomAccessFile(portName,"rw");
ra=new RandomAccessFile(portName,"rws");
ra=new RandomAccessFile(portName,"rwd");
Avatar
pcouas
Les 3 options de RandomAccessFile ne donne pas mieux


ra=new RandomAccessFile(portName,"rw");
ra=new RandomAccessFile(portName,"rws");
ra=new RandomAccessFile(portName,"rwd");
Avatar
TestMan
Bonjour

J'ai essaye

1
String portName=new String("/dev/ptyqf");
fos2 = new FileOutputStream(portName);

// fis2 = new FileInputStream(portName); // Ne fonctionne pas
FileDescriptor fd = fos2.getFD();
fis2 = new FileInputStream(fd);

=> Cela fonctionne en ecriture mais en lecture j'ai "Mauvais File
Descriptor !

2 Avec un RandomAccessFile
ra=new RandomAccessFile(portName,"rw");
FileDescriptor fd = ra.getFD();
fos2 = new FileOutputStream(fd);

=> le RandomAccessFile ne s'ouvre pas


Cordialement
Philippe



Bonjour,

Exception ?
Droits sur le pty ?

Essayez ensuite cet exemple utilisant les "nio" pour copier un fichier
vers le pty :
http://www.exampledepot.com/egs/java.nio/CreateMemMap.html

A+
TM

Avatar
pcouas
Le messsage d'erreur est /dev/ptyqf (Erreur d'entrée/sortie)

et les droits crw-rw-rw- 1 root tty 2, 31 fév 18 2004
ptyqf

J'essaye ta suggestion
Avatar
pcouas
J'ai deux tests

Le 1er Class15 fonctionne en Output mais pas en Input ou le Message Bad
Descriptor est génére
Le voici


import java.io.*;

public class Class15
{
String portName=null;
FileOutputStream fos2 = null;
FileInputStream fis2 = null;


public byte[] toBytes(char[] from) {
byte[] result = new byte[from.length];

for (int i = 0; i < from.length; i++) {
result[i] = (byte)from[i];
}

return result;
}




public Class15(String mportName) throws Exception
{
portName=mportName;

System.out.println("cl01 portName "+portName);



fos2 = new FileOutputStream(portName);

System.out.println("cl02 portName "+portName);

// fis2 = new FileInputStream(portName); // Ne
fonctionne pas
FileDescriptor fd = fos2.getFD();
fis2 = new FileInputStream(fd);
//fis2 = new FileInputStream(fd.in);
System.out.println("cl03 portName "+portName);

System.out.println("cl04 portName fis2
"+fis2.available());

}

public void go(String xx)
{

try{
fos2.write(toBytes(xx.toCharArray()));
fos2.write('n');
fos2.flush();

// fos2.close();
} catch(Exception e)
{
System.out.println("Erreur1 "+e.getMessage());

}

}





public String read(){
String rt=null;
try{
System.out.println("r01 *"+portName+"*");

System.out.println("r03 available
*"+fis2.available());
System.out.println("r04 valid
*"+fis2.getFD().valid());

while (true) {

System.out.println("r05 *"+portName+"*");
byte b[] = new byte[100];
// Read some data from the modem.
int nbytes = fis2.read(b);
System.out.println("r06 *"+nbytes+"*");


System.out.println (new String (b, 0, nbytes));
}
}
catch(Exception e1){
System.out.println(e1.getMessage());

}



return rt;
}



public void close() {
try{
fos2.close();
fis2.close();
}catch(Exception e1){}
}


/**
*
* @param args
*/
public static void main(String[] args)
{
String portName=new String("/dev/ptyqf");
String xx=new String("COUCOU DE JAVA"); // Demande de l'Heure

if(args.length>0)
portName = args[0]; // ex /dev/ttyq0
try{
Class15 x=new Class15(portName);

System.out.println("PORT OUVERT, lancer le BASIC avant de
Valider!");
System.out.println("APPUYER SUR ENTREE pour l'envoi");

System.out.println("fis2 "+x.fis2.available());


//Lecture du clavier
BufferedReader entree=new BufferedReader(new
InputStreamReader(System.in));

while(entree.readLine()!=null &&
!entree.readLine().equalsIgnoreCase("f"))
{
System.out.println("GO");

System.out.println("fis2B "+x.fis2.available());
x.go(xx);
System.out.println("fis2c "+x.fis2.available());


//Test de lecture depuis le BASIC sur le pty
String lu=x.read();
System.out.println("LU depuis le BASIC "+lu);

}
System.out.println("FERMETURE DU FLUX!");
x.close();
}
catch(Exception e1){System.out.println(e1.getMessage());}
}
}

---------------------------------------------------------------
Le 2eme avec les NIO s'arrete pendant l'ecriture
au niveau du buf.hasRemaining() et l'ecriture ne se fait pas


import java.io.BufferedReader;
import java.io.FileOutputStream;

import java.io.InputStreamReader;

import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;

public class Class20 {

WritableByteChannel channel =null;
String portName=new String("/dev/ptyqf");
String xx=new String("COUCOU DE JAVA"); // Demande de l'Heur


public Class20() throws Exception {
System.out.println("cl01 portName "+portName+"
"+this.getClass().getName());
channel = new FileOutputStream( portName).getChannel();
System.out.println("cl02 portName "+portName+"
"+this.getClass().getName());
}


public byte[] toBytes(char[] from) {
byte[] result = new byte[from.length];

for (int i = 0; i < from.length; i++) {
result[i] = (byte)from[i];
}

return result;
}




public void write()
{
System.out.println("write01");
try {

Character CR=new Character('n');

String zz=new String(xx);
zz=zz.concat(CR.toString());
ByteBuffer buf = ByteBuffer.allocateDirect(zz.length());


System.out.println("write02");

buf.put(toBytes(xx.toCharArray()));

System.out.println("write03");


System.out.println("write04");
// Set the limit to the current position and the
position to 0
// making the new bytes visible for write()
buf.flip();

System.out.println("write05");
// Write the bytes to the channel
int numWritten = channel.write(buf);

System.out.println("write06 "+numWritten );

// Check if all bytes were written
if (buf.hasRemaining()) {
System.out.println("write07");
// If not all bytes were written, move the
unwritten bytes
// to the beginning and set position just after the
last
// unwritten byte; also set limit to the capacity
buf.compact();
} else {
System.out.println("write08");
// Set the position to 0 and the limit to capacity
buf.clear();
}
// }
} catch (Exception e) {
}
System.out.println("write09");
}

public void closewrite() throws Exception
{
// Close the file
channel.close();
}


public static void main(String[] args)
{
try
{
Class20 x=new Class20();

//Lecture du clavier pour etre sur que le prog de test de
l'autre cote est ouvert
BufferedReader entree=new BufferedReader(new
InputStreamReader(System.in));


while(entree.readLine()!=null &&
!entree.readLine().equalsIgnoreCase("f"))
{
System.out.println("GO");
x.write();

}


x.closewrite();
}
catch(Exception e1) {
System.out.println(e1.getMessage());
}

}

}


Cordialement
Philippe
Avatar
niavlys75
pcouas a écrit le 06/12/2006 à 14h53 :
J'ai deux tests

Le 1er Class15 fonctionne en Output mais pas en Input ou le Message Bad
Descriptor est génére
Le voici


import java.io.*;

public class Class15
{
String portName=null;
FileOutputStream fos2 = null;
FileInputStream fis2 = null;


public byte[] toBytes(char[] from) {
byte[] result = new byte[from.length];

for (int i = 0; i < from.length; i++) {
result[i] = (byte)from[i];
}

return result;
}




public Class15(String mportName) throws Exception
{
portName=mportName;

System.out.println("cl01 portName "+portName);



fos2 = new FileOutputStream(portName);

System.out.println("cl02 portName "+portName);

// fis2 = new FileInputStream(portName); // Ne
fonctionne pas
FileDescriptor fd = fos2.getFD();
fis2 = new FileInputStream(fd);
//fis2 = new FileInputStream(fd.in);
System.out.println("cl03 portName "+portName);

System.out.println("cl04 portName fis2
"+fis2.available());

}

public void go(String xx)
{

try{
fos2.write(toBytes(xx.toCharArray()));
fos2.write('n');
fos2.flush();

// fos2.close();
} catch(Exception e)
{
System.out.println("Erreur1 "+e.getMessage());

}

}





public String read(){
String rt=null;
try{
System.out.println("r01 *"+portName+"*");

System.out.println("r03 available
*"+fis2.available());
System.out.println("r04 valid
*"+fis2.getFD().valid());

while (true) {

System.out.println("r05 *"+portName+"*");
byte b[] = new byte[100];
// Read some data from the modem.
int nbytes = fis2.read(b);
System.out.println("r06 *"+nbytes+"*");


System.out.println (new String (b, 0, nbytes));
}
}
catch(Exception e1){
System.out.println(e1.getMessage());

}



return rt;
}



public void close() {
try{
fos2.close();
fis2.close();
}catch(Exception e1){}
}


/**
*
* @param args
*/
public static void main(String[] args)
{
String portName=new String("/dev/ptyqf");
String xx=new String("COUCOU DE JAVA"); // Demande de l'Heure

if(args.length>0)
portName = args[0]; // ex /dev/ttyq0
try{
Class15 x=new Class15(portName);

System.out.println("PORT OUVERT, lancer le BASIC avant de
Valider!");
System.out.println("APPUYER SUR ENTREE pour l'envoi");

System.out.println("fis2 "+x.fis2.available());


//Lecture du clavier
BufferedReader entree=new BufferedReader(new
InputStreamReader(System.in));

while(entree.readLine()!=null &&
!entree.readLine().equalsIgnoreCase("f"))
{
System.out.println("GO");

System.out.println("fis2B "+x.fis2.available());
x.go(xx);
System.out.println("fis2c "+x.fis2.available());


//Test de lecture depuis le BASIC sur le pty
String lu=x.read();
System.out.println("LU depuis le BASIC "+lu);

}
System.out.println("FERMETURE DU FLUX!");
x.close();
}
catch(Exception e1){System.out.println(e1.getMessage());}
}
}

---------------------------------------------------------------
Le 2eme avec les NIO s'arrete pendant l'ecriture
au niveau du buf.hasRemaining() et l'ecriture ne se fait pas


import java.io.BufferedReader;
import java.io.FileOutputStream;

import java.io.InputStreamReader;

import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;

public class Class20 {

WritableByteChannel channel =null;
String portName=new String("/dev/ptyqf");
String xx=new String("COUCOU DE JAVA"); // Demande de l'Heur


public Class20() throws Exception {
System.out.println("cl01 portName "+portName+"
"+this.getClass().getName());
channel = new FileOutputStream( portName).getChannel();
System.out.println("cl02 portName "+portName+"
"+this.getClass().getName());
}


public byte[] toBytes(char[] from) {
byte[] result = new byte[from.length];

for (int i = 0; i < from.length; i++) {
result[i] = (byte)from[i];
}

return result;
}




public void write()
{
System.out.println("write01");
try {

Character CR=new Character('n');

String zz=new String(xx);
zz=zz.concat(CR.toString());
ByteBuffer buf = ByteBuffer.allocateDirect(zz.length());


System.out.println("write02");

buf.put(toBytes(xx.toCharArray()));

System.out.println("write03");


System.out.println("write04");
// Set the limit to the current position and the
position to 0
// making the new bytes visible for write()
buf.flip();

System.out.println("write05");
// Write the bytes to the channel
int numWritten = channel.write(buf);

System.out.println("write06 "+numWritten );

// Check if all bytes were written
if (buf.hasRemaining()) {
System.out.println("write07");
// If not all bytes were written, move the
unwritten bytes
// to the beginning and set position just after the
last
// unwritten byte; also set limit to the capacity
buf.compact();
} else {
System.out.println("write08");
// Set the position to 0 and the limit to capacity
buf.clear();
}
// }
} catch (Exception e) {
}
System.out.println("write09");
}

public void closewrite() throws Exception
{
// Close the file
channel.close();
}


public static void main(String[] args)
{
try
{
Class20 x=new Class20();

//Lecture du clavier pour etre sur que le prog de test de
l'autre cote est ouvert
BufferedReader entree=new BufferedReader(new
InputStreamReader(System.in));


while(entree.readLine()!=null &&
!entree.readLine().equalsIgnoreCase("f"))
{
System.out.println("GO");
x.write();

}


x.closewrite();
}
catch(Exception e1) {
System.out.println(e1.getMessage());
}

}

}


Cordialement
Philippe


Bonjours,
Je me permets d'ajouter une question.
J'essaie d'executer le process "vi" de linux en java. Evidemment j'ai des soucis tty.

Est ce que vous avez une idée sur la manière de faire la chose?
MERCI