




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
山东师范大学人口资源与环境学院实 验 报 告(五)实验课程Java语言程序设计实验名称常用系统类的使用以Java IO为例学 号201214010422姓 名吴琳班 级2012地信实验日期2014.5.5指导教师董春玲成 绩一、实验目的与要求1.掌握不同类型的输入输出流类,标准数据流、文件流、数据输入输出流、对象流等。2.掌握RandomAccessFil类的使用。3.掌握ZipInputStream流的使用。4.了解数学函数类、日期类、向量类等常用系统类的使用方法。2、 实验内容与步骤1. 使用标准数据流的应用程序标准数据流指在字符方式下(如DOS提示符)程序与系统进行输入输出的方式,键盘和显示器屏幕是标准输入输出设备,数据输入的起点为键盘,数据输出的终点是屏幕,输出的数据可以在屏幕上显示出来。(1)程序功能:将键盘上输入的字符在屏幕上显示出来。(2)编写LX5_3.java程序文件,源代码如下。class LX5_3 public static void main(String args) throws java.io.IOException byte buffer=new byte10; System.out.println(从键盘输入不超过10个字符,按回车键结束输入:); int count =System.in.read(buffer);/读取输入的字符并存放在缓冲区buffer中System.out.println(保存在缓冲区buffer中元素的个数为:+count); System.out.println(buffer中各元素的值为:); for (int i=0;icount;i+) System.out.print( + bufferi);/在屏幕上显示buffer元素的值 System.out.println(); System.out.println(输出buffer字符元素:); System.out.write(buffer, 0, buffer.length); 2. 使用文件输入输出流的应用程序(1)程序功能:将保存在本地机当前文件夹中的LX5_1.HTML文本文件的内容在屏幕上显示出来,然后将其另存为LX5_1.txt文件。(2)编写LX5_4.java程序文件,源代码如下。import java.io.*; public class LX5_4 public static void main(String args) throws IOException FileReader in=new FileReader(LX5_1.HTML);/建立文件输入流BufferedReader bin=new BufferedReader(in);/建立缓冲输入流FileWriter out=new FileWriter( LX5_1.txt,true);/建立文件输出流String str; while (str=bin.readLine()!=null) /将缓冲区内容通过循环方式逐行赋值给字符串str System.out.println(str);/在屏幕上显示字符串str out.write(str+n);/将字符串str通过输出流写入LX5_1.txt中 in.close(); out.close(); 3. 使用随机文件类的应用程序使用文件输入类FileReader只能将文件内容全部读入。如果要选择读入文件的内容,可使用随机文件类RandomAccessFile。(1)程序功能:建立数据流,通过指针有选择的读入文件内容。(2)编写LX5_5.java程序文件,源代码如下。import java.io.*; class LX5_5 public static void main(String args) String str=First linen,Second linen,Last linen; try RandomAccessFile rf=new RandomAccessFile(LX5_5.txt, rw); System.out.println(n文件指针位置为:+rf.getFilePointer(); System.out.println(文件的长度为:+rf.length(); rf.seek(rf.length(); System.out.println(文件指针现在的位置为:+rf.getFilePointer(); for (int i=0; i=0)m=m-1;random.seek(m);int c=random.readByte();if(c=0)System.out.print(char)c);elsem=m-1;random.seek(m);byte cc=new byte2;random.readFully(cc);System.out.print(new String(cc);catch(IOException e)2.使用数学函数类源程序如下。importjava.util.*;classKY1_2publicstaticvoidmain(Stringargs)Randomr1=newRandom(1234567890L);Randomr2=newRandom(1234567890L);booleanb=r1.nextBoolean();inti1=r1.nextInt(100);inti2=r2.nextInt(100);doublei3=r1.nextDouble();doublei4=r2.nextDouble();doubled1=Math.sin(Math.toRadians(30.0);doubled2=Math.log(Math.E);doubled3=Math.pow(2.0,3.0);intr=Math.round(33.6F);System.out.println(b的随机数不为0时+b);System.out.println(i1的随机数为+i1);System.out.println(i2的随机数为+i2);System.out.println(i3的随机数为+i3);System.out.println(i4的随机数为+i4);System.out.println(30弧度的正弦值:Math.sin(Math.toRadians(30.0)+d1);System.out.println(E的对数值:Math.log(Math.E)+d2);System.out.println(2的3次方:Math.pow(2.0,3.0)+d3);System.out.println(33.6F四舍五入:Math.round(33.6F)+r);3.日期函数源代码如下。import java.util.*;import java.text.*;public class KY5_10public static void main (String args)Date today = new Date();SimpleDateFormat sdf;sdf= new SimpleDateFormat(yyyy 年MM 月dd 日hh 时mm 分ss 秒 a EEEEE);System.out.println(当前日期和时间: +sdf.format(today);long hms=System.currentTimeMillis(); /当前时间的毫秒数System.out.println(当前时间的毫秒数=+hms);Date tomorrow = new Date(hms+24*60*60*1000);System.out.println(明天是+sdf.format(tomorrow);Calendar now = Calendar.getInstance();int year =now.get(Calendar.YEAR); int month=now.get(Calendar.MONTH)+1;int day = now.get(Calendar.DATE); System.out.print(今天是+year+年+month+月+day+日);int week = now.get(Calendar.DAY_OF_WEEK); switch (week)case 1: System.out.println( 星期日);break;case 2: System.out.println( 星期一);break;case 3: System.out.println( 星期二);break;case 4: System.out.println( 星期三);break;case 5: System.out.println( 星期四);break;case 6: System.out.println( 星期五);brea
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 应急安全培训题库大全课件
- 2025广东机电职业技术学院第二批招聘工作人员11人笔试备考试题含答案详解
- 新生儿感染性疾病的病原学分类与传播途径
- 土地抵押合同(标准版)
- 中介和买家合同(标准版)
- 2025年环保设备制造业竞争激烈产品创新趋势分析报告
- 2025年工业互联网平台增强现实交互技术在虚拟现实工厂中的应用报告
- 2025年母婴用品跨境电商行业发展趋势与市场机会研究报告
- 中考英语一轮复习完形填空汇编知识点-+典型题及解析
- 山东省德州市2024-2025学年高三下学期三模化学试题(含答案)
- 成人床旁心电监护护理规程
- 本科生科研管理制度
- 大输液产品研究报告
- 2025版技术服务合同协议
- GB 5768.1-2025道路交通标志和标线第1部分:总则
- 食品仓库记录管理制度
- 企业团委管理制度
- 冻干粉培训课件
- 公路应急抢修合同标准文本
- IQC基础知识培训课件
- 政府代建项目回购协议书范本
评论
0/150
提交评论