一个是读取一行字符..返回为字符.String型
readInt是读取int 类型的吧.返回是int型.主要用于Date流文件类型.如你在DateOutputStream中定了一个int i=23。存入以后.然后用DateInputStream的readInt读出来以后就是一个int型..
那么可以这样写了
int ii =readInt(...) +333;
readInt()、readDouble()、readChar()都是从父类中继承而来的,用来读取相应类型的数据。没有什么回车符的限制
了解每种类型的字节数,高低位,就可以为所欲为了
呵呵
例子 double
long l;
l = b[0];
l &= 0xff;
l |= ((long) b[1] << 8);
l &= 0xffff;
l |= ((long) b[2] << 16);
l &= 0xffffff;
l |= ((long) b[3] << 24);
l &= 0xffffffffl;
l |= ((long) b[4] << 32);
l &= 0xffffffffffl;
l |= ((long) b[5] << 40);
l &= 0xffffffffffffl;
l |= ((long) b[6] << 48);
l |= ((long) b[7] << 56);
return Double.longBitsToDouble(l);
发一段我的程序和linux下的C通讯的代码,希望对你有所启示.
//发送获取资源列表的命令
Log.log("/n 开始发送获取资源列表的命令:-----------------/n");
outData.write(strHead.getBytes());
outData.writeByte(version);
outData.writeInt(reverseByte_32(serial));
outData.writeShort(reverseByte_16(type));
outData.writeShort(reverseByte_16(sub_type));
outData.writeInt(reverseByte_32(len));
outData.writeInt(reverseByte_32(result));
outData.writeLong(reverseByte_32(timeout));
//接收返回信息
Log.log("/n 开始接收~获取资源列表~返回信息:-----------------/n");
byte head[] = new byte[16];
inData.read(head);
String strHead_b = new String(head, "GB2312");
byte version_b = inData.readByte();
int serial_b = reverseByte_32(inData.readInt());
short type_b = reverseByte_16(inData.readShort());
short sub_type_b = reverseByte_16(inData.readShort());
int len_b = reverseByte_32(inData.readInt());
int result_b = reverseByte_32(inData.readInt());
....
.....
=======================================================
//java的16BIT的数据格式转换为C语言的数据格式。
public static short reverseByte_16(int param) throws Exception {
String order = SysConfig.loadSysConfig("System.byteOrder");
if (order.equals("1")) { //高字节序传送数据,无须任何转换
return (short) param;
}
int r1 = param & 0x000000ff;
r1 <<= 8;
param >>= 8;
int r2 = 0x0000ffff & param;
short result = (short) (r1 | r2);
return result;
}
//java的32BIT的数据格式转换为C语言的数据格式。
public static int reverseByte_32(int param) throws Exception {
String order = SysConfig.loadSysConfig("System.byteOrder");
if (order.equals("1")) { //高字节序传送数据,无须任何转换
return param;
}
int r4 = param & 0x000000ff;
r4 <<= 24;
int r3 = param & 0x0000ff00;
r3 <<= 8;
param >>= 8;
int r2 = param & 0x0000ff00;
param >>= 16;
int r1 = 0x000000ff & param;
int result = (int) (r4 | r3 | r2 | r1);
return result;
}
/*
I/O流的层次
1.字节流 2.字符流 3.对象流 4.其它
详细介绍:
---------------------------------------
1.字节流:
从InputStream和OutputStream派生出来的一系列类。这类流以字节(byte)为基本处理单位。
◇ InputStream、OutputStream
◇ FileInputStream、FileOutputStream
◇ PipedInputStream、PipedOutputStream
◇ ByteArrayInputStream、ByteArrayOutputStream
◇ FilterInputStream、FilterOutputStream
◇ DataInputStream、DataOutputStream
◇ BufferedInputStream、BufferedOutputStream
---------------------------------------
InputStream 和OutputStream
InputStream 和OutputStream 是其他字节流的父类,他是一个抽象类,各有一个抽象方法:
abstract int read()和abstract write(int b);
InputStream:
◇ 从流中读取数据:
int read( ); //读取一个字节,返回值为所读的字节
int read( byte b[ ] ); //读取多个字节,放置到字节数组b中,通常
//读取的字节数量为b的长度,返回值为实际
//读取的字节的数量
int read( byte b[ ], int off, int len ); //读取len个字节,放置
//到以下标off开始字节
//数组b中,返回值为实
//际读取的字节的数量
int available( ); //返回值为流中尚未读取的字节的数量
long skip( long n ); //读指针跳过n个字节不读,返回值为实际
//跳过的字节数量
◇ 关闭流:
close( ); //流操作完毕后必须关闭
◇ 使用输入流中的标记:
void mark( int readlimit ); //记录当前读指针所在位置,readlimit
//表示读指针读出readlimit个字节后
//所标记的指针位置才失效
void reset( ); //把读指针重新指向用mark方法所记录的位置
boolean markSupported( ); //当前的流是否支持读指针的记录功能
OutputStream:
◇ 输出数据:
void write( int b ); //往流中写一个字节b
void write( byte b[ ] ); //往流中写一个字节数组b
void write( byte b[ ], int off, int len ); //把字节数组b中从
//下标off开始,长度为len的字节写入流中
◇ flush( ) //刷空输出流,并输出所有被缓存的字节
//由于某些流支持缓存功能,该方法将把
//缓存中所有内容强制输出到流中。
◇ 关闭流:
close( ); //流操作完毕后必须关闭
---------------------------------------
FileInputStream和FileOutputStream
类FileInputStream和FileOutputStream用来进行文件I/O处理,由它们所提供的方法可以打
开本地主机上的文件,并进行顺序的读/写。例如,下列的语句段是顺序读取文件名为text的
文件里的内容,并显示在控制台上面,直到文件结束为止。
*/
//程序 1:
import java.io.*;
import javax.swing.*;
public class FileInputStreamDemo
{
public static void main(String[] args)
{
try
{
JFileChooser chooser = new JFileChooser();
int status = chooser.showOpenDialog(null);
if (status == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
FileInputStream fis = new FileInputStream (file);
System.out.println ("The content of text is:");
int b;
while ((b = fis.read())!=-1)//顺序读取文件text里的内容并赋值
//给整型变量b,直到文件结束为止。
{
System.out.print((char)b);
}
fis.close();
}
}
catch(FileNotFoundException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
/*
---------------------------------------
RandomAccessFile
对于InputStream 和OutputStream 来说,它们的实例都是顺序访问流,也就是说,只能对文件进行
顺序地读/写。随机访问文件则允许对文件内容进行随机读/写。在java中,类RandomAccessFile 提
供了随机访问文件的方法。
类RandomAccessFile的声明为:
public class RandomAccessFile extends Object implements DataInput, DataOutput
接口DataInput 中定义的方法主要包括从流中读取基本类型的数据、读取一行数据、或者读取指定
长度的字节数。如:readBoolean( )、readInt( )、readLine( )、readFully( ) 等。
接口DataOutput 中定义的方法主要是向流中写入基本类型的数据、或者写入一定长度的字节数组。
如:writeChar( )、writeDouble( )、write( ) 等。 下面详细介绍RandomAccessFile类中的方法。
◇ 构造方法:
RandomAccessFile(String name,String mode); //name是文件名,mode
//是打开方式,例如"r"表示只读,"rw"表示可读写,"
RandomAccessFile(File file,String mode); //file是文件对象
◇ 文件指针的操作
long getFilePointer( ); //用于得到当前的文件指针
void seek( long pos ); //用于移动文件指针到指定的位置
int skipBytes( int n ); //使文件指针向前移动指定的n个字节
------------------------------------------------------------
过滤流FilterInputStream和FilterOutputStream
过滤流在读/写数据的同时可以对数据进行处理,它提供了同步机制,使得某一时刻只有一个线程可以
访问一个I/O流,以防止多个线程同时对一个I/O流进行操作所带来的意想不到的结果。
类FilterInputStream和FilterOutputStream分别作为所有过滤输入流和输出流的父类。
为了使用一个过滤流,必须首先把过滤流连接到某个输入/出流上,通常通过在构造方法的参数中指定所
要连接的输入/出流来实现。例如:
FilterInputStream( InputStream in );
FilterOutputStream( OutputStream out );
几种常见的过滤流
◇ BufferedInputStream和BufferedOutputStream
缓冲流,用于提高输入/输出处理的效率。
◇ DataInputStream 和 DataOutputStream
不仅能读/写数据流,而且能读/写各种的java语言的基本类型,如:boolean,int,float等。
◇ LineNumberInputStream
除了提供对输入处理的支持外,LineNumberInputStream可以记录当前的行号。
◇ PushbackInputStream
提供了一个方法可以把刚读过的字节退回到输入流中,以便重新再读一遍。
◇ PrintStream
打印流的作用是把Java语言的内构类型以其字符表示形式送到相应的输出流。
*/
/*
进行I/O操作时可能会产生I/O例外,属于非运行时例外,应该在程序中处理。
如:FileNotFoundException, EOFException, IOException
*/
/*
-----------------------------------
2.字符流:
从Reader和Writer派生出的一系列类,这类流以16位的Unicode码表示的字符为基本处理单位。
◇ Reader、Writer
◇ InputStreamReader、OutputStreamWriter
◇ FileReader、FileWriter
◇ CharArrayReader、CharArrayWriter
◇ PipedReader、PipedWriter
◇ FilterReader、FilterWriter
◇ BufferedReader、BufferedWriter
◇ StringReader、StringWriter
------------------------------------
3.对象流
◇ ObjectInputStream、ObjectOutputStream
4.其它
◇ 文件处理:
File、RandomAccessFile;
◇ 接口
DataInput、DataOutput、ObjectInput、ObjectOutput;