//建立ObjectOutputStream对象,并且与文件对象object_file 连接。
ObjectInputStream ois=new ObjectInputStream( new FileInputStream(object_file )); Hashtable ht1=(Hashtable)ois.readObject(); //从输入源读入对象 System.out.println(ht1); //标准输出ht1 }
catch(Exception e){ e.printStackTrace(); } } }
3、利用StringBuffer类编写从键盘读入字符串、整数、实型数,并在屏幕上输出。 源程序:
import java.io.*;
public class StringBufferToString {
public static void main(String[] args) throws IOException {
char ch;
StringBuffer strb = new StringBuffer(); //创建一个空的StringBuffer类 System.out.println(\Enter the char ,int number or float number:\ while((ch = (char)System.in.read() )! = '\\n') {
strb.append(ch); //把字符连接到当前串尾 }
String str = strb.toString( ); //把StringBuffer类转换为String类 System.out.println(\ System.out.println(str); } }
4、什么叫流?简述流的分类?
“流”可以被理解为一条“管道”。这条“管道”有两个端口:一端与数据源(当输入数据时)或数据宿(当输出数据时)相连,另一端与程序相连。在与数据源或是数据宿相连的端口,“管道”在读/写数据时能够应付数据源和数据宿的多样性,消化掉因数据源和数据宿的多样性带来的数据读/写的复杂性;而在与程序相连的端口,“管道”提供了输入/输出的统一界面。
根据流中的数据传输的方向,流可分为输入流和输出流;根据“管道”里流动的数据的类型,流可分为字符流和字节流;根据流的建立方式和工作原理,流可分为节点流与过滤流。
5、编写一个测试文件一般属性(如显示文件的路径、绝对路径、显示文件是否可写、显示文件是否可读、显示文件的大小等属性)的程序。 import java.io.*;
public class FileInfo
{
public static void main(String[] args) {
System.out.println(\ char c;
StringBuffer buf = new StringBuffer( ) ; try {
while( (c = (char)System.in.read() ) != '\\n') buf.append(c);
//将输入的字符加到buf中,直到遇见回车符 } catch(java.io.IOException e) {
System.out.println(\ }
File file = new File(buf.toString( ).trim( ) ); //创建File类的file对象
if ( file.exists( ) )
//如果文件在当前目录存在 {
System.out.println(\ //显示文件的文件名
System.out.println(\ //显示文件的路径
System.out.println(\ //显示文件的绝对路径
System.out.println(\ //显示文件是否可写
System.out.println(\ // 显示文件是否可读
System.out.println(\ //显示文件的大小 }
else //如果文件不在当前目录
System.out.println(\ //显示文件没有找到的提示消息 } }
6、利用RandomAccessFile类编写应用程序,要求输入10组数据到文件中,每组数据为1个整形数和一个双精度数,然后随机修改文件的某组数,并显示修改的结果。 import java.io.*;
public class testRandom2{
public static void main(String args[]){ try{
RandomAccessFile rf=new RandomAccessFile(\for(int i = 0; i < 10; i++){ rf.writeInt(i);
rf.writeDouble(i*1.414); }
rf.close();
rf = new RandomAccessFile(\rf.seek(5*(4+8)); rf.writeInt(47);
rf.writeDouble(47.0001); rf.close();
rf = new RandomAccessFile(\for(int i = 0; i < 10; i++){ ;
System.out.println(\}
rf.close();
}catch(IOException e){
System.out.println(e.toString()); } } } 7、将如下三组不同类型的数据利用DataInputStream和DataOutputStream写入文件,然后从文件中读出。 三组数据如下
{ 19.99, 9.99, 15.99,3.99, 4.99 }; { 12, 8, 13, 29, 50 };
{ \
解:import Java.io.*; public class DataIOTest {
public static void main(String[] args) throws IOException {
//创建数据输出流
DataOutputStream out = new DataOutputStream(new FileOutputStream(\ //初始化输出数据
double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 }; int[] units = { 12, 8, 13, 29, 50 }; String[] descs = {
\
\
\ \
\};
//数据输出
for (int i = 0; i < prices.length; i ++) {
out.writeDouble(prices[i]); out.writeChar('\\t'); out.writeInt(units[i]); out.writeChar('\\t');
out.writeChars(descs[i]); out.writeChar('\\n'); }
//关闭数据输出流 out.close(); //创建数据输入流
DataInputStream in = new DataInputStream( new FileInputStream(\
double price; int unit; String desc;
double total = 0.0; try {
//利用数据输入流读文件内容 while (true) {
price = in.readDouble();
in.readChar(); // throws out the tab unit = in.readInt();
in.readChar(); // throws out the tab desc = in.readLine();
System.out.println(\ unit + \ desc + \ total = total + unit * price; } }
//捕获异常 catch (EOFException e)
{
e.printStackTrace(); }
System.out.println(\ //关闭数据输入流 in.close(); } }
8、利用BufferedReader和BufferedWriter在文件中实现输入输出字符串。 import java.io.*;
public class ko24_6 {
public static void main(String args[]) throws IOException {
String line;
String str=\
BufferedWriter kow=new BufferedWriter(new FileWriter(\ kow.write(str,0,str.length()); kow.flush(); BufferedReader kor=new BufferedReader(new FileReader(\ line=kor.readLine(); System.out.println(line); } }
9、什么是过滤流,并举例
解:过滤流在读/写数据的同时可以对数据进行处理,它提供了同步机制,使得某一时刻只有一个线程可以访问一个I/O流,以防止多个线程同时对一个I/O流进行操作所带来的意想不到的结果。类FilterInputStream和FilterOutputStream分别作为所有过滤输入流和输出流的父类。
为了使用一个过滤流,必须首先把过滤流连接到某个输入/出流上,通常通过在构造方法的参数中指定所要连接的输入/出流来实现。例如:
FilterInputStream( InputStream in ); FilterOutputStream( OutputStream out ); 下面的类均是过滤流的例子
BufferedInputStream和BufferedOutputStream 缓冲流,用于提高输入/输出处理的效率。 DataInputStream 和 DataOutputStream 不仅能读/写数据流,而且能读/写各种的java语言的基本类型,如:boolean,int,float等。
LineNumberInputStream
除了提供对输入处理的支持外,LineNumberInputStream可以记录当前的行号。
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库Java程序设计各章习题及其答案(8)在线全文阅读。
相关推荐: