📄 program5.txt
字号:
raf2.close();
}
catch(Exception e)
{
System.err.println(e);
}
}
}
}
——————————————————————————————
——————————————————————————————
4.5.3抽象流类
1.InputStream类
InputStream类的定义如下:
public abstract class InputStream extends Object
{
public InputStream();
public abstract int read() throws IOException;
public int read(byte b[]) throws IOException ;
public int read(byte b[], int off, int len) throws IOException ;
public long skip(long n) throws IOException ;
public int available() throws IOException ;
public void close() throws IOException ;
public synchronized void mark(int readlimit) ;
public synchronized void reset() throws IOException ;
public boolean markSupported() ;
}
——————————————————————————
2.OutputStream类
与输入流类相对应的另一个抽象类是OutputStream类,它定义了与输出操作有关的方法,其定义如下:
public abstract class OutputStream extends Object
{
public OutputStream();
public abstract void write(int b) throws IOException;
public void write(byte b[]) throws IOException;
public void write(byte b[], int off, int len) throws IOException;
public void flush() throws IOException;
public void close() throws IOException;
}
————————————————————————————
————————————————————————————
4.5.4文件输入输出流类
1.FileInputStream类
FileInputStream类提供从文件中读入数据的方法,其定义为:
public class FileInputStream extends InputStream
{
public FileInputStream(String name) throws FileNotFoundException;
public FileInputStream(File file) throws FileNotFoundException;
public FileInputStream(FileDescriptor fdObj);
public int read() throws IOException;
public int read(byte b[]) throws IOException;
public int read(byte b[], int off, int len) throws IOException;
public long skip(long n) throws IOException;
// 使文件的读入指针向前或向后移动n个字节,其参数值相对于当前文件指针
public int available() throws IOException;
//返回文件输入流中可供读入的字节数,实际上就是文件的长度。
public void close() throws IOException;
public final FileDescriptor getFD() throws IOException;
protected void finalize() throws IOException;
}
————————————————————————————————
例子:下面的程序说明FileInputStream类的使用方法。程序实现类似MS-DOS操作系统中type命令,用于显示文件内容。
//Program Name: FileType.java
import java.io.*;
class FileType
{
public static void main(String[] args)
{
if (args.length != 1)
{
System.err.println("Usage: java FileType <input_file>");
System.exit(-1);
}
File file = new File(args[0]);
try
{
FileInputStream in = new FileInputStream(file);
int c;
int i = 0;
while ((c = in.read()) > -1)
{
if((char)c == '\n') i++; //统计行数
System.out.print((char)c);
}
in.close();
System.out.flush();
System.out.println("\n\n\n----------------");
System.out.println("File " + args[0] + " Lines: " + i);
}
catch (FileNotFoundException e)
{
System.err.println(file + " is not found");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
程序在生成FileInputStream对象时,用File对象给出文件名,虽然比用字符串给出文件名稍麻烦点,但这种方法使应用程序独立于平台,是一种良好的设计习惯。
程序在显示文件内容时使用了System.out.print()方法,而不是以前程序中常用的方法System.out.println()。
两者的区别是:println()执行时,立即把字符显示在屏幕上,并且每次调用println()方法,它总是在新的一行输出数据。print()执行时,并不立即显示数据,而把数据缓存在输出流中,直到遇到新行字符'\n'或执行了flush()方法后,才使字符显示在屏幕上。如果程序中使用println(),那么在屏幕上的显示情况就是一行一个字符,不是原来文件的行格式。
————————————————————————————————
2.FileOutputStream类
FileOutputStream类提供把数据写出到文件中的方法,其定义为:
public class FileOutputStream extends OutputStream
{
public FileOutputStream(String name) throws IOException;
public FileOutputStream(String name, boolean append) throws IOException;
public FileOutputStream(File file) throws IOException;
public FileOutputStream(FileDescriptor fdObj);
public void write(int b) throws IOException;
public void write(byte b[]) throws IOException;
public void write(byte b[], int off, int len) throws IOException;
public void close() throws IOException;
public final FileDescriptor getFD() throws IOException;
}
————————————————————————————————
————————————————————————————————
4.5.6其它输入输出流类
1.SequenceInputStream类
SequenceInputStream类可以将两个或几个输入流不露痕迹地接合在一起,生成一个长长的接合流,在读入数据时,它忽略前面几个输入流的结束符EOF,直到最后一个流的结束符EOF时,才完成流的输入,其定义如下:
public class SequenceInputStream extends InputStream
{
public SequenceInputStream(Enumeration e);
//能够接合任意多个输入流
public SequenceInputStream(InputStream s1, InputStream s2);
只能接合两个输入流。
public int read() throws IOException;
public int read(byte buf[], int pos, int len) throws IOException;
public int available() throws IOException;
public void close() throws IOException;
}
————————————————————————————————
例子:下面程序把两个文件输入流接合成一个SequenceInputStream流,再写到一个文件中。
//Program Name: SeqTest.java
import java.io.*;
class SeqTest
{
public static void main(String[] args)
{
FileInputStream fis1 = null;
FileInputStream fis2 = null;
FileOutputStream fos = null;
SequenceInputStream sis =null;
int ch;
if(args.length != 3)
{
System.out.println("Usage: java SeqTest <file1><file2><outfile>");
System.exit(0);
}
try
{
fis1 = new FileInputStream(new File(args[0]));
fis2 = new FileInputStream(new File(args[1]));
fos = new FileOutputStream(new File(args[2]));
}
catch (IOException e)
{
System.out.println("Cannot find file!");
System.exit(-1);
}
sis = new SequenceInputStream(fis1, fis2);
try
{
while((ch = sis.read()) > -1) fos.write(ch);
fis1.close();
fis2.close();
sis.close();
}
catch (IOException e)
{
System.out.println(e);
System.exit(-1);
}
}
}
————————————————————————————————
2.管道输入输出流类
Java使用了管道技术,用PipedInputStream类和PipedOutputStream类实现管道操作,这两个类必须同时使用,它们的定义分别为:
public class PipedInputStream extends InputStream
{
public PipedInputStream(PipedOutputStream src) throws IOException;
public PipedInputStream();
public synchronized int read() throws IOException;
public synchronized int read(byte b[], int off, int len) throws IOException;
public synchronized int available() throws IOException;
public void close() throws IOException;
public void connect(PipedOutputStream src) throws IOException;
}
public class PipedOutputStream extends OutputStream
{
public PipedOutputStream(PipedInputStream snk) throws IOException;
public PipedOutputStream();
public void write(int b) throws IOException;
public void write(byte b[], int off, int len) throws IOException;
public synchronized void flush() throws IOException;
public void close() throws IOException;
public void connect(PipedInputStream snk) throws IOException;
}
——————————————————————————————
例子:下面的程序使用两个线程:一个线程模拟数据采集,用Random类随机生成数值,另一个线程模拟数据处理,使用数据,计算其平均值。
//Program Name: PipeTest.java
import java.io.*;
import java.util.Random;
//数据处理
class RunningAverage extends Thread
{
private DataInputStream in;
double total = 0;
long count = 0;
RunningAverage(InputStream i)
{
in = new DataInputStream(i);
}
public void run()
{
while (true)
{
try
{
double num = in.readDouble();
total += num;
count++;
System.out.println(count + ": " + num + "\t avg = "+ total/count);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
//数据采集
class NumberGenerator extends Thread
{
private DataOutputStream out;
private Random gen = new Random();
private final long RANGE = 1000;
NumberGenerator(OutputStream o)
{
out = new DataOutputStream(o);
}
public void run()
{
while (true)
{
try
{
double num = gen.nextDouble() * RANGE;
out.writeDouble(num);
out.flush();
sleep(500);
}
catch (IOException e)
{
e.printStackTrace();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
//主类
class PipeTest
{
public static void main(String[] args)
{
try
{
PipedOutputStream producer = new PipedOutputStream();
PipedInputStream consumer = new PipedInputStream(producer);
RunningAverage avg = new RunningAverage(consumer);
NumberGenerator gen = new NumberGenerator(producer);
gen.start();
avg.start();
try
{
Thread.sleep(5000);
}
catch (InterruptedException e) {}
gen.stop();
avg.stop();
producer.close();
consumer.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
程序用PipedOutputStream()生成管道输出流producer,用PipedInputStream(producer)生成管道输入流consumer,同时实现了两个管道的连接。
程序中的两个类RunningAverage和NumberGenerator,都是Thread类的子类,是两个不同的线程。前一个类生成数据,用管道输出这些数据,后一个类从输入管道接收数据,然后计算平均值。main()在启动了这两个线程工作后,睡眠5秒钟,然后stop()两个线程,再close()管道输入输出流而结束程序。
__________________________________________
__________________________________________
5.3网络编程
5.3.1 InetAddress
例1:TestAdd.java
import java.net.*;
import java.io.*;
public class TestAdd
{
public static void main(String args[])
{
try
{
if(args.length==1)
{
InetAddress ipa=InetAddress.getByName(args[0]);
System.out.println("Host name:"+ipa.getHostName());
//获取主机名
System.out.println("Host Address:"+ipa.getHostAddress());
//获取IP地址
System.out.println("Host IP Address:"+ipa.toString());
//获取主机名/IP地址
System.out.println("Local Host:"+InetAddress.getLocalHost());
String s=ipa.isMulticastAddress()? " Yes" : " No";
System.out.println("is MuticastAddress:"+s);
byte[] addr=ipa.getAddress();
System.out.println("Use getAddress():"+addr[0]+"."+addr[1]+"."+addr[2]+"."+addr[3]);
System.out.println("Converted getAddress():"+(addr[0]&0XFF)+"."+(addr[1]&0XFF)+"."+(addr[2]&0XFF)+"."+(addr[3]&0XFF));
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -