皮皮学,免费搜题
登录
搜题
【简答题】
上机实践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)小题
拍照语音搜题,微信中搜索"皮皮学"使用
参考答案:
参考解析:
知识点:
.
..
皮皮学刷刷变学霸
举一反三
【单选题】下列关于乌龙茶饮用的讲究,说法正确的是()
A.
乌龙茶冲泡时间在10分钟左右
B.
乌龙茶最好采用内外兼热的方法将茶迅速泡开
C.
乌龙茶泡好之后,可以先倒满一杯再去倒另一杯
D.
泡茶至最后,茶壶内剩余的茶叶和茶不被需要
【单选题】班轮运输的“一负责”指的是( )?
A.
托运人负责装和卸
B.
承运人 负责装和卸
C.
发货人负责装和卸
D.
收货人负责装和卸
【判断题】长时间运动时,蛋白质的氧化是运动肌主要的能源来源。
A.
正确
B.
错误
【单选题】对会计科目的具体内容进行分类核算的项目,称为会计要素。 ( )
A.
正确
B.
错误
【简答题】目的:练习会计要素的分类。 资料:某企业与会计要素有关的项目见表2-1。 表2-1   某企业与会计要素有关的项目 序号 项目 会计科目 会计要素 1 房屋及建筑物 2 机器设备 3 运输汽车 4 库存生产用钢材 5 完工验收入库的产成品 6 存放在银行的款项 7 由出纳员保管的现金 8 应收某厂购买企业产品的货款 9 从银行借入的不超过1年的借款 10 应付购买某厂材料的货款 11 欠缴的税金 ...
【单选题】炒青绿茶根据其外形不同可分为长炒青、圆炒青、扁炒青、卷曲炒青等。以下有关这些分类的说法正确的是()。1江西婺源的“婺绿炒青”,安徽屯溪、休宁的“屯绿炒青”均为长炒青绿茶2圆炒青绿茶即珠茶,是浙江特产。特点是外形浑圆紧结,香高味浓耐冲泡3扁炒青绿茶即龙井、大方等,产于江苏省4卷曲炒青绿茶如碧螺春等,产于浙江、安徽等省
A.
①②
B.
①③
C.
②④
D.
③④
【简答题】班轮运输的“一负责”指的是______ 。
【单选题】Where will the woman go?
A.
The bus station.
B.
The railway station.
C.
Germany.
【简答题】对会计科目的具体内容进行分类核算的项目,称为会计要素。( )
【简答题】The following notice is posted in the bus station. Time Table: ●Buses leave the Railway Station, New York City, from7:00 a.m. and every half-hour thereafter, until 11:30 p.m. (7 days a week) ●Buses le...
相关题目: