皮皮学,免费搜题
登录
logo - 刷刷题
搜题
【简答题】
上机实践9 输入输出流 实验1 字符输入/输出流:FileReader / FileWriter 教材第10章例6 import java.io.*; public class Example10_6 { public static void main(String args[]) { File sourceFile = new File("a.txt"); //读取的文件 File targetFile = new File("b.txt"); //写入的文件 char c[] =new char[19]; //char型数组 try{ Writer out = new FileWriter(targetFile,true); //指向目的地的输出流 Reader in = new FileReader(sourceFile); //指向源的输入流 int n = -1; while((n=in.read(c))!=-1) { out.write(c,0,n); } out.flush(); out.close(); } catch(IOException e) { System.out.println("Error "+e); } } 请运行以上代码,并将运行结果截图写入实验报告。 实验2 字节输入/输出流:FileInputStream/FileOutputStream 教材第10章例4 import java.io.*; public class Example10_4 { public static void main(String args[]) { int n=-1; byte [] a=new byte[100]; try{ File f=new File("Example10_4.java"); InputStream in = new FileInputStream(f); while((n=in.read(a,0,100))!=-1) { String s=new String (a,0,n); System.out.print(s); } in.close(); } catch(IOException e) { System.out.println("File read Error"+e); } } } 教材第10章例5 import java.io.*; public class Example10_5 { public static void main(String args[]) { byte [] a = "新年快乐".getBytes(); byte [] b = "Happy New Year".getBytes(); File file = new File("a.txt"); //输出的目的地 try{ OutputStream out=new FileOutputStream(file); //指向目的地的输出流 System.out.println(file.getName()+"的大小:"+file.length()+"字节");//a.txt的大小:0字节 out.write(a); //向目的地写数据 out.close(); out=new FileOutputStream(file,true); //准备向文件尾加内容 System.out.println(file.getName()+"的大小:"+file.length()+"字节");///a.txt的大小:8字节 out.write(b,0,b.length); System.out.println(file.getName()+"的大小:"+file.length()+"字节");///a.txt的大小:8字节 out.close(); } catch(IOException e) { System.out.println("Error "+e); } } } 请运行以上代码,并将运行结果截图写入实验报告。 实验3 数据输入流/输出流:DataInputStream/DataOutputStream import java.io.*; public class TestDataStream { public static void main(String[] args) throws IOException { // Create an output stream for file temp.dat DataOutputStream output = new DataOutputStream(new FileOutputStream("temp.dat")); // Write student test scores to the file output.writeUTF("John"); output.writeDouble(85.5); output.writeUTF("Jim"); output.writeDouble(185.5); output.writeUTF("George"); output.writeDouble(105.25); // Close output stream output.close(); // Create an input stream for file temp.dat DataInputStream input = new DataInputStream(new FileInputStream("temp.dat")); // Read student test scores from the file System.out.println(input.readUTF() + " " + input.readDouble()); System.out.println(input.readUTF() + " " + input.readDouble()); System.out.println(input.readUTF() + " " + input.readDouble()); input.close(); } } 请运行以上代码,并将运行结果截图写入实验报告。 实验4 随机输入/输出流:RandomAccessFile 教材第10章例8 import java.io.*; public class Example10_8 { public static void main(String args[]) { RandomAccessFile inAndOut=null; int data[]={1,2,3,4,5,6,7,8,9,10}; try{ inAndOut=new RandomAccessFile("tom.dat","rw"); for(int i=0;i =0;i--) { //一个int型数据占4个字节,inAndOut从 inAndOut.seek(i*4); //文件的第36个字节读取最后面的一个整数, System.out.printf("\t%d",inAndOut.readInt()); //每隔4个字节往前读取一个整数 } inAndOut.close(); } catch(IOException e){} } } 请运行以上代码,并将运行结果截图写入实验报告。 实验5 对象输入/输出流 : ObjectInputStream/ObjectOutputStream 教材第10章例13 TV.java import java.io.*; public class TV implements Serializable{ String name; int price; public void setName(String s) { name=s; } public void setPrice(int n) { price=n; } public String getName() { return name; } public int getPrice() { return price; } } Example10_13.java import java.io.*; public class Example10_13 { public static void main(String args[]) { TV changhong = new TV(); changhong.setName("长虹电视"); changhong.setPrice(5678); File file=new File("television.txt"); try{ FileOutputStream fileOut=new FileOutputStream(file); ObjectOutputStream objectOut=new ObjectOutputStream(fileOut); objectOut.writeObject(changhong); objectOut.close(); FileInputStream fileIn=new FileInputStream(file); ObjectInputStream objectIn=new ObjectInputStream(fileIn); TV xinfei=(TV)objectIn.readObject(); objectIn.close(); xinfei.setName("新飞电视"); xinfei.setPrice(6666); System.out.println("changhong的名字:"+changhong.getName()); System.out.println("changhong的价格:"+changhong.getPrice()); System.out.println("xinfei的名字:"+xinfei.getName()); System.out.println("xinfei的价格:"+xinfei.getPrice()); } catch(ClassNotFoundException event) { System.out.println("不能读出对象"); } catch(IOException event) { System.out.println(event); } } } 请运行以上代码,并将运行结果截图写入实验报告。 练习1 请用文字说明实验1--实验5共五种输入流/输出流对象的区别点。 练习2 请在实验报告中阐述分析过程,并上机验证分析结果和运行结果是否一致: 教材320页,第3题第(1)小题 教材320页,第3题第(2)小题 练习3 请在实验报告中描述程序思路,并展示源代码和运行结果: 教材321页,第4题第(1)小题, 教材321页,第4题第(2)小题
手机使用
分享
复制链接
新浪微博
分享QQ
微信扫一扫
微信内点击右上角“…”即可分享
反馈
参考答案:
举一反三
【简答题】前照灯有哪些元件构成?前照灯特性由哪些?
【单选题】绘图铅笔铅芯一般露出约( )
A.
5~6mm
B.
6~7mm
C.
6~8mm
D.
6~9mm
【单选题】设有Teachers表,该表的定义如下:CREATE TABLE Teachers( Tno CHAR(8) PRIMARY KEY, Tname VARCHAR(10) NOT NULL, Age TINYINT CHECK(Age BETWEEN 25 AND 65) )插入语句中,不能正确执行的是( )。
A.
INSERT INTO Teachers VALUES('T100','张宏',NULL)
B.
INSERT INTO Teachers(Tno,Tname,Age) VALUES('T100','张宏',30)
C.
INSERT INTO Teachers(Tno,Tname) VALUES('T100','张宏')
D.
INSERT INTO Teachers VALUES('T100','张宏')
【单选题】设有Teachers表,该表的定义如下: CREATE TABLE Teachers( Tno CHAR(8) PRIMARY KEY, Tname VARCHAR(10) NOT NULL, Age TINYINT CHECK(Age BETWEEN 25 AND 65) ) 下列插入语句中,不能正确执行的是( )。
A.
INSERT INTO Teachers VALUES('T100','张鸿',NULL)
B.
INSERT INTO Teachers(Tno,Tname,Age) VALUES('T100','张鸿',30)
C.
INSERT INTO Teachers(Tno,Tname) VALUES('T100','张鸿')
D.
INSERT INTO TeachersVALUES('T100','张鸿')
【单选题】雷诺数是判别下列哪种流态的重要无量纲数
A.
急流和缓流
B.
均匀流和非均匀流
C.
层流和紊流
D.
恒定流和非恒定流
【单选题】若软弱下卧层承载力不能满足要求,下列( )措施是无效的。
A.
增大基础面积
B.
减小基础埋深
C.
提高混凝土强度等级
D.
采用补偿式基础
【单选题】在制备电子显微镜观察组织样本时常用的固定试剂是
A.
甲醇
B.
甲醛
C.
乙醇
D.
锇酸
E.
丙烯酰胺
【单选题】若软弱下卧层承载力不能满足要求,下列哪个措施是无效的?
A.
增大基础底面积
B.
减小基础埋深
C.
提高混凝土强度等级
D.
采用补偿式基础
【单选题】若软弱下卧层承载力不能满足要求,下列( )措施是无效的。
A.
增大基础面积
B.
减小基础埋深
C.
提高混凝土强度
D.
采用补偿式基础
【单选题】绘图铅笔铅芯一般露出约
A.
1~2mm
B.
3~5mm
C.
6~8mm
D.
10~13mm
相关题目:
参考解析:
知识点:
题目纠错 0
发布
创建自己的小题库 - 刷刷题