云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > JAVA实验报告(实验五)

JAVA实验报告(实验五)

  • 62 次阅读
  • 3 次下载
  • 2025/7/12 21:14:44

s = inTwo.readLine(); // inTwo读取一行 }

inOne.close(); inTwo.close(); out.flush(); out.close(); tofile.close();

inOne = new FileReader(tempFile); // 创建指向文件tempFile的输入流 inTwo = new BufferedReader(inOne); // 创建指向inOne file的输入流 tofile =new FileWriter(file); // 创建指向文件file的输出流

out = new BufferedWriter(tofile); // 创建指向tofile的输出流

while((s = inTwo.readLine())!=null) // inTwo读取一行 {

out.write(s); out.newLine(); }

inOne.close(); inTwo.close(); out.flush(); out.close(); tofile.close();

inOne = new FileReader(file); // 创建指向文件file的输入流

inTwo = new BufferedReader(inOne); // 创建指向inOne file的输入流

while((s = inTwo.readLine())!=null) // inTwo读取一行 {

System.out.println(s); }

inOne.close(); inTwo.close(); tempFile.delete(); }

catch(IOException e) {

System.out.println(e); } } }

实验结果:

在c:/1000目录下存在一个hello.txt文件文件内容如下:

对ReadExample.java编译运行结果为:

分析:程序先新建一个temp.txt文件,将hello.txt的每一行复制到temp.txt并在每一行之前加入行号,然后用temp.txt的每一行覆盖原来hello.txt的每一行,最后在控制台打印添加行号的hello.txt

实验内容2源代码:

import java.io.*;

public class NumberExample {

public static void main(String args[]) {

int a1=12,a2=1180; long b=808080;

float x1=3.14F,x2=12.456F; double d=1234.9876;

boolean boo1=true,boo2=false; char c='我';

File f= new File(\创建文件。 try {

FileOutputStream fos = new FileOutputStream(f); // 创建指向f文件输出流

DataOutputStream out_data = new DataOutputStream (fos); // 创建指向fos 的数据输出流

out_data.writeInt(a1); // out_data将数据a1写入到文件 out_data.writeInt(a2); // out_data将数据a2写入到文件 out_data.writeLong(b); // out_data将数据b写入到文件 out_data.writeFloat(x1); // out_data将数据x1写入到文件 out_data.writeFloat(x2); // out_data将数据x2写入到文件

out_data.writeDouble(d); // out_data将数据d写入到文件

out_data.writeBoolean(boo1);// out_data将数据boo1写入到文件 out_data.writeBoolean(boo2);// out_data将数据boo2写入到文件 out_data.writeChar(c);// out_data将数据c写入到文件 }

catch(IOException e){} try {

FileInputStream fis = new FileInputStream(f); // 创建指向f文件输入流 DataInputStream in_data = new DataInputStream(fis); // 创建指向fis 的数据输入流

System.out.println(\ // in_data读取int整数 System.out.println(\ // in_data读取int整数 System.out.println(\ // in_data读取long整数 System.out.println(\ // in_data读取float数 System.out.println(\ // in_data读取float数 System.out.println(\in_data.readDouble()); // in_data读取double数

System.out.println(\in_data.readBoolean()); // in_data读取boolean数据

System.out.println(\in_data.readBoolean()); // in_data读取boolean数据

System.out.print(\in_data.readChar()); // in_data读取char数据

}

catch(IOException e){} } }

实验结果:

实验前c:/1000内容如下:

对NumberExample.java编译运行结果为

且c:/1000目录下多了一个hello1.txt

由于使用的是二进制I/O类所以hello1.txt以文本模型显示时为乱码:

分析:程序使用二进制I/O将若干字节写入一个名为hello1.txt文件,后将其读出显示于控制台

搜索更多关于: JAVA实验报告(实验五) 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

s = inTwo.readLine(); // inTwo读取一行 } inOne.close(); inTwo.close(); out.flush(); out.close(); tofile.close(); inOne = new FileReader(tempFile); // 创建指向文件tempFile的输入流 inTwo = new BufferedReader(inOne); // 创建指向inOne file的输入流 tofile =new FileW

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com